Skip to the workspace
Pebellslearn by building
Recursion, Searching & Sorting  ›  Lesson

Quick Sort & Top Picks

Quick Sort 25 minPartition around a pivot — to sort in place, or to find the top k without sorting

dsa / Quick Sort & Top Picks

Explore each scene at your own pace.

01 · Build Top Picks — find the top 10 without sorting 50,000

Build Top Picks — find the top 10 without sorting 50,000

Quick Sort & Top Picks

Definition
Quick sort sorts a list in place by choosing a pivot, partitioning the items so smaller ones sit before it and the rest after it, then recursing on each side; it averages O(n log n) but degrades to O(n²) with bad pivots.
In plain words
Pick one person in a line as the pivot and ask everyone shorter to stand on their left and everyone taller on their right. The pivot is now in the right place, and you repeat the idea on each side.

In short: quick sort's core move is partitioning: pick a pivot, rearrange the list so everything smaller sits left of it and everything else sits right, and the pivot is then in its final sorted position. Recurse on each side. It sorts in place (no second list, unlike merge sort) in O(n log n) on average — but a bad pivot choice degrades to O(n²), and the partition step alone, stopped early, finds the top-k items in about O(n) without sorting the rest.

A line of people with a "pivot" person holding a sign, a wall sliding left-to-right behind them sorting shorter people to the left of the wall and taller to the right, pivot stepping into the gap at the end

Build Top Picks — find the top 10 without sorting 50,000

Tunebox's home screen shows the 10 most-played tracks out of 50,000. Sorting everything to keep 10 is wasted work. The Lomuto partition uses the last value as pivot and one wall i: everything before i is known smaller than the pivot. Scan with j; swap each smaller value across the wall:

watch it move · array

Lomuto partition keeps one wall, i, with everything left of it smaller than the pivot, while j scans for values to throw over the wall.

The pivot is the last value, 25, and nothing is known to be smaller yet, so the wall i starts at 0. 7 cells. pivot at index 6. i at index 0. j at index 0.

1 / 12

The same partition, smaller, stepped as plain code:

step through it
1items = [4, 8, 1, 5]
2pivot = items[3]
3i = 0
4for j in range(0, 3):
5 if items[j] < pivot:
6 items[i], items[j] = items[j], items[i]
7 i += 1
8items[i], items[3] = items[3], items[i]
9print(items, i)

The mistake: thinking partitioning sorts either side. It only places the pivot — both sides still need their own recursive sort:

What does this print?
def partition(items, lo, hi):
    pivot = items[hi]
    i = lo
    for j in range(lo, hi):
        if items[j] < pivot:
            items[i], items[j] = items[j], items[i]
            i += 1
    items[i], items[hi] = items[hi], items[i]
    return i

nums = [9, 3, 7, 1, 5]
partition(nums, 0, len(nums) - 1)
print(nums)
python

The same mistake shows up as a bug when partition's last line goes missing — the recursion then trusts an index the pivot never moved to:

broken — fix it

This should print the plays sorted low→high. It prints [42, 7, 19, 3, 88, 56] — the recursion trusts an index the pivot never moved to.

your code, drawn · array

Your quick sort, partition by partition — each pivot is marked done the moment it reaches its final index.

Scene 1 of 8
Project context and further resources
You're building a piece ofTunebox — search & charts
This piece — top_k_tracks(): Finds the k most-played tracks without sorting everything.
Scenario The home screen's Top picks row shows the handful of most-played tracks from a library of thousands. Tunebox partitions the winners to the front and sorts only them.
top_k_tracks.py
Call the function you wrote — just like your app's frontend would. Edit the inputs and hit Call.
top_k_tracks(tracks, k) → listTitles of the k most-played tracks — plays high→low, ties A→Z.
try:
returned
Press Run to execute your code.
Press Check to run the tests for this lesson.
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.