A levels Computer Science · Algorithms
Searching and sorting
1Linear search
A linear search checks each item in turn from the start until it finds the target or reaches the end.
def linear_search(items, target):
for i in range(len(items)):
if items[i] == target:
return i
return -1 # not found
- Works on unsorted data.
- In the worst case it checks every item: O(n).
- Simple to write, and fine for small lists.
Check yourself
What is the worst-case number of comparisons for a linear search of 500 items?
Show the answer
500
Every item.
2Binary search
A binary search works only on sorted data. It checks the middle item, then throws away the half that cannot contain the target.
def binary_search(items, target):
low, high = 0, len(items) - 1
while low <= high:
mid = (low + high) // 2
if items[mid] == target:
return mid
elif items[mid] < target:
low = mid + 1
else:
high = mid - 1
return -1
Binary search for 34. Blue items are still in range; the amber item is the middle one checked. Only 4 comparisons are needed.
Each comparison halves the search space, so it is O(log n). A list of 1000 items needs at most 10 comparisons, because 2¹⁰ = 1024.
Check yourself
What is the maximum number of comparisons for a binary search of 1 000 000 sorted items?
Show the answer
20
2²⁰ ≈ 1 000 000.
3Bubble sort
Bubble sort passes through the list, comparing each adjacent pair and swapping them if they are in the wrong order. After each pass, the largest remaining item has "bubbled" to the end.
One pass of bubble sort on [5, 1, 4, 2]
- Compare 5 and 1: swap → [1, 5, 4, 2].
- Compare 5 and 4: swap → [1, 4, 5, 2].
- Compare 5 and 2: swap → [1, 4, 2, 5]. The 5 is now in place.
- Pass 2 gives [1, 2, 4, 5]. Pass 3 makes no swaps, so the list is sorted and the algorithm stops.
Bubble sort is O(n²): easy to write, but very slow for large lists. Stopping early when a pass makes no swaps makes it fast for lists that are already nearly sorted.
Check yourself
What is the main sign that bubble sort can stop early?
Show the answer
A whole pass makes no swaps
Already sorted.
4Insertion sort
Insertion sort builds a sorted part at the front. Each new item is taken and shifted left until it is in the right place.
Insertion sort on [6, 3, 8, 1]
- Sorted part [6]. Insert 3: it moves before 6 → [3, 6, 8, 1].
- Insert 8: already in place → [3, 6, 8, 1].
- Insert 1: shifts past 8, 6 and 3 → [1, 3, 6, 8].
It is also O(n²) in the worst case, but is quick for small or nearly sorted lists, and sorts in place (no extra memory).
Check yourself
After two insertions, what is [7, 2, 5, 1]?
Show the answer
[2, 5, 7, 1]
Front part sorted.
5Merge sort
Merge sort is a divide-and-conquer algorithm:
- Split the list in half repeatedly until each part has one item.
- Merge pairs of parts back together in order, until one sorted list remains.
Merge sort on 8 items: three levels of splitting (log₂ 8 = 3), then three levels of merging pairs of sorted lists.
Merging two sorted lists compares their front items and takes the smaller each time. There are log₂ n levels of splitting, and each level does about n work, so merge sort is O(n log n). It needs extra memory for the merged lists.
Check yourself
Why is merge sort faster than bubble sort for large lists?
Show the answer
It is O(n log n), not O(n²)
Divide and conquer.
6Quick sort and comparing algorithms
Quick sort picks a pivot, moves smaller items before it and larger items after it, then sorts each side recursively. It is O(n log n) on average but O(n²) in the worst case (for example, if the pivot is always the smallest item).
| Algorithm | Best | Average | Worst | Extra memory |
|---|---|---|---|---|
| linear search | O(1) | O(n) | O(n) | none |
| binary search | O(1) | O(log n) | O(log n) | none |
| bubble sort | O(n) | O(n²) | O(n²) | none |
| insertion sort | O(n) | O(n²) | O(n²) | none |
| merge sort | O(n log n) | O(n log n) | O(n log n) | O(n) |
| quick sort | O(n log n) | O(n log n) | O(n²) | O(log n) |
Check yourself
What is the worst-case complexity of quick sort?
Show the answer
O(n²)
Bad pivots.
Now test yourself
This lesson has a 8-question quiz and 5 exam-style questions with mark schemes in Impera. Free, with your progress saved.
Practise in Impera