Why Some Algorithms Win: Big-O for Search and Sort
Byte, a friendly luminous robot, stands at a glowing whiteboard tracing two climbing curves while sorted number cards float in midair beside a ticking stopwatch
- Trace how linear search and binary search locate a target value in a list.
- Explain why binary search requires the input list to be sorted to stay correct.
- Classify linear search, binary search, and common sorts by their Big-O worst-case time complexity.
- Compare two algorithms and justify which scales better as input size grows.
- Distinguish correctness (does it give the right answer) from efficiency (how fast it runs).
Key terms
- Linear search
- Checking items one by one from the start, running in O(n) time.
- Binary search
- Halving a sorted list each step, running in O(log n) time.
- Correctness
- Whether an algorithm always produces the right answer for valid input.
- Efficiency
- How much work an algorithm does, often measured by Big-O time complexity.
- Merge sort
- A divide-and-conquer sort running in O(n log n) worst-case time.
Correctness Versus Efficiency
Two independent questions judge any algorithm: is it correct, meaning it always gives the right answer, and is it efficient, meaning it does so with little work? These are not the same, and confusing them leads to subtle bugs. Binary search is highly efficient at O(log n) but only correct when the input is sorted; on unsorted data it stays fast yet returns wrong answers, so it is unusable there. Linear search is slower at O(n) but correct on any list. A good algorithm must satisfy both properties for the data it will actually face.
Comparing Sort Mechanisms
Sorting itself costs time, and the mechanism determines the cost. Bubble sort makes repeated passes swapping adjacent out-of-order pairs until none remain, giving O(n²) in the worst case. Insertion sort instead takes each element and walks it backward into the already-sorted prefix, shifting larger items aside, also O(n²) but by a different mechanism that is fast on nearly-sorted data. Merge sort uses divide-and-conquer, splitting the list, sorting halves, and merging them in O(n log n), which scales far better. Choosing a sort means weighing input size, existing order, and worst-case guarantees.
Worked examples
Estimate worst-case comparisons for binary search on a sorted list of one million items.
- Binary search is O(log n), so count how many halvings reduce n to 1.
- Each step halves the remaining range, so the count is log base 2 of 1,000,000.
- 2^20 = 1,048,576, which just exceeds one million.
- Therefore about 20 comparisons suffice in the worst case.
Answer: About 20 comparisons, versus up to one million for linear search.
Activity
Order these Big-O complexities from slowest-growing (most efficient) to fastest-growing (least efficient) as n gets large
Practice
Order O(1), O(n), O(log n), O(n^2), and O(n log n) from most to least efficient.
Explain why binary search can wrongly report not found on an unsorted list.
Common mistakes to avoid
- Binary search works on unsorted listsIt needs sorted data; on unsorted lists it may discard the half holding the target.
- Simpler code means faster algorithmCode simplicity does not set time complexity; a simple algorithm can still scale poorly.
Check your understanding
A list of 1,000,000 items is already sorted. About how many comparisons does binary search need in the worst case?
Why can binary search give a WRONG answer on an unsorted list?
Which sorting algorithm scales BETTER for a very large list?
Linear search needs about 100 checks in the worst case on a list of 100 items. About how many worst-case checks on 200 items?
Recap
Linear search is O(n) and works on any list, while binary search is O(log n) but requires sorted input. Correctness and efficiency are separate properties, and sorts range from O(n²) bubble and insertion sort to O(n log n) merge sort.
Reflect
When would you accept a slower O(n) algorithm to avoid the cost of first sorting the data?