Programmer's Picnic • Bubble Sort

Bubble Sort

Repeatedly compares adjacent boxes and swaps them when they are out of order. Large values bubble toward the end.

Back to home

Visualizer

Numbered boxes show both the value and its current position number. Movement is animated from one slot to another. For divide-and-conquer algorithms, the page also shows how the list splits into parts and then comes back together.

compare swap / move fixed / merged

Dry run

Divide-and-conquer view

This area becomes especially useful for Merge Sort, Quick Sort, and TimSort-style run/merge explanation. It visually shows parts, subparts, pivot-based partitions, or natural runs depending on the algorithm.

Complexity and notes

Best
O(n)
Average
O(n²)
Worst
O(n²)
Stable
Yes
In-place
Yes
Type
Core / Counting / Heap

Tip: try arrays with duplicates and partially sorted values. Watching the movements helps understand why some algorithms preserve order and some do not.

Python code

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

Embedded Python editor via share-hash

The editor URL below has been updated to remove lesson-viewer. The code is packed into the hash so the editor can load a ready example directly.