1

Merge sort is not sorting, the program asks for a number which is then sent into the merge sort function, to be sorted.

Had this problem for a few days and still cant pinpoint the error

The program runs fine, it is just when the merge sort function is called that it isn't processing correctly - evident due to merge sort being able to sort 100000 numbers in 0ms (apparently)

Why is this not working correctly? can you see why the vector isn't being sorted?

   //merge sort
    void merge(vector<int>& a, int l, int m, int r)
    {
        int i, j, k;
        int n1 = m - l + 1;
        int n2 = r - m;

        /* create temp aays */
        std::vector<int> L(n1), R(n2);


        /* Copy data to temp arrays L[] and R[] */
        for (i = 0; i < n1; i++)
            L[i] = a[l + i];
        for (j = 0; j < n2; j++)
            R[j] = a[m + 1 + j];

        /* Merge the temp aays back into a[l..r]*/
        i = 0; // Initial index of first subaay
        j = 0; // Initial index of second subarray
        k = l; // Initial index of merged subarray
        while (i < n1 && j < n2)
        {
            if (L[i] <= R[j])
            {
                a[k] = L[i];
                i++;
            }
            else
            {
                a[k] = R[j];
                j++;
            }
            k++;
        }

        /* Copy the remaining elements of L[], if there
        are any */
        while (i < n1)
        {
            a[k] = L[i];
            i++;
            k++;
        }

        /* Copy the remaining elements of R[], if there
        are any */
        while (j < n2)
        {
            a[k] = R[j];
            j++;
            k++;
        }
    }

    /* l is for left index and r is right index of the
    sub-array of arr to be sorted */
    void mergeSort(vector<int>& a, int l, int r)
    {
        if (l < r)
        {
            // Same as (l+r)/2, but avoids overflow for
            // large l and h
            int m = l + (r - l) / 2;

            // Sort first and second halves
            mergeSort(a, l, m);
            mergeSort(a, m + 1, r);

            merge(a, l, m, r);
        }
    }
int main(int argc, char const *argv[])
{

    int num;
    int upperBound = 5;
    cout << "Enter the size of the vector to be sorted: ";
    cin >> num;

    vector<int> a(num, upperBound);
    if (num <= 20)
    {
        for (int i = 0; i < num; i++)
        {
            cout << "Enter numbers: ";
            cin >> a[i];
        }
    }
    else
    {
        for (int i = 0; i < num; i++)
        {
            a[i] = rand() % upperBound;
        }
    }
    //
    //
    int selection;
    cout << "What sorting algoithm would you like to use. 1. Bubble Sort 2. Quicksort 3.Mergesort: ";
    cin >> selection;
    //quicksort setting start and end of vector
    int p = 0;
    int q = num;



    switch (selection) {
    case 1://Passes Numbers to be 'Bubble' sorted
    {
        //cout << "======Original=======" << endl;;
        //printVector(a);
        //cout << "======Sorted=======" << endl;
        the_clock::time_point start = the_clock::now(); // start clock

        bubbleSort(a);
        the_clock::time_point end = the_clock::now(); //end clock
        auto time_taken = duration_cast<milliseconds>(end - start).count(); //calculating time

        //printVector(a);
        cout << "It took " << time_taken << " ms." << endl; //displaying in ms
    }
    break;       // and exits the switch
    case 2://Passes to quicksort
    {
        cout << "======Original=======" << endl;
        //for (auto e : a)
        //  cout << e << " ";
        //cout << endl;

        the_clock::time_point startTime = the_clock::now(); // start clock

        quickSort(a, p, q);

        the_clock::time_point endTime = the_clock::now(); //end clock
        auto time_taken = duration_cast<milliseconds>(endTime - startTime).count(); //calculating time


        cout << "======Sorted=======" << endl;
        //for (auto e : a)
        //  cout << e << " ";
        //cout << endl;
        cout << "It took " << time_taken << " ms." << endl; //displaying in ms
    }
    break;
    case 3:
    {

        int a_size = sizeof(a) / sizeof(a[0]);
        the_clock::time_point startTime = the_clock::now(); // start clock

        mergeSort(a, 0, a_size - 1);

        the_clock::time_point endTime = the_clock::now(); //end clock
        auto time_taken = duration_cast<milliseconds>(endTime - startTime).count(); //calculating time


        cout << "======Sorted=======" << endl;
        //for (auto e : a)
        //  cout << e << " ";
        //cout << endl;
        cout << "It took " << time_taken << " ms." << endl; //displaying in ms
    }break;




    // and exits the switch
    }



}
Harvvv
  • 21
  • 4
  • Recommend making a compilable program that only performs the merge sort. Less code to hunt for bugs in. – user4581301 Dec 02 '17 at 22:16
  • 1
    [Interesting read](https://stackoverflow.com/questions/24650626/how-to-implement-classic-sorting-algorithms-in-modern-c). Second, instead of 100000 numbers, you should be trying to sort maybe 5 or 6 **known** numbers, not random numbers. Using random numbers just creates more confusion as the numbers change each time you run your application. – PaulMcKenzie Dec 02 '17 at 22:19
  • Definitely a bug in there. This should help you find it: https://ideone.com/VjtJLx – user4581301 Dec 02 '17 at 22:22
  • was just using 100000 numbers as an example, I have tested 5or6, I will use just merge sort and search for the bug – Harvvv Dec 02 '17 at 22:30
  • Use your debugger. Look at how many items you're telling `mergeSort` to sort. – 1201ProgramAlarm Dec 03 '17 at 00:40

0 Answers0