// File: QuickSort.cpp // Quick Sort Implementation #include #include #include using namespace std; int Partition(int low,int high,int arr[]); void Quick_sort(int low,int high,int arr[]); void print(int arr[],int elts); // A couple of global variables so we can show what's going on... int global_n; bool global_flag; int main(int argc,char ** argv) { int *a,n,low,high,i; global_flag=(argc>1); cout<<"Enter number of elements:"; cin>>n; a=new int[global_n=n]; /* Enter data manually cout<<"enter the elements:"; for(i=0;i>a; */ // Generate data randomly srand(time(NULL)); for(i=0;ilow) { high_vac=arr[high]; // Working right-to-left, // find the first value in the list less than or equal to the pivot while(pivotlow_vac) // true the 1st time, unless while above broke { if(high<=low) break; low++; low_vac=arr[low]; } // copy 1st value bigger than pivot into rightmost value l.t. pivot // result is swapping values about pivot, and ordering at least two values arr[high]=low_vac; if (global_flag) { cout << " Array after this iteration in partition:\n"; print(arr,global_n); cout << "Boundaries:" << low << "," << high << endl; } } if (global_flag) cout << "Array Partitioned. Pivot index is " << low << endl; // Low is now == high: // elts below low are l.t. pivot & elts above low are g.t. pivot arr[low]=pivot; // copy the pivot back into its spot return low; // return the index of the pivot }