#include <iostream> using namespace std; class QuickSort { public: int* quickSort(int* A, int n) { quickSortRecursive(A, 0, n - 1); return A; } void quickSortRecursive(int* A, int index_beg, int index_end){ if(index_beg < index_end){ int median = partition(A, index_beg, index_end); quickSortRecursive(A, index_beg, median - 1); quickSortRecursive(A, median + 1, index_end); } } int partition(int* A, int index_beg, int index_end){ int pivot = index_end; int tmp, index_partitioned = index_beg; for(int index = index_beg; index < index_end; index++){ if(A[index] <= A[pivot]){ tmp = A[index]; A[index] = A[index_partitioned]; A[index_partitioned] = tmp; // cout<<index<<" is swap to "<<index_partitioned<<endl; // cout<<"index: "<<index<<endl; // cout<<"index_partitioned: "<<index_partitioned<<endl; index_partitioned++; // cout<<"value after swap: "<<endl; // for(int i = index_beg; i <= index_end; i++) // cout<<A[i]<<" "; // cout<<endl; } } tmp = A[pivot]; A[pivot] = A[index_partitioned]; A[index_partitioned] = tmp; // cout<<"swap povit: "; // for(int i = index_beg; i <= index_end; i++) // cout<<A[i]<<" "; // cout<<endl; return index_partitioned; } }; int main() { int a[6] = {1, 5, 7, 2, 9, 4}; int* res; QuickSort sorter; res = sorter.quickSort(a, 6); cout<<"after sorting:"<<endl; for(int index = 0; index < 6; index ++){ cout<<res[index]<<" "; } cout<<endl; return 0; }