B2.4 · Sorting · Current focus

Bubble sort & selection sort

Follow how each pass moves values towards their final positions, then implement and compare two sorts.

55 minutes

For this lesson

Python · step by step

Bubble sort

Watch the list change while following the actual Python execution.

Select Play or Next line to follow Python execution.

Python implementation

The highlight marks the next line to execute. Values show the state before that line.

def bubble_sort(items):    for end in range(len(items) - 1, 0, -1):        swapped = False        for j in range(end):            if items[j] > items[j + 1]:                items[j], items[j + 1] = items[j + 1], items[j]                swapped = True        if not swapped:            break    return items

The suffix beyond end is sorted. An early-exit flag gives O(n) best-case time; average and worst cases are O(n²). This in-place implementation uses O(1) auxiliary space.

Python · step by step

Selection sort

Watch the list change while following the actual Python execution.

Select Play or Next line to follow Python execution.

Python implementation

The highlight marks the next line to execute. Values show the state before that line.

def selection_sort(items):    for start in range(len(items) - 1):        smallest = start        for j in range(start + 1, len(items)):            if items[j] < items[smallest]:                smallest = j        items[start], items[smallest] = items[smallest], items[start]    return items

Find the smallest remaining item, then swap it into place. The prefix before start is sorted. Comparisons take O(n²) even for sorted input. Auxiliary space is O(1).

By the end of this lesson

  • Trace bubble and selection sort pass by pass.
  • Distinguish comparisons from swaps.
  • Test empty, sorted, reversed and duplicate-containing lists.

Work through the lesson

01 · 5 minutes

Retrieve

Explain why sorted data makes binary search possible. Predict whether sorting first is worthwhile for a single small search.

02 · 15 minutes

Trace bubble sort

Use [7, 2, 5, 1]. Compare adjacent values and swap when necessary. Record the list after each pass and explain what is now in its final position.

03 · 15 minutes

Trace selection sort

Use the same starting list. Find the smallest remaining value and swap it into the next position. Record comparisons and swaps separately.

04 · 15 minutes

Implement and test

Implement one sort without sorted() or list.sort(). Test [], [4], an already sorted list, a reversed list and repeated values.

05 · 5 minutes

Compare

Explain a difference between the two algorithms using your traces as evidence.

Before you leave

Exit question

Which work is repeated on every pass, and how does that change as the list grows?

Homework for this topic

Due 12 Oct 2026 · Questions 3–7 · Assigned

Holiday homework: finish questions 3–7

Finish the unfinished work in questions 3–7 of the B2.4 Algorithms student handout over the holiday. Questions 1 and 2 are already complete.

Due 7 Oct 2026 · Q9(a) + Q10 (bubble sort only) · Assigned

Bubble sort: Q9(a) and the bubble-sort part of Q10

Complete Q9(a) on printed page 11 and only the bubble-sort part of Q10 on printed page 12 of the B2.4 Algorithms student handout. Due 7 October 2026.