1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
|
#include<iostream>
#include<vector>
using namespace std;
int partition(vector<int>&,int,int);
void quickSort(vector<int>& arr,int left, int right)
{
int middle; //left it uninitialized. Was not sure whether to pass it or not.
//Anyway, doesn't really affecting the program.
cout<<"calling quickSort()"<<endl;
cout<<"left is\t"<<left<<endl;
cout<<"right is\t"<<right<<endl;
cout<<"middle is\t"<<middle<<endl;
if(left < right)
{
middle = partition(arr,left,right);
quickSort(arr,left,middle);
quickSort(arr,middle+1,right);
}
return;
}
int partition(vector<int>& a,int left,int right)
{
cout<<"Calling partition\n"<<endl;
int x= a[left];
int i = left-1; //out of bound. But brought back it back in the loop
int j = right+1; //out of bound. But brought back it back in the loop
int temp;
do
{
cout<<"x is\t"<<x<<endl;
cout<<"i is\t"<<i<<"\t"<<"a[i] is\t"<<a[i]<<endl;
cout<<"j is\t"<<j<<"\t"<<"a[j] is\t"<<a[j]<<endl;
cout<<endl<<endl;
do
{
j--;
}while(x>a[j]);
do
{
i++;
}while(x<a[i]);
if(i<j)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}while(i<j);
return j;
}
int main()
{
vector<int> array;
array.push_back(20);
array.push_back(10);
array.push_back(30);
array.push_back(60);
array.push_back(40);
array.push_back(50);
cout<<"size of vector is\t"<<array.size()<<endl;
quickSort(array,0,5);
cout<<" sorted array is\t";
for(int i=0;i<array.size();i++)
{
cout<<array[i]<<" ";
}
return 0;
}
|