Skip to content
Ayotl

← Algorithms · Lesson 5/11

Quicksort

▶ Watch it as a video: one minute with LeIA

Watch on YouTube →

✕
1/7

A pivot

Quicksort also divides, but the other way around from merge sort: it arranges first and splits afterwards. It picks a pivot (here, the last one in the range) and walks the range, leaving every number smaller than it on the left. At the end it drops the pivot right after them: that's its final slot. Then it repeats the same thing, separately, on what's to its left and what's to its right.

In the program, j walks the range and i marks where the next smaller number goes; this is called the Lomuto partition. The function takes the range (ini and fin), so the first call is rapido(a, 0, a.length - 1).

5283716401234567i, j ↓p ↓
leccion.js

$ node leccion.js

Press RUN (or Ctrl+Enter).

What to look at

  • It returns nothing: it sorts the array in place, with no copies.
  • The swap fits on one line with destructuring: [a[i], a[j]] = [a[j], a[i]].
The whole lesson on one page

A pivot

Quicksort also divides, but the other way around from merge sort: it arranges first and splits afterwards. It picks a pivot (here, the last one in the range) and walks the range, leaving every number smaller than it on the left. At the end it drops the pivot right after them: that's its final slot. Then it repeats the same thing, separately, on what's to its left and what's to its right.

In the program, j walks the range and i marks where the next smaller number goes; this is called the Lomuto partition. The function takes the range (ini and fin), so the first call is rapido(a, 0, a.length - 1).

5283716401234567i, j ↓p ↓
function rapido(a, ini, fin) {
  if (ini >= fin) return;
  const p = a[fin];
  let i = ini;
  for (let j = ini; j < fin; j++) {
    if (a[j] < p) {
      [a[i], a[j]] = [a[j], a[i]];
      i++;
    }
  }
  [a[i], a[fin]] = [a[fin], a[i]];
  rapido(a, ini, i - 1);
  rapido(a, i + 1, fin);
}

const lista = [5, 2, 8, 3, 7, 1, 6, 4];
rapido(lista, 0, lista.length - 1);
console.log(lista);
  • It returns nothing: it sorts the array in place, with no copies.
  • The swap fits on one line with destructuring: [a[i], a[j]] = [a[j], a[i]].

The partition, step by step

Just the first partition, with 4 as the pivot: it prints the array after checking each j and says whether it was smaller than the pivot.

function particion(a, ini, fin) {
  const p = a[fin];
  let i = ini;
  for (let j = ini; j < fin; j++) {
    const menor = a[j] < p;
    if (menor) {
      [a[i], a[j]] = [a[j], a[i]];
      i++;
    }
    console.log("j =", j, menor ? "smaller" : "bigger", "→", ...a);
  }
  [a[i], a[fin]] = [a[fin], a[i]];
  console.log("pivot", p, "lands in slot", i, "→", ...a);
}

particion([5, 2, 8, 3, 7, 1, 6, 4], 0, 7);
  • The ones smaller than 4 (the 2, the 3 and the 1) gather together at the front.
  • At the end, the 4 swaps with the first of the bigger ones and lands in slot 3: only smaller numbers to its left, only bigger ones to its right. It never moves again.

Average and worst case

If the pivot splits the range roughly in half, there are about log n levels and each one walks n numbers: O(n log n), like merge sort, but with no extra memory and very little work per step. That's why so many libraries use it, in some variant, under the hood.

The risk is the pivot: if it's always the biggest (or the smallest), each partition removes just one number and it's back to O(n²). With the last one as the pivot, that happens exactly on an already sorted list. Real implementations pick the pivot better: at random, or the median of three.

let c = 0;
function rapido(a, ini, fin) {
  if (ini >= fin) return;
  const p = a[fin];
  let i = ini;
  for (let j = ini; j < fin; j++) {
    c++;
    if (a[j] < p) {
      [a[i], a[j]] = [a[j], a[i]];
      i++;
    }
  }
  [a[i], a[fin]] = [a[fin], a[i]];
  rapido(a, ini, i - 1);
  rapido(a, i + 1, fin);
}

// random numbers: always the same ones, because the seed is fixed
function alAzar(n) {
  let s = 42;
  return Array.from({ length: n }, () => (s = s * 16807 % 2147483647) % 1000);
}

for (const n of [100, 1000]) {
  c = 0;
  rapido(alAzar(n), 0, n - 1);
  console.log(n, "random:", c, "comparisons");
}
c = 0;
rapido(Array.from({ length: 1000 }, (_, i) => i + 1), 0, 999);
console.log("1000 already sorted:", c, "comparisons");
  • Random: 690 comparisons with 100 numbers and about 10,800 with 1,000, close to merge sort's 8,700.
  • Already sorted: 499,500, the same as bubble sort. Every partition leaves everything on one side of the pivot.

Practice with a video challenge

Further reading