LOGBOOK

HELP

Quiz Entry - updated: 2026.12.02

How does quick-sort's partition step split a sequence around the pivot, and what does it cost?

Remove the pivot $x$ and put it into $E$, then take the remaining elements from the front one at a time and append each one to $L$, $E$ or $G$, depending on how it compares with $x$. Every remove and append at an end is $O(1)$, so partitioning $n$ elements costs $O(n)$.

Partitioning 7 2 9 4 3 7 6 1 around the pivot 6 into L = 2 4 3 1, E = 6 and G = 7 9 7

* $L$ and $G$ keep the input order of their elements; sorting them is left to the recursion. *

Algorithm partition(S, p)          // p: position of the pivot
  L, E, G ← empty sequences
  x ← S.remove(p);  E.insertLast(x)
  while ¬S.isEmpty()
    y ← S.remove(S.first())
    if y < x            L.insertLast(y)
    else if y = x       E.insertLast(y)
    else { y > x }      G.insertLast(y)
  return L, E, G

Two reading notes: p is the pivot's position in $S$ (say, the third element), not its value. And in this pseudo-code curly braces mark a comment, so { y > x } states what must hold in the last branch; it is not a block.

$E$ is there for duplicates: every copy of the pivot value is finished business, because it belongs exactly between $L$ and $G$. Without $E$, copies of the pivot would land in $L$ or $G$ and be sorted again; if they all land on the same side, an input made of equal keys produces the most lopsided split possible at every level. With three lists, $n$ equal keys are sorted in a single $O(n)$ partition.

This version builds three new sequences per call, which is easy to follow but costs memory. Practical implementations partition in place, by swapping elements inside the array.

Go deeper:

From Quiz: ADS / Merge-Sort and Quick-Sort | Updated: Dec 02, 2026