62 — Bubble sort — your first algorithm
Guide 39 let Python sort for you. Here you build the sort yourself. Bubble sort is an algorithm — a fixed recipe of steps that always ends with the list in order. It compares neighbor pairs, swaps them when they are out of order, and repeats until a pass makes no swap.
Steps
Compare the first two numbers. If the left one is larger, swap them. A swap needs a third variable:
tempholds the first value whilenumbers[0]takes the second value's place:
run it →numbers = [5, 2, 9, 1, 7, 3] if numbers[0] > numbers[1]: temp = numbers[0] numbers[0] = numbers[1] numbers[1] = temp show numbersIt prints
[2, 5, 9, 1, 7, 3]— the5and2changed places.One loop can walk the whole list, comparing each neighbor pair and swapping when needed. This is one pass, and it moves the largest value to the end:
run it →numbers = [5, 2, 9, 1, 7, 3] n = len(numbers) for j in range(0, n - 1): if numbers[j] > numbers[j + 1]: temp = numbers[j] numbers[j] = numbers[j + 1] numbers[j + 1] = temp show numbersIt prints
[2, 5, 1, 7, 3, 9]. The9bubbled to the end — that is where the name comes from.One pass is not enough: the
2and1are still out of order. An outer loop repeats the pass. After each pass the largest remaining value is already in place, so the inner loop stops one cell earlier —n - i - 1:
run it →numbers = [5, 2, 9, 1, 7, 3] n = len(numbers) for i in range(n): for j in range(0, n - i - 1): if numbers[j] > numbers[j + 1]: temp = numbers[j] numbers[j] = numbers[j + 1] numbers[j + 1] = temp show numbersIt prints
[1, 2, 3, 5, 7, 9]— sorted.Sorting a list that is already sorted should stop fast. A flag records whether a pass made any swap; if it made none, the list is done and the loop can
break:
run it →numbers = [5, 2, 9, 1, 7, 3] n = len(numbers) for i in range(n): swapped = False for j in range(0, n - 1): if numbers[j] > numbers[j + 1]: temp = numbers[j] numbers[j] = numbers[j + 1] numbers[j + 1] = temp swapped = True if not swapped: break show numbersIt prints
[1, 2, 3, 5, 7, 9]. The flag is the early-exit trick.Now the whole thing in one program. It sorts the list by hand, prints every pass, and checks the result against Python's
sorted(). Savebubble.nme:
run it →# bubble.nme — bubble sort by hand, compared with the built-in sort. # Run: nme r bubble # # Each pass compares neighbor pairs and swaps them when they are # out of order, so the largest remaining value bubbles to the end. # The flag exits early when a pass makes no swaps. numbers = [5, 2, 9, 1, 7, 3] built_in = sorted(numbers) show f"Before: {numbers}" show "" n = len(numbers) comparisons = 0 swaps = 0 for i in range(n): swapped = False for j in range(0, n - i - 1): comparisons = comparisons + 1 if numbers[j] > numbers[j + 1]: temp = numbers[j] numbers[j] = numbers[j + 1] numbers[j + 1] = temp swapped = True swaps = swaps + 1 show f"Pass {i + 1}: {numbers}" if not swapped: show " no swaps — already sorted, stopping early" break show "" show f"After my loop: {numbers}" show f"After sorted(): {built_in}" if numbers == built_in: show "Both orders agree." show f"{comparisons} comparisons and {swaps} swaps for {n} values"Run it:
nme r bubbleBefore: [5, 2, 9, 1, 7, 3] Pass 1: [2, 5, 1, 7, 3, 9] Pass 2: [2, 1, 5, 3, 7, 9] Pass 3: [1, 2, 3, 5, 7, 9] Pass 4: [1, 2, 3, 5, 7, 9] no swaps — already sorted, stopping early After my loop: [1, 2, 3, 5, 7, 9] After sorted(): [1, 2, 3, 5, 7, 9] Both orders agree. 14 comparisons and 8 swaps for 6 valuesPass 4 makes no swap, so the flag stops the sort early — the loop never ran a fifth or sixth pass.
sorted()is Python's built-in sort; matching its order is a good sanity check for a hand-written algorithm.Korean writes the same steps with
말해, Korean variable names, and a Korean list. The full Korean program is in the Korean guide.
Try it yourself
Change the list to [9, 8, 7, 6, 5] — reversed order — and rerun. Almost
every pass swaps, so the flag never stops the sort early and all five passes
run. Then try [1, 2, 3, 4, 5], already sorted: the first pass swaps
nothing, and the program stops after a single pass.
What you learned
- Bubble sort compares neighbor pairs and swaps them when they are out of order.
- A swap needs a temporary variable so no value is lost.
- An outer loop repeats the pass; each pass needs one fewer comparison (
n - i - 1). - A
swappedflag lets the loop stop early once the list is sorted. - Checking your result against
sorted()verifies the algorithm.