-4

The program below is for sorting a list using quicksort C++.The code typed below has compiled successfully in code::blocks and in http://cpp.sh/, but unfortunately it hangs after entering in the elements,any help will be appreciated..

void quicksort(vector<int> v,int left_index,int right_index)
{
    if(left_index>=right_index)
        return;
    int pivot=(right_index+left_index)/2;

    int left=left_index;
    int right=right_index;
    while(left<=right)
    {
        while(v[left]<v[pivot])
            left++;
        while(v[right]>v[pivot])
            right--;
        if(left<=right)
        {
            swap(v,left,right);
            left++;right--;
        }
    }
    quicksort(v,left_index,right);
    quicksort(v,left,right_index);
}
  • 5
    The right tool to solve such problems is your debugger. You should step through your code line-by-line *before* asking on Stack Overflow. For more help, please read [How to debug small programs (by Eric Lippert)](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). At a minimum, you should \[edit] your question to include a [Minimal, Complete, and Verifiable](http://stackoverflow.com/help/mcve) example that reproduces your problem, along with the observations you made in the debugger. – πάντα ῥεῖ Oct 11 '16 at 13:31
  • You should learn about [passing parameters as references](http://www.learncpp.com/cpp-tutorial/73-passing-arguments-by-reference/) and when to pass by value. See also http://stackoverflow.com/a/3399248/104774 – stefaanv Oct 11 '16 at 13:45
  • 1
    Aside from passing by reference, I don't think your inplementation is guaranteed to make progress: if one of the partions is empty, the recursion gets stuck and either overflows the stack or runs forever. – Dietmar Kühl Oct 11 '16 at 13:53
  • @stefaanv I learnt quicksort from this place: http://www.bogotobogo.com/Algorithms/quicksort.php , where they're able to get the output even without passing by reference. – midnightmoon Oct 11 '16 at 16:15
  • @DietmarKühl That is why the base case of if(left_index>=right_index) return; has been specified,this avoids the scenario you listed above.Please correct me if I'm wrong. – midnightmoon Oct 11 '16 at 16:18
  • @JeswinJacob: the intereating side is the non-empty side! You check is for the empty side. – Dietmar Kühl Oct 11 '16 at 17:25

1 Answers1

1
  1. Passing by reference is must as others have pointed out.
  2. Keep pivot constant during a partition. pivot = v[pivot] ensures that.
  3. outer loop bounds changed to left<=right from left<right.

The running code.

#include <iostream>
#include<vector>
using namespace std;

void print(const vector<int> &v)
{
    cout<<"The sorted list is:"<<endl;
    for(int i=0;i<(int)v.size();i++)
        cout<<v[i]<<' ';
    cout<<endl;
}
void swap(vector<int> &v,int left,int right)
{
    int temp=v[left];
    v[left]=v[right];
    v[right]=temp;
}

void quicksort(vector<int> &v,int left_index,int right_index)
{
    if(left_index>=right_index)
        return;
    int pivot=(right_index+left_index)/2;

    pivot = v[pivot];
    int left=left_index;
    int right=right_index;
    while(left<right)
    {
        while(v[left]<=pivot)
            left++;
        while(v[right]>pivot)
            right--;
        if(left<right){
            swap(v,left,right);
            left++;
            right--;
        }
    }
    quicksort(v,left_index,right);
    quicksort(v,left,right_index);
    print(v);
}

int main()
{
    int no;
    vector<int> v;
    cout << "Please enter the elements in your list" << endl;
    cout << "Enter 0 to exit..."<<endl;
    while(cin >> no)
    {
        if(no==0)
            break;
        v.push_back(no);
    }
    quicksort(v,0,v.size()-1);
    return 0;
}
v78
  • 2,803
  • 21
  • 44