Retrieve
Explain why sorted data makes binary search possible. Predict whether sorting first is worthwhile for a single small search.
Follow how each pass moves values towards their final positions, then implement and compare two sorts.
Watch the list change while following the actual Python execution.
1def bubble_sort(items):2 for end in range(len(items) - 1, 0, -1):3 swapped = False4 for j in range(end):5 if items[j] > items[j + 1]:6 items[j], items[j + 1] = items[j + 1], items[j]7 swapped = True8 if not swapped:9 break10 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.
Watch the list change while following the actual Python execution.
1def selection_sort(items):2 for start in range(len(items) - 1):3 smallest = start4 for j in range(start + 1, len(items)):5 if items[j] < items[smallest]:6 smallest = j7 items[start], items[smallest] = items[smallest], items[start]8 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).
Explain why sorted data makes binary search possible. Predict whether sorting first is worthwhile for a single small search.
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.
Use the same starting list. Find the smallest remaining value and swap it into the next position. Record comparisons and swaps separately.
Implement one sort without sorted() or list.sort(). Test [], [4], an already sorted list, a reversed list and repeated values.
Explain a difference between the two algorithms using your traces as evidence.
Which work is repeated on every pass, and how does that change as the list grows?
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.
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.