impera Open Impera

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?

  • 1

  • 250

  • 9

  • 500

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?

  • 1000

  • 1 000 000

  • 500 000

  • 20

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.

Worked example

One pass of bubble sort on [5, 1, 4, 2]

  1. Compare 5 and 1: swap → [1, 5, 4, 2].
  2. Compare 5 and 4: swap → [1, 4, 5, 2].
  3. Compare 5 and 2: swap → [1, 4, 2, 5]. The 5 is now in place.
  4. 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?

  • The last item is the largest

  • The list has an even length

  • A whole pass makes no swaps

  • The first item is the smallest

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.

Worked example

Insertion sort on [6, 3, 8, 1]

  1. Sorted part [6]. Insert 3: it moves before 6 → [3, 6, 8, 1].
  2. Insert 8: already in place → [3, 6, 8, 1].
  3. 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]?

  • [7, 5, 2, 1]

  • [1, 2, 5, 7]

  • [2, 7, 5, 1]

  • [2, 5, 7, 1]

Show the answer

[2, 5, 7, 1]

Front part sorted.

5Merge sort

Merge sort is a divide-and-conquer algorithm:

  1. Split the list in half repeatedly until each part has one item.
  2. 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?

  • It is O(n log n), not O(n²)

  • It uses no comparisons

  • It only works on small lists

  • It sorts in place

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).

AlgorithmBestAverageWorstExtra memory
linear searchO(1)O(n)O(n)none
binary searchO(1)O(log n)O(log n)none
bubble sortO(n)O(n²)O(n²)none
insertion sortO(n)O(n²)O(n²)none
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)
Check yourself

What is the worst-case complexity of quick sort?

  • O(n)

  • O(log n)

  • O(n log n)

  • O(n²)

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