BeginnerCoder

Lesson 23 — Quicksort

Quicksort: pick a pivot, split, repeat

Quicksort is a divide and conquer sort: pick a pivot value, split the list into items smaller than it, equal to it, and bigger than it, then sort the two outer parts the same way (recursion). Lists of length 0 or 1 are already sorted — the base case.

small = [n for n in nums if n < pivot]

On average it runs in O(n log n) — far faster than bubble sort on big data.

Why it matters: variants of quicksort sit inside many standard libraries' sort functions.

Want it another way?
Task

Fill in left, mid and right with list comprehensions. Expected: [1, 2, 5, 5, 6, 9].

Run your code — when the output matches the task, you will see a green check. Then mark the lesson complete.

← Back
Next →
Test your knowledge

A quick 3-question quiz on this lesson — instant feedback.

Enjoying this lesson? Share it.

Keep going with Algorithms Foundations

Ready for more? Optional paid resources that pick up where this lesson leaves off.

Some links on this page are affiliate links: we may earn a commission at no extra cost to you. As an Amazon Associate I earn from qualifying purchases.