Quicksort is an in-place divide-and-conquer sorting algorithm. Its efficiency comes from partitioning the array around a chosen pivot so that smaller elements move to the left and larger ones to the right. The algorithm then recursively sorts the two sides.

A[p : q−1] ≤ pivot = A[q]A[q+1 : r] > pivot
The pivot ends in its correct final position.QUICKSORT(A, p, q−1)QUICKSORT(A, q+1, r)QUICKSORT(A, p, r)
if p < r
// Partition the subarray around the pivot, which ends up in A[q].
q = PARTITION(A, p, r)
QUICKSORT(A, p, q-1) // recursively sort the low side
QUICKSORT(A, q+1, r) // recursivey sort the high side
In the PARTITION:
A[r].i for the boundary of elements ≤ pivot.j; whenever A[j] ≤ pivot, increment i and swap.i+1.i+1 as the pivot’s final index.This ensures correct separation of elements and enables recursive sorting.
PARTITION(A, p, r)
x = A[r] // the pivot
i = p – 1 // highest index into the low side
for j = p to r – 1 // process each element other than the pivot
if A[j] ≤ x // does this element belong on the low side?
i = i + 1 // index of a new slot in the low side
exchange A[i] with A[j] // put this element there
exchange A[i + 1] with A[r] // pivot goes just to the right of the low side
return i + 1 // new index of the pivot
