Skip to content

Chapter 9: Sorting Algorithms (Part 1)

Vol 2: Algorithm Forest Adventure · Station 9


Metadata Card

AttributeValue
Difficulty(Foundation)
PrerequisitesArrays, Linked Lists, Recursion
KeywordsSelection Sort Bubble Sort Insertion Sort Shell Sort Merge Sort Divide and Conquer Stable Sort

Your Progress

Master Chen taught you how to use data, but not how to organize it. Before you lies a chaotic pile of items (input). To find the smallest, largest, or median — sorting is your first weapon.


Breakthrough · Origins

Selection Sort: Find the Minimum Each Round

python
def selection_sort(arr):
    for i in range(len(arr) - 1):
        min_idx = i
        for j in range(i + 1, len(arr)):
            if arr[j] < arr[min_idx]:
                min_idx = j
        arr[i], arr[min_idx] = arr[min_idx], arr[i]
    return arr

Bubble Sort: Adjacent Comparison (As a Counterexample)

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

Insertion Sort: Like Organizing Playing Cards

python
def insertion_sort(arr):
    for i in range(1, len(arr)):
        key = arr[i]
        j = i - 1
        while j >= 0 and arr[j] > key:
            arr[j + 1] = arr[j]
            j -= 1
        arr[j + 1] = key
    return arr

Key insight: Insertion sort is O(n) on nearly-sorted data — used as the fallback in advanced sorts (Timsort).

Merge Sort: The First O(n log n) Divide and Conquer

python
def merge_sort(arr):
    if len(arr) <= 1:
        return arr
    mid = len(arr) // 2
    left = merge_sort(arr[:mid])
    right = merge_sort(arr[mid:])
    return merge(left, right)

def merge(left, right):
    result = []
    i = j = 0
    while i < len(left) and j < len(right):
        if left[i] <= right[j]:
            result.append(left[i])
            i += 1
        else:
            result.append(right[j])
            j += 1
    result.extend(left[i:])
    result.extend(right[j:])
    return result

Complexity Comparison

AlgorithmBestAverageWorstSpaceStable
SelectionO(n²)O(n²)O(n²)O(1)No
BubbleO(n)O(n²)O(n²)O(1)Yes
InsertionO(n)O(n²)O(n²)O(1)Yes
MergeO(n log n)O(n log n)O(n log n)O(n)Yes

Verification Checklist

  • [ ] Can hand-write selection, bubble, insertion, merge sort
  • [ ] Can explain why insertion sort is O(n) on nearly-sorted data
  • [ ] Can draw the recursion tree for merge sort (n=8)
  • [ ] Can explain merge sort's stability

Traveler's Notes

  • Selection: most honest O(n²), always O(n²)
  • Bubble: most intuitive, but worst performer
  • Insertion: most practical O(n²), foundation for advanced sorts
  • Merge: divide and conquer perfected, stable, O(n log n) guaranteed

Next Stop Preview

Chapter 10: Sorting Algorithms (Part 2) — "Quicksort, Heapsort, and linear-time sorting."

Built with VitePress | Software Systems Atlas