lou.tools
EN

Quicksort

Quicksort, step by step

Watch it sort any list, step by step, with its pseudocode and its Python.

Updated

Free, in your browser. Your lists never leave it.Privacy

8 values to sort.

Comparisons: 0Swaps: 0lo = 0 · hi = 7

What happens next?

The run stops five times and asks what comes next. Answer, and it plays on.

Pseudocode and implementation

Pseudocode

  1. To sort the part of the list from lo to hi (at first, the whole list):
  2. If the part has more than one value, take its last value as the pivot; i is where the next value smaller than the pivot will go, lo at first.
  3. Walk the part from lo to just before the pivot:
  4. if the value is smaller than the pivot,
  5. swap it to position i and move i one place right.
  6. Swap the pivot to position i: it is in its final place.
  7. Sort the values before it and the values after it, each the same way.
  8. Return the sorted list.

Python

def quicksort(a, lo=0, hi=None):    if hi is None:        hi = len(a) - 1    if lo < hi:        pivot = a[hi]        i = lo        for j in range(lo, hi):            if a[j] < pivot:                a[i], a[j] = a[j], a[i]                i += 1        a[i], a[hi] = a[hi], a[i]        quicksort(a, lo, i - 1)        quicksort(a, i + 1, hi)    return a

How it works

Quicksort picks a value of the part it is sorting as the pivot (here, the last one) and partitions the part: every value smaller than the pivot goes before it, every other value after it. The pivot is then in its final place, and each side is sorted the same way, until the parts have one value or none.

The partition here is Lomuto's: one walk from left to right with a marker, i, where the next smaller value goes. Each smaller value is swapped to i and i moves on, and at the end the pivot is swapped to i. Each level of partitions looks at every value once, so with pivots near the middle there are about log₂ n levels and n log₂ n comparisons.

With bad pivots it is slow: on a list already sorted, the last value is always the greatest, each partition leaves everything on one side, and it takes about n²/2 comparisons. Real implementations pick the pivot at random or as the median of three. It sorts in place, with only the recursion's memory, but it is not stable.

Complexity

Best caseO(n log n)
Average caseO(n log n)
Worst caseO(n²)
Extra spaceO(log n)
StableNo
In placeYes

n is how many values the list has.

Questions

When is quicksort a good choice?
For sorting arrays in memory when speed matters more than stability: on average it is the fastest of the classic sorts, and it needs no second list. Pick the pivot at random, or as the median of three, so that sorted or nearly sorted input does not make it quadratic.
How is it different from merge sort?
Merge sort splits at the middle without looking and does its work merging; quicksort does its work partitioning around a pivot and has nothing to merge. Quicksort works in place and is usually faster; merge sort is always n log n and stable.
What is the most common mistake?
Leaving the pivot in the recursive calls. After the partition it is in its final place, so the two calls sort lo to i − 1 and i + 1 to hi; including i can make the recursion never end.
How does "What happens next?" work?
It runs quicksort on a new random list and stops five times to ask what comes next: whether a value is smaller than the pivot, where the pivot ends up, which pair is compared, or how the list looks after a partition. Your answers stay in your browser.

More algorithms