Analyzing Algorithms: Searching and Sorting
Halving is powerful: you can only halve n about log₂ n times before you're down to 1. Binary search uses that to find an item in log₂ n comparisons, and merge sort uses it to sort in n log₂ n, which is as fast as any comparison sort can go.
What the symbols mean
- Running time
Comparisons merge sort makes on n items.
- Input size
How many items are being sorted.
Explanation
Analyzing an algorithm simply means counting how many basic steps (here, comparisons) it takes, as a function of how much data it gets. It's good for picking the right tool before you write a line of code. All we're really saying in this lesson is that cutting a problem in half, over and over, gets you to the bottom shockingly fast. The formula looks intimidating, but read it piece by piece: (the work to sort items) equals (sorting two half-size lists) plus (the work to merge them back together). And just means " work, done times," once for each time you can halve the list. Now that we can compare growth rates, let's use big-O on the two jobs computers do constantly: finding things and putting things in order.
Searching. Linear search checks items one by one, so it can take up to comparisons. But if the list is sorted, you can do much better, the same way you'd look up a word in a dictionary. Binary search compares with the middle item. If the target is smaller, the whole right half can't contain it, so you throw that half away (and the left half if it's bigger). After steps at most items remain, and that hits 1 once , so about steps are enough. That's 20 comparisons for a million items.
Simple sorting. Selection sort finds the smallest item, then the next smallest, and so on, which ends up comparing every pair once: comparisons, so .
Merge sort. Here's where halving helps with sorting too. Split the list in half, sort each half recursively, then merge the two sorted halves. Merging is cheap: the smallest remaining item is always at the front of one of the two halves, so each comparison places one item, and it takes at most comparisons. That gives . To see what this solves to, picture the recursion tree. The list can be halved times, so there are levels, and on every level the pieces add up to items, so each level does work in total. That makes , which induction confirms.
No comparison sort can do better. Think about what a sort has to figure out: which of the possible orderings it was handed. Each comparison has two outcomes, so comparisons can tell apart at most cases. To cover all , some input needs at least comparisons. So merge sort is optimal up to a constant factor.
See it
Worked examples
Binary search for 37 in the sorted list .
- 1
The middle of 1–100 is 50. That's bigger than 37, so 37 must be in 1–49.
Practice
Type your answer and check it. Solve them all to complete the lesson.
What is the most comparisons binary search needs in a sorted list of 1000 items?
With and , what is ?
Selection sort compares every pair of items once. How many comparisons does it make on 100 items?
Comparison sorts need at least comparisons in the worst case. For , what is ?
A bug appeared somewhere in the last 500 commits of a project. The command git bisect tests the commit halfway through the suspect range each time. How many test runs does it need, at most, to find the first bad commit?
Solve with , and give .
The hardest one here. Get it, and you've got this lesson.
Karatsuba's method multiplies two -digit numbers with 3 multiplications of -digit numbers plus linear extra work, so . Its running time grows like . Find .
Where it's used
Every database index, search engine and spreadsheet sort relies on O(log n) searching and O(n log n) sorting.