Bucket Sort Algorithm: A Step-by-Step Visualization

In this video, I will show you how to use the bucket sort algorithm to sort an array of numbers from least to greatest, a common task in university level computer science courses. I will show you step by step visualization. This step by step guide explains how numbers are placed into buckets using formulas and then sorted within each bucket. It's a great tutorial for learning this sorting method. I will also go over the code in python. Bucket sort is also called Bin Sort. Watch Insertion Sort:    • Insertion Sort in 2 min (Python)   When learning about sorting algorithms in computer science, bucket sort often comes up as a fascinating example of how dividing data into groups can simplify the process of ordering numbers. In this video, I take you step by step through bucket sort, explaining not just the mechanics of how it works but also why it is considered one of the classic algorithms in computer science education. By the end of this tutorial, you will not only understand bucket sort, but you will also see why this algorithm is worth knowing for interviews, academic assignments, and practical programming challenges. At its core, bucket sort is an algorithm that works by distributing the elements of an array into several smaller groups called “buckets.” Once the numbers are placed into their appropriate buckets, each bucket is sorted individually—commonly with another simple algorithm like insertion sort. Finally, the contents of the buckets are concatenated to form the sorted output. This three-step approach—distribute, sort, and combine—is what makes bucket sort conceptually simple yet powerful when applied to the right kind of data. In this tutorial, you’ll see me demonstrate bucket sort on an example array of numbers. The process starts with creating a number of empty buckets equal to the size of the input array. Each number is normalized against the maximum value to determine which bucket it belongs to. This normalization step is critical because it ensures that every number falls into the correct bucket. Once the distribution is complete, you’ll see how insertion sort is used to order the numbers inside each bucket, and finally, how we combine everything into the final sorted array. Throughout this video, I will emphasize how stability and distribution affect the performance of bucket sort. Stability in sorting means that equal elements retain their relative order after sorting. Since I use insertion sort within each bucket, this implementation of bucket sort is stable. That’s important when dealing with datasets that have secondary sorting keys, where the order of equal elements matters. Another key point you will learn in this tutorial is that bucket sort works best with data that is evenly spread out over a range. For example, if you’re sorting decimals in the range [0,1], bucket sort is extremely efficient. But in my implementation, I generalize it to work with any non-negative numbers by normalizing with the maximum value. This allows bucket sort to handle arrays that include integers, floating-point numbers, and other non-negative values. For those preparing for coding interviews, understanding bucket sort gives you an edge. While you may not always be asked to code bucket sort directly, knowing how it works helps you understand concepts like distribution, normalization, stability, and how auxiliary algorithms like insertion sort can be combined with larger sorting strategies. In fact, bucket sort is closely related to radix sort, which also depends on stable intermediate sorting steps, often using counting sort. So by mastering bucket sort, you’re also building a foundation for understanding radix sort and other non-comparison-based sorting algorithms. In this video, I’ll also highlight the contrast between bucket sort and other popular sorting algorithms. While quicksort and mergesort rely on comparisons and recursive partitioning, bucket sort avoids most comparisons by grouping elements according to a formula. This difference in approach makes bucket sort an ideal example when teaching the variety of strategies used in algorithm design.