Searching, sorting and algorithm efficiency
A-Level Computer Science · Algorithms and Data Structures
Linear search
Check each element in turn. Works on unsorted data, needs no preparation, but on average examines half the list and in the worst case all of it — $O(n)$.
Binary search
Requires a sorted list. Compare the middle element; if the target is larger discard the lower half, if smaller discard the upper half, and repeat. Each comparison halves the remaining data, so the worst case is $O(\log_2 n)$: $1\,000\,000$ items need about $20$ comparisons. The precondition is the whole point — a binary search on unsorted data returns wrong answers, not slow ones.
Bubble sort
Repeatedly pass through the list swapping adjacent elements that are out of order; after each pass the largest remaining value has 'bubbled' to the end. A flag that records whether any swap occurred lets it stop early on nearly sorted data. Simple to code, but $O(n^2)$.
Insertion sort
Take each element and insert it into its correct place among those already sorted. Also $O(n^2)$, but usually faster than bubble sort in practice and very fast on nearly sorted data.
Talking about efficiency properly
Big-O describes how run time grows with input size, not the actual time. Doubling $n$ roughly doubles the work of an $O(n)$ algorithm and quadruples that of an $O(n^2)$ one — that difference is invisible at $n=10$ and decisive at $n=100\,000$. Also consider memory: an algorithm that is faster but needs a second copy of the data may not fit.
Choosing
Sort once and search many times, and the cost of sorting is repaid by binary searches. Search a small list once, and a linear search on unsorted data is the right answer.
Worked example: tracing a bubble sort and counting comparisons
Sort the sequence $[7,\ 3,\ 9,\ 2,\ 5]$ into ascending order using bubble sort. Record the sequence after each pass and count the total number of comparisons if an early-stop flag is NOT used.
Solution
Pass 1 ($4$ pairs compared):
$(7,3)$ swap ⇒ $[3,7,9,2,5]$
$(7,9)$ keep ⇒ $[3,7,9,2,5]$
$(9,2)$ swap ⇒ $[3,7,2,9,5]$
$(9,5)$ swap ⇒ $[3,7,2,5,9]$
End of pass 1: $[3,7,2,5,\mathbf{9}]$: the largest value is now in the right place.
Pass 2 ($3$ pairs compared):
$(3,7)$ keep; $(7,2)$ swap ⇒ $[3,2,7,5,9]$; $(7,5)$ swap ⇒ $[3,2,5,7,9]$
End of pass 2: $[3,2,5,\mathbf{7},9]$
Pass 3 ($2$ pairs compared):
$(3,2)$ swap ⇒ $[2,3,5,7,9]$; $(3,5)$ keep
End of pass 3: $[2,3,\mathbf{5},7,9]$
Pass 4 ($1$ pair compared): $(2,3)$ keep ⇒ $[2,3,5,7,9]$
Result: $[2,3,5,7,9]$.
Total comparisons $=4+3+2+1=\mathbf{10}$, exactly equal to $\dfrac{n(n-1)}{2}=\dfrac{5\times4}{2}=10$.
With an early-stop flag: pass 4 has no swaps, but the algorithm has to run through all of pass 4 to find that out. For this sequence, the flag saves pass 5 onward (there is none), so it saves nothing. The flag is only valuable when the data is already nearly sorted, for example the sequence $[1,2,3,5,4]$ needs only two passes instead of four.
Why this is $O(n^2)$: the formula $\dfrac{n(n-1)}{2}$ is dominated by its quadratic term, so doubling $n$ increases the number of comparisons about four times.