Quick Sort — Algorithms

Quick sort picks one number as the pivot (a reference number). Numbers smaller than the pivot are moved to the left, and bigger or equal ones to the right…

Merge sort just splits the array in the middle. Its hard work comes at the end, when it merges. Quick sort is the opposite: its hard work comes at the start, when it splits. So when the parts are joined again, they only need to be put side by side.

Quick sort picks one number as the pivot (a reference number). Then the other numbers are separated: numbers smaller than the pivot go left, bigger ones go right. Both sides are then sorted the same way. It's like lining people up by height. Pick one person. Shorter people stand on their left, taller people on their right, then each group is arranged again the same way. The person you picked is the pivot, and a person's height is a number's value.

Pick a pivot, split, repeat

Here's what happens to [10, 7, 3, 9, 1, 5, 8], with the last number of each part as the pivot:

Part Pivot Left (smaller) Right (bigger)
[10, 7, 3, 9, 1, 5, 8] 8 [7, 3, 1, 5] [10, 9]
[7, 3, 1, 5] 5 [3, 1] [7]
[3, 1] 1 [] [3]
[10, 9] 9 [] [10]

A part with 0 or 1 numbers is always sorted, so it doesn't need to be split again. That's the base case. Then build the result, starting from the shortest parts, in the order left, pivot, right: [1, 3], then [1, 3, 5, 7], then [1, 3, 5, 7, 8, 9, 10]. Look at the first row of the table. After one split, how many numbers are sure to be in their final place? Just one, the pivot 8. Every smaller number is on its left, and every bigger number is on its right.

Here is quick sort in JavaScript, with the last number as the pivot:

function quickSort(arr) {
  if (arr.length <= 1) return arr; // base case

  const pivot = arr[arr.length - 1]; // pivot: the last number
  const rest = arr.slice(0, -1); // every number except the pivot
  const left = rest.filter(x => x < pivot); // smaller
  const right = rest.filter(x => x >= pivot); // bigger or equal

  return [...quickSort(left), pivot, ...quickSort(right)];
}

console.log(quickSort([10, 7, 3, 9, 1, 5, 8]));
// [1, 3, 5, 7, 8, 9, 10]

The Lab's version: partitioning in place

The Lab's code doesn't make new arrays. Its partition function separates the numbers inside the array itself. That's why it's called partitioning (separating) in place:

  1. partition walks from left to right.
  2. Every number smaller than the pivot is swapped into a row at the front of the array.
  3. Finally, the pivot is swapped into the spot right after that row.

With pivot 8, [10, 7, 3, 9, 1, 5, 8] becomes [7, 3, 1, 5, 8, 9, 10]. 7, 3, 1, 5 move to the front, and 8 sits at index 4, its final place. This saves memory, because there's no extra array apart from the recursion's own record (the list of function calls that haven't finished).

Fast, but with a worst case

Big-O says how fast the number of steps grows as the data grows.

Quick sort is not stable: the original order of equal values can change.

⚠️ Lost duplicates, and a pivot that comes back

TL;DR: Quick sort picks one number as the pivot (a reference number). Numbers smaller than the pivot are moved to the left, and bigger or equal ones to the right. Both sides are sorted with recursion (quickSort calls itself), then the result is built as left, pivot, right: [...quickSort(left), pivot, ...quickSort(right)]. After one split, the pivot is in its final place. On average quick sort is O(n log n). Its worst case is O(n²), when the pivot is always the smallest or largest, like sorted data with the pivot at one end. So use a random pivot or the middle number, and a three-way split (<, ===, >) when there are many duplicates. Quick sort isn't stable: the original order of equal values can change.

⚡ Pick a pivot, split, repeat

Write:

function quickSort(arr) {
  if (arr.length <= 1) return arr; // base case

  const pivot = arr[arr.length - 1]; // pivot: the last number
  const rest = arr.slice(0, -1); // every number except the pivot
  const left = rest.filter(x => x < pivot); // smaller
  const right = rest.filter(x => x >= pivot); // bigger or equal

  return [...quickSort(left), pivot, ...quickSort(right)];
}

Meaning: Quick sort picks a pivot (a reference number), here the last number. The other numbers (rest) that are smaller than the pivot go into left, and bigger or equal ones go into right. Both sides are sorted again by calling quickSort (recursion), then put together: left, pivot, right. After one split, the pivot is in its final place.

⚡ >= on the right, the pivot taken out

Guess first: if right uses x > pivot, what happens to [3, 6, 8, 10, 1, 2, 1]? The pivot is the last number, 1.

One of its 1s goes missing. The other 1 is equal to the pivot, so it goes neither left (< pivot) nor right (> pivot).

Meaning: right is taken from rest (every number except the pivot), not from arr. If the pivot goes into right again, data like [2, 2] makes the recursion never finish.

⚡ Fast on average, but with a worst case

Meaning: On average quick sort is O(n log n), because the pivot usually splits the data roughly in two. But when the pivot is always the smallest or largest, like sorted data with the pivot at one end, quick sort becomes O(n²). That's its worst case. The fix: pick a random pivot or the middle number, and a three-way split (<, ===, >) when there are many duplicates.

What you will learn