Quick Sort
| ||||||||||||||||||||||||||||||||||||||||||||||||||
This sort uses recursion - the process of "calling itself". Recursion will be studied at a later date.
//Quick Sort Functions for Descending Order
// (2 Functions) void QuickSort(apvector <int> &num, int top, int bottom) { // top = subscript of beginning of array // bottom = subscript of end of array int middle; if (top < bottom) { middle = partition(num, top, bottom); quicksort(num, top, middle); // sort first section quicksort(num, middle+1, bottom); // sort second section } return; } //Function to determine the partitions // partitions the array and returns the middle subscript int partition(apvector <int> &array, int top, int bottom) { int x = array[top]; int i = top - 1; int j = bottom + 1; int temp; do { do { j - -; }while (x >array[j]); do { i++; } while (x <array[i]); if (i < j) { temp = array[i]; array[i] = array[j]; array[j] = temp; } }while (i < j); return j; // returns middle subscript } |
Thursday 17 March 2016
About Rishabh Aggarwal
Competitive Programmer and Ruby on Rails Developer
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment