Programmer's Picnic • Bucket Sort

Bucket Sort

Distributes values into buckets, sorts each bucket, and then joins the buckets together.

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 + k)
Average
O(n + k)
Worst
O(n²)
Stable
Depends
In-place
No
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 bucket_sort(arr, bucket_count=5):
    if not arr:
        return []
    min_val, max_val = min(arr), max(arr)
    if min_val == max_val:
        return arr[:]

    buckets = [[] for _ in range(bucket_count)]
    interval = (max_val - min_val + 1) / bucket_count

    for num in arr:
        idx = min(bucket_count - 1, int((num - min_val) / interval))
        buckets[idx].append(num)

    result = []
    for bucket in buckets:
        result.extend(sorted(bucket))
    return result

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.