Some algorithms are easier to understand when you can see the objects move. AlgoMaster’s supplied reel describes an eight-element bubble-sort animation. Its character theme is part of the source video; the explanation and illustrations here use our own examples.

Bubble sort repeatedly checks neighbours and swaps them when they are out of order. The interesting part is how those small local decisions eventually create a globally sorted list.

Watch the source reel

Source reel: AlgoMaster.io (@algomasterio), dated 2026-09-03 in Instagram’s accessible post description. Creator and caption were checked on 8 October 2026. This article is an independently researched explanation of the topic, not a transcript or a claim to ownership of the video.

The embedded reel remains hosted by Instagram. Playback may depend on sign-in, browser settings and the creator’s permissions. Use the original-post link if it is unavailable.

One pass moves the largest remaining value right

The NIST definition describes repeated adjacent comparisons and swaps. In the ascending, left-to-right version below, the largest value in the active range reaches its right-hand end after a complete pass. The next pass can leave that finished position alone.

Swapping only when the left value is strictly greater preserves the order of equal values: this version is stable. It works in place using constant extra working storage. These properties depend on the implementation, not on the animation’s visual theme.

Trace an original four-number example

Start with [5, 1, 4, 2]. Compare 5 and 1: swap to get [1, 5, 4, 2]. Compare 5 and 4: swap to get [1, 4, 5, 2]. Compare 5 and 2: swap to get [1, 4, 2, 5].

The 5 is now finished. On the next pass, 1 and 4 stay in place; 4 and 2 swap. The list becomes [1, 2, 4, 5]. A final pass over the remaining active pair makes no swap, so the algorithm stops.

Try writing each pair on paper before looking ahead. The algorithm does not jump directly to the smallest item or compare every value with a chosen pivot. It asks the same local question repeatedly.

Run the Python version

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

print(bubble_sort([5, 1, 4, 2]))
# [1, 2, 4, 5]

The input list is changed. If you need to keep it, pass a copy with values[:]. The swapped flag is reset for each pass. A pass with no swaps means every adjacent pair in the active range is already ordered, while the suffix was completed earlier.

For empty and single-item lists, the outer loop has no work. Repeated values remain present; sorting is not deduplication. The examples were checked against Python’s sorted result on empty, repeated, ordered, reversed and randomly generated integer inputs.

Tall blocks gather at the right end while a student examines the remaining row.
AI-generated editorial illustration. Conceptual, not evidence of a real event or a measured result.

Count comparisons, not only visible movement

Without an early stop, the shrinking loops make (n-1)+(n-2)+...+1 = n(n-1)/2 comparisons. For eight values that is 28; for 10,000 values it is 49,995,000. These are calculated counts for this loop structure, not a timing benchmark.

The worst-case growth is quadratic. An already sorted list with the early-exit flag needs one pass, giving linear best-case work. Few visible swaps do not automatically mean few comparisons. Animation speed is also a presentation choice, not a reliable performance comparison.

Learn the mechanism; use the standard tools for ordinary work

Bubble sort is a useful exercise in loop boundaries, mutation and correctness. For normal Python application code, the official sorting guide explains sorted(values) for a new list and values.sort() for modifying the existing list. These built-ins are not bubble sort.

When reviewing your own version, check that the right neighbour stays within the list, that the active range gets smaller and that equal values are not unnecessarily swapped. These details explain the result more clearly than memorising a block of code.

The lesson is how a pass establishes a finished position. Once that is clear, compare it with quicksort’s partitioning approach: both arrange data, but the work they perform is very different.