OCR H446, section 2.3.1 (d)
To compare two algorithms, compare their complexity (2.3.1 (c)): how time and space grow with the size of the data. The algorithm with the slower-growing complexity is better for large inputs.
Comparing growth
| Linear | Binary | Bubble | Merge | |
|---|---|---|---|---|
| 100 | 100 | 7 | 10,000 | 664 |
| 10,000 | 10,000 | 14 | 100,000,000 | 132,877 |
| 1,000,000 | 1,000,000 | 20 | about 20,000,000 |
For one million items, a bubble sort needs a trillion steps, but a merge sort needs about 20 million: 50,000 times fewer. The gap widens as grows.
Summary of the standard algorithms
Searching
| Algorithm | Best | Average | Worst | Space |
|---|---|---|---|---|
| Linear search | ||||
| Binary search (sorted array) | iterative |
Sorting
| Algorithm | Best | Average | Worst | Space |
|---|---|---|---|---|
| Bubble sort | with early exit | |||
| Insertion sort | ||||
| Merge sort | ||||
| Quick sort | stack |
Graph and tree algorithms
| Algorithm | Complexity |
|---|---|
| Depth-first and breadth-first traversal | : every node (vertex) and edge once; for a tree |
| Dijkstra's | About with a simple list, better with a priority queue |
| A* | Depends on the heuristic; in good cases it explores far fewer nodes than Dijkstra's |
Data structure operations
| Operation | Array | Linked list | Stack / queue |
|---|---|---|---|
| Access by index | |||
| Search (unsorted) | |||
| Insert/delete at the end | (or with a tail pointer) | Push/pop/enqueue/dequeue | |
| Insert/delete in the middle | (shift items) | once found, to find |
How to compare algorithms
- Identify the complexity class of each (time and space).
- Compare for the size of data expected. For tiny , the simpler one may be as good or faster.
- Consider best, average and worst cases, and whether the worst case matters (a guaranteed response time).
- Consider space as well as time.
- Consider the data: sorted or not, and how often the task is repeated.
- Consider other factors: simplicity, stability, ease of writing without bugs.
Important caveats
- Big O hides constants. An algorithm with a large constant may be slower than an one for realistic .
- Big O is about growth for large , so it may not predict the behaviour on small data.
- Insertion sort () can beat merge sort () on very short lists, because it has less overhead.
- The worst case may be rare: quick sort is in the worst case, but is often the fastest in practice.
Worked example
Task: compare bubble sort and merge sort for sorting 50,000 records, using steps estimates.
| Bubble sort | Merge sort | |
|---|---|---|
| Time complexity | ||
| Approximate steps | ||
| Space | : in place | : needs extra arrays |
| Complexity of the code | Very simple | More complicated (recursion and merging) |
- Bubble sort needs about 3,000 times more steps, so for 50,000 records, merge sort is far faster. If the machine does steps a second, the bubble sort takes about 25 seconds and the merge sort under 0.01 seconds.
- Merge sort needs extra memory for the merging. If memory were very limited, that would count against it.
- Recommendation: merge sort for large data. Bubble sort is only suitable for a very small list, where its simplicity is a benefit.
Exam tip: compare by giving both complexities and a conclusion for the situation ("for 50,000 items, merge sort's is far faster than , but it needs extra space"). Remember to compare time and space, and to say when the simpler algorithm is good enough.
Check yourself
Why does the better algorithm matter more as the data grows?
The difference in growth rates widens as n increases, so the slower-growing algorithm saves far more steps on large data.
Which is more efficient for large data, a bubble sort or a merge sort, and why?
Merge sort, because its time is O(n log n), which grows much more slowly than the bubble sort's O(n²).
Give one disadvantage of merge sort compared with bubble sort
It needs extra memory, O(n), for merging, and it's more complex to code.
Why might an O(n²) algorithm sometimes beat an O(n log n) algorithm?
For small data sets, the simpler algorithm has less overhead (constant factors), and Big O only describes behaviour as n grows large.