Skip to content
Definition
An algorithm is suitable for a task when both its execution time and the memory it needs are acceptable for the amount of data and the way that data is arranged. Two algorithms that give the same result can differ greatly in both.

Time and Space Complexity of the Standard Algorithms

Key Point
AlgorithmBest-case timeAverage timeWorst-case timeSpace
Linear searchO(1)O(n)O(n)O(1)
Binary searchO(1)O(log n)O(log n)O(1)
Bubble sortO(n)O(n²)O(n²)O(1)
Insertion sortO(n)O(n²)O(n²)O(1)
Merge sortO(n log n)O(n log n)O(n log n)O(n)
Quick sortO(n log n)O(n log n)O(n²)O(log n)

Choosing an Algorithm for a Task and Data Set

SituationSuitable algorithmReason
Searching data that is not in orderLinear searchA binary search only works on sorted data
Searching a large sorted listBinary searchO(log n): about 20 comparisons for a million items
Sorting a short list, or one that is almost in orderInsertion sort or bubble sortClose to O(n) on nearly sorted data, O(1) space and simple to write
Sorting a long list when memory is plentifulMerge sortO(n log n) in every case, but needs O(n) extra memory for the sublists
Sorting a long list when memory is limitedQuick sortO(n log n) on average with little extra memory, but O(n²) if the pivots are badly chosen
Shortest path when the distance left can be estimatedA* algorithmThe heuristic means fewer nodes are visited than with Dijkstra's algorithm
Worked Example
How the Number of Steps Grows With n
Items (n)Binary search O(log n)Linear search O(n)Merge sort O(n log n)Bubble sort O(n²)
1651664256
1,024111,024about 10,000about 1 million
1,048,576211,048,576about 21 millionabout 1 million million
The figures are worst-case numbers of comparisons or steps. With 16 items the choice of algorithm makes little difference. With a million items a bubble sort needs roughly 50,000 times as many steps as a merge sort, and a linear search roughly 50,000 times as many comparisons as a binary search.
Exam Tip
When comparing algorithms, give the Big O for both time and space and link each to the data in the question: how many items there are, whether they are already in order and how much memory is available.