c++algorithmdata-structuresmergesortdivide-and-conquer

Divide and conquer algo. not giving proper output


I tried to print the left and right parts of an array using divide and conquer algorithm but it is not giving output as required.

The code is given below:

#include <iostream>

using namespace std;

void merge(int *arr, int l, int mid, int r) {
    cout << "Left Part: ";
    for (int i = 0; i < mid + 1; i++) {
        cout << arr[i] << " ";
    }
    // cout << "\n";

    //print right part
    cout << "Right Part: ";
    for (int i = mid + 1; i < r + 1; i++) {
        cout << arr[i] << " ";
    }
    // cout << "\n";
}

void divide(int *arr, int l, int r) {
    int mid;
    if (l < r) {
        mid = (l + (r - l)) / 2;

        divide(arr, l, mid);
        divide(arr, mid + 1, r);
        merge(arr, l, mid, r);
    }
}

int main() {
    int arr[] = { 38, 27, 43, 3, 9, 82, 10 };
    divide(arr, 0, 6);
    return 0;
}

output:

Left Part: 38 Right Part: 27 

Not printing printing full array


Solution

  • The way you compute the mid index is incorrect: instead of mid = (l + (r - l)) / 2; you should write:

    mid = l + (r - l) / 2;
    

    Note also these remarks:

    In your implementation, r is the index of the last element of the right part and mid is the index of the last element of the left part. This is somewhat confusing. It is idiomatic in C and C++ to select mid as the index of the first item in the right part and r the index past the last element of the right part. This allow the first call to use the length of the array and avoids confusing and error prone +1/-1 adjustments.

    Here is a modified version:

    #include <iostream>
    
    using namespace std;
    
    void merge(int *arr, int start, int mid, int end) {
        cout << "Left Part:";
        for (int i = start; i < mid; i++) {
            cout << " " << arr[i];
        }
        cout << "\n";
    
        //print right part
        cout << "Right Part:";
        for (int i = mid; i < end; i++) {
            cout << " " << arr[i];
        }
        cout << "\n";
    }
    
    void divide(int *arr, int start, int end) {
        if (end - start > 1) {
            int mid = start + (end - start) / 2;
            divide(arr, start, mid);
            divide(arr, mid, end);
            merge(arr, start, mid, end);
        }
    }
    
    int main() {
        int arr[] = { 38, 27, 43, 3, 9, 82, 10 };
        divide(arr, 0, sizeof(arr) / sizeof(arr[0]));
        return 0;
    }