OCR H446, section 2.3.1 (b)
Often several algorithms solve the same problem. The best one depends on the task and the data. The two main measures are:
- Execution time: how long it takes to run (the number of steps).
- Space: how much memory it needs.
What affects suitability?
| Factor | Question |
|---|---|
| Size of the data | A small list can use a simple method. A large one needs an efficient one |
| State of the data | Is it already sorted, nearly sorted, or random? |
| Type of data structure | An array allows quick access by index. A linked list doesn't |
| Frequency of use | If the same search is done many times, it may be worth sorting first |
| Memory available | A method needing a copy of the data may not fit |
| Does order of equal items matter? | Some sorts are stable, others aren't |
| Simplicity | A simple algorithm is less likely to contain errors |
| Worst case vs average | Is a guaranteed time needed? |
Searching
| Algorithm | Needs sorted data? | Time | Notes |
|---|---|---|---|
| Linear search | No | Up to comparisons | Simple, works on any list, even a linked list |
| Binary search | Yes | About comparisons | Much faster for large sorted arrays, needs random access |
Choosing: for a small or unsorted list, linear search is fine. For a large, sorted list, binary search is far better. If the data is unsorted and searched once, sorting first costs more than a linear search. If it is searched many times, sorting once and then using binary search wins.
Sorting
| Algorithm | Typical time | Extra space | Best suited to |
|---|---|---|---|
| Bubble sort | None (in place) | Small or nearly sorted lists (with an early-exit check), and teaching | |
| Insertion sort | None | Small or nearly sorted lists, and data arriving one item at a time | |
| Merge sort | Extra space for merging | Large data, and when a guaranteed time is needed | |
| Quick sort | average, worst | Little (in place) | Large random data, usually the fastest in practice |
Choosing a sort
- Tiny list or nearly sorted: insertion sort is simple and quick (it does very little work on sorted data).
- Large list: merge or quick sort.
- Memory is tight: quick sort (in place) over merge sort.
- Worst case matters: merge sort has no bad case ( always).
- Data already sorted: quick sort with a poor pivot (first item) degrades to . Insertion sort is .
Shortest path
| Algorithm | Notes |
|---|---|
| Dijkstra's | Finds the shortest path from a start to all other nodes. Needs non-negative weights |
| A* | Finds the shortest path from a start to one goal, using a heuristic to explore fewer nodes. Faster for a single destination |
Trade-offs: time against space
A common trade-off is using more memory to save time, or the reverse:
- Caching and memoisation (2.1.2 (c)): store results, so that they needn't be recalculated.
- Lookup tables instead of calculating.
- Hash table (fast search, more memory) against a sorted array (compact, slower to insert).
- Merge sort (extra space, guaranteed speed) against quick sort (little space, risk of a slow worst case).
Worked example
Task: choose algorithms for a school system. (1) A teacher looks up one student in a list of 30 names. (2) The school sorts 60,000 exam results each year to rank them. (3) A website has a sorted list of 5 million product codes, and is searched thousands of times per second.
- 30 names, one lookup: linear search. The list is tiny, so the difference in speed is nothing, and linear search is simple and needs no sorting.
- 60,000 results sorted once: merge sort or quick sort, at about operations. A bubble sort needs about , which is thousands of times slower.
- 5 million sorted codes, searched constantly: binary search, which needs at most 23 comparisons ( million). A linear search could need 5 million. For even faster access, a hash table could be used at the cost of more memory.
Exam tip: pick an algorithm and justify it with the data: its size, whether it's sorted, how often it's used, and the memory available. A good answer states the trade-off ("faster, but needs extra space"), not just "it is quicker".
Check yourself
What two measures are used to compare algorithms?
Execution time (how many steps it takes) and space (how much memory it needs).
When is linear search more suitable than binary search?
When the list is unsorted, or small, or is only searched once, so that sorting it would cost more than searching it.
Why might you choose insertion sort for a nearly sorted list?
It does very little work when most items are already in place, so it takes close to linear time.
Give one trade-off between time and space
Merge sort is fast with a guaranteed time but needs extra memory; quick sort uses little extra memory but can be slow in its worst case. Caching also uses memory to save time.