#include <iostream> using namespace std; void swap(int &a, int &b) { int temp; temp = a; a = b; b = temp; } void qsort(int* A, int left, int right) { int i = left; int j = right; int middle = (i+j)/2; while(i<j) { while((A[i]<A[middle]) && (i<right)) i++; while((A[middle]<A[j]) && (j>left)) j--; if (i<j) swap(A[i], A[j]); i++; j--; //if(i<=j) swap(A[i++], A[j--]); (*) //line * can substitute the three lines above } if(j>left) qsort(A, left, j); if(i<right) qsort(A, i, right); } int main() { int A[8] = {23, 5, 23, 52, 523, 62, 32, 12}; qsort(A, 0, 7); for(int i=0; i<8; i++) cout << A[i] << " " ; cout << endl; system("pause"); return 0; } //here are the codes for quick sort.. // C++ template: template <class Elem, class Comp> void qsort(Elem A[], int i, int j) { if (j <= i) return ; int pivotindex = findpivot(A, i, j); swap(A, pivotindex, j); int k = partition<Elem, Elem>(A, i-1, j, A[j]); swap(A, k, j); qsort<Elem, Elem>(A, i, k-1); qsort<Elem, Elem>(A, k+1, j); } template <class Elem> int findpivot(Elem A[], int i, int j) { return (i+j)/2; } template <class Elem, class Comp> int partition(Elem A[], int l, int r, Elem& pivot) { do { while (Comp::lt(A[++l], pivot)); while ((r!=0) && Comp::gt(A[--r], pivot)); swap(A, l, r); }while (l < r); swap(A, l, r); return l; }