Search code examples
c++sortingrecursionquicksort

No output in C++ program despite no errors and complete execution


I wrote the following recursive program in C++ to sort an array using QuickSort Algorithm, but I am not getting any output(while I should have received output as space separated array because of cout) despite the program execution being completed.

I am relatively new to C++(not to programming) and wasn't able to find anything specific wrt my issue.

Please Help!!

CODE:

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

int partition(int a[], int low, int high)
{
    int pivot = a[high];
    int x = low;
    int y = high;

    while (x < y)
    {
        do
        {
            x++;
        } while (a[x] <= pivot);
        do
        {
            y--;
        } while (a[y] > pivot);

        a[x] = a[x] + a[y];
        a[y] = a[x] - a[y];
        a[x] = a[x] - a[y];
    }

    a[x] = a[x] + a[high];
    a[high] = a[x] - a[high];
    a[x] = a[x] - a[high];

    return x;
}

void quickSort(int a[], int low, int high)
{
    if (low < high)
    {
        int new_limit = partition(a, low, high);
        quickSort(a, low, new_limit);
        quickSort(a, new_limit + 1, high);
    }
}

int main()
{
    int n;
    int a[1001];
    cin >> n;
    for (int i = 1; i <= n; ++i)
    {
        cin >> a[i];
    }
    a[0] = -1001;

    quickSort(a, 0, n);

    for (int i = 1; i <= n; ++i)
    {
        cout << a[i] << " ";
    }

    return 0;
}

Putting random cout statements, only the statements before the recursive quicksort calls within quicksort functions are returning output.

My Input:

7
8 7 14 6 98 5 4

Solution

  • I already place comments besides the bugs you made.

    #include <cmath>
    #include <vector>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    
    int partition(int a[], int low, int high)
    {
        int pivot = a[high];
        int x = low;
        int y = high - 1; // should be high - 1
    
        while (x <= y) // this must be less than or equal to account for x == y
        {
            while (x <= high - 1 && a[x] <= pivot) { // should use while loop instead of while do loop
                x++;
            }
            while (y >= low && a[y] > pivot) {
                y--;
            }
            if (x < y) { // swap only if the array has not been completely partitioned
                a[x] = a[x] + a[y]; 
                a[y] = a[x] - a[y];
                a[x] = a[x] - a[y];
            }
        }
    
    
        if (x != high){ // you must check x != high because of this obfuscated swap, think about why
            a[x] = a[x] + a[high]; 
            a[high] = a[x] - a[high];
            a[x] = a[x] - a[high];
        }
    
        return x;
    }
    
    void quickSort(int a[], int low, int high)
    {
        if (low < high)
        {
            int new_limit = partition(a, low, high);
            quickSort(a, low, new_limit - 1); // new_limit - 1 instead of new_limit
            quickSort(a, new_limit + 1, high);
        }
    }
    
    int main()
    {
        int n;
        int a[1001];
        cin >> n;
        for (int i = 1; i <= n; ++i)
        {
            cin >> a[i];
        }
        a[0] = -1001;
    
        quickSort(a, 1, n); // pass index 1 in lower bound if you want don't want to use a[0]!
    
        for (int i = 1; i <= n; ++i)
        {
            cout << a[i] << ' ';
        }
        return 0;
    }
    

    output for your input:

    4 5 6 7 8 14 98