Bitpath

Boss lesson · Unit 03

Quicksort

Partition around a pivot, then sort each side in place

13 exercisesabout 3 min

What you practise

  • Lomuto partition
  • The pivot lands in its final place
  • O(n²) on sorted input with a last-element pivot

Try a question from this lesson

Pick one

Pick one value from a list and call it the pivot. Move every smaller value to its left and every other value to its right. What is now true of the pivot?

The ideas

Concept cards are short on purpose. In the app they appear between questions, after you have met the idea.

Concept

Partition

This partition uses the last item of the range lo..hi as the pivot.

i marks where the next smaller value belongs. j scans the range: each value smaller than the pivot is swapped to position i, and i moves up. At the end the pivot is swapped into position i, between the two groups, and i is returned.

def partition(a, 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]
    return i
function partition(a, lo, hi) {
  const pivot = a[hi];
  let i = lo;
  for (let j = lo; j < hi; j++) {
    if (a[j] < pivot) {
      [a[i], a[j]] = [a[j], a[i]];
      i++;
    }
  }
  [a[i], a[hi]] = [a[hi], a[i]];
  return i;
}
function partition(
  a: number[], lo: number, hi: number,
): number {
  const pivot = a[hi];
  let i = lo;
  for (let j = lo; j < hi; j++) {
    if (a[j] < pivot) {
      [a[i], a[j]] = [a[j], a[i]];
      i++;
    }
  }
  [a[i], a[hi]] = [a[hi], a[i]];
  return i;
}

TypeScript noteA long parameter list is wrapped over several lines. ): number { then gives the return type: the index where the pivot ends up.

Concept

Quicksort

Partition puts one value in its final place. Quicksort repeats that on each side.

Here partition(a, lo, hi) places the pivot of the range and returns its index p. The left side lo..p - 1 and the right side p + 1..hi are then sorted the same way. A range of 0 or 1 items is the base case.

Everything happens inside the one list: no merging, no copies.

def quicksort(a, lo, hi):
    if lo >= hi:
        return
    p = partition(a, lo, hi)
    quicksort(a, lo, p - 1)
    quicksort(a, p + 1, hi)
function quicksort(a, lo, hi) {
  if (lo >= hi) return;
  const p = partition(a, lo, hi);
  quicksort(a, lo, p - 1);
  quicksort(a, p + 1, hi);
}
function quicksort(
  a: number[], lo: number, hi: number,
): void {
  if (lo >= hi) return;
  const p = partition(a, lo, hi);
  quicksort(a, lo, p - 1);
  quicksort(a, p + 1, hi);
}

TypeScript note): void says the function returns nothing: it sorts the array it was given in place.

10 more exercises in the app

  • 3× type the answer
  • 3× multiple choice
  • 2× fill the blanks
  • 1× true or false
  • 1× match the pairs

Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.

One email when Bitpath is ready to try. Nothing else. Privacy