QuickSort, or Quick Sort, is an efficient method for sorting an array, discovered in 1960 by the British programmer C.A.R. Hoare. For a set of n arbitrary values, the algorithm performs O(n log n) comparisons, but in the worst case, it performs O(n^2) comparisons.
The algorithm is of the divide and conquer type; it sorts a sequence of the array (initially the entire array) as follows:
The following C++ function sorts an array, passed as a parameter:
void QuickSort(int v[], int st, int dr)
{
if(st < dr)
{
// the pivot is initially v[st]
int m = (st + dr) / 2;
int aux = v[st];
v[st] = v[m];
v[m] = aux;
int i = st, j = dr, d = 0;
while(i < j)
{
if(v[i] > v[j])
{
aux = v[i];
v[i] = v[j];
v[j] = aux;
d = 1 - d;
}
i += d;
j -= 1 - d;
}
QuickSort(v, st, i - 1);
QuickSort(v, i + 1, dr);
}
}