Pivot
Scanner (j)
Wall (i)
Sorted
6
2
5
9
1
7
4

The Need for Speed

Quick Sort is typically the fastest sorting algorithm in practice. It uses partitioning to place one number (the 'Pivot') in its exact final spot.

Pick a Pivot

We pick the last element (4) as our 'Pivot'. Our goal: Put all smaller numbers to its left, and larger numbers to its right.

Scan: 6

6 > 4. It belongs on the right. We do nothing but continue scanning.

Scan: 2

2 <= 4. It belongs on the left! We swap it with the first 'large' number we found.

Swap 2 & 6

We move the boundary (orange wall) and swap 2 to the safe zone.

Scan: 5

5 > 4. Too big. Ignore.

Scan: 9

9 > 4. Too big. Ignore.

Scan: 1

1 <= 4. Found another small one! We must rescue it from the sea of large numbers.

Swap 1 & 6

We advance the boundary and swap 1 with the next available large number (6).

Scan: 7

7 > 4. Ignore.

Place Pivot

We are done scanning. The boundary is at index 1. The Pivot (4) goes immediately after it, at index 2. We swap 4 with 5.

Focus Left

Pivot 4 is locked. Now we recurse on the left sub-array: [2, 1]. Pivot is 1.

Sort Left

2 > 1. We just swap Pivot (1) with the first large element (2).

Left Sorted

Remaining element 2 is a list of size 1. It is automatically sorted.

Focus Right

Now for the right side: [9, 6, 7, 5]. We pick 5 as Pivot.

Scanning Right

9 > 5. 6 > 5. 7 > 5. All are larger! The 'orange wall' never moves.

Place Pivot 5

Swap Pivot (5) with the first element (9). 5 finds its home.

Final Stretch

Last chunk: [6, 7, 9]. Pivot 9.

Easy Sort

6 < 9. 7 < 9. They stay put. 9 stays put.

Ordered Chaos

Every element has found its place. The array is fully sorted.

Dangerous Speed

On average, O(n log n). But if the pivot choice is bad (e.g. smallest element), it degrades to O(n²)! Randomized pivots usually fix this.

Light Speed.

Quick Sort is the industry standard for a reason.

AlgoAnimator: Interactive Data Structures