AlgoLab

O(n log n)·O(log n) space·not stable
645559896316373459948511843281951114277

Starting quicksort on an array of 20 items.

Starting quicksort on an array of 20 items.
  1. 1if the range holds more than one value, choose the last value as the pivot
  2. 2compare each other value in the range with the pivot
  3. 3if a value is smaller than the pivot, swap it next to the smaller group
  4. 4swap the pivot into place between the smaller and larger groups
  5. 5recursively sort the values smaller than the pivot
  6. 6recursively sort the values larger than the pivot
  7. 7the array is fully sorted
Comparisons
0
Writes
0
Accesses
0

Complexity

BestO(n log n)
AverageO(n log n)
WorstO(n²)
Extra spaceO(log n)
StableNo
n² at n=20 predicts 400; this run took 0.

Quicksort

Quicksort picks a pivot value, partitions the range so every smaller value ends up to its left and every larger value to its right, then recursively sorts each side. This implementation uses the Lomuto partition scheme, always choosing the last element of each range as the pivot.

When it is the right choice

Quicksort is usually the fastest general-purpose comparison sort in practice, and it partitions in place, needing only O(log n) auxiliary space for its recursion. Its worst case is O(n²), and this fixed last-element pivot rule makes that worst case easy to trigger deliberately: an already-sorted or reverse-sorted array. Production implementations avoid this by choosing a random or median-of-three pivot instead of a fixed one, for exactly this reason.

How it works

Worth noticing

Cost

Best
O(n log n)
Average
O(n log n)
Worst
O(n²)
Extra space
O(log n)
Stable
No

Worth comparing

All algorithms