fayotech.academy⚖️ Algorithms: Better Recipes
Order & cost

🃏 Sorting: paying for speed

Why: Sorting: how chaos becomes order, one swap at a time.

Unlocks: The language of cost — comparing algorithms like a professional.

Binary search demanded a sorted list — so who does the sorting? Sorting is the toll you pay once so that every later search is nearly free. The simplest recipe is bubble sort: walk the list, compare neighbors, swap them when they're backwards. Repeat until a full pass needs no swaps. Big values "bubble up" to the end like air in water.

Quick challenge

Why does bubble sort re-scan the list again and again instead of finishing in one pass?

That n × n is the sting: sorting a million items with bubble sort ≈ a trillion comparisons. Real libraries use cleverer recipes (Python's built-in sorted() runs one called Timsort) that finish in n × log(n) — a million items in ~20 million steps, not a trillion.

Quick challenge

Your app searches a million-entry list thousands of times per day. The winning strategy is…