# Move over bubble & bin: Bucket sort is the fastest sort!

Don’t click away because the Bucket Sort Algorithm is possibly the best sorting method to grace the product world.

Big claim, yes I know – but we’re going to cover why.

## So what is the Bucket Sorting Method?

Starting off with the assumption you are looking to sort data in an array, bucket sort is a great starting point. If it suits your needs, don’t look any further – your search is over!

Sorting an array of items can be done in two main ways – either traversing through the list comparing each value to another to find the correct order (bubble sort), or giving each value its own area of storage and moving them into their respective place (bin sort).

Bucket Sort works using the latter method, bin sort that is. Let’s take a closer look at how it differs from bin sort before we show you why bucket sort is so good.

All right, so let’s say we have an array containing 10 integers, these are our buckets. We have a counter which acts as the number of items in each bucket – so when we start up this counter is equal to the number of elements in our array i.e. 10.

Before we begin to sort through this array, let’s fill each bucket with the same value – an integer value between 0 and 9 inclusive.

Now that all 5 buckets are full, it’s time to move on to sorting. How do we know how much capacity each bucket has? Each bucket will hold double the values currently within it i.e. 2n where n = 0…9 (10 buckets). The reason for doubling things here is because when you go to look inside one bucket to see if the value you are looking for is in there, it will only take up 1/10th of the space.

For example – bucket 5 holds 8 values (8 * 2) and the next smallest bucket which contains two values would hold 4 (4 * 2). Do you see how this is doubling all the way down?

Looking at our first value to be sorted i.e. 5… we know that going to look inside bucket number 5 will only take a tiny fraction of a second due to its low capacity. Now let’s say we want to find where 34 is within our array – well looking inside bucket #34 won’t even take 0.000000001 seconds! It’ll take forever because there’s 100000 values in there!

Alright, I think you get the point. Now let’s take a quick squizz at the Bucket Sort’s alternatives.

### Bucket Sort Algorithm History

So, when did this method begin? Bucket Sort is credited to be invented in 1955, but it was later discovered that the method had been around at least since 1945.

It started gaining traction during the 1970’s but still wasn’t widely adopted until 1995 when computer speeds really began taking off.

Nowadays bucket sort has become one of the most popular sorting algorithms out there – third after merge sort and quick sort! Although this may not seem like a big deal because both bin sort and bucket sort are generally speaking O(n * log(n)), if your data is large you’ll want to use bucket sort anyway due to its efficient manner of making comparisons.

## What is Bin and Bubble Sort?

In Bin Sort the idea is to have a separate ‘area’ for each value being sorted. Here’s an example of bin sort implementation:

This implementation probably isn’t any faster than Bucket Sort and it pretty much has the same time complexity as bucket sort, but what if we could shave off that tiny little bit extra? Well, we can!

### Bubble Sort vs Bucket Sort – How does Bubble Sort fit into this equation?

With bubble sort, all you do is compare 2 adjacent values and swap them if required (a brute-force approach). The only difference with bucket sort is how big these buckets are, wherein bubble sort they’re all equal in size. If you make them the same size as the largest bucket in a bucket sort, you get bucket sort!

## Bubble Sort vs Bucket Sort – Why is Bucket sort faster?

In a nutshell, the time complexity of bubble sort is O(n^2). The time complexity of bucket sort can be found by multiplying all the branches it’s going to take to find elements within it i.e. look inside each and every bucket until eventually we’ve either found it or haven’t gone deep enough.

If we did this for 10 buckets with 100000 items in each… well… I don’t think our system will last very long – that’s also why bin sort doesn’t work very well because not only do we have doubling down, but then we add another factor which becomes exponentially slow.

Now, what if we let bucket sort hold 10 buckets with 100000 items in each? How much time does it take for bucket sort now? O(n) (linear)!

That’s right. The same as if we were to do a linear search through an array of 100000 integers! All the branches and checks that the bucket sort algorithm is doing behind the scenes can be boiled down to ‘is this integer less than or greater than the one I’m looking for – which would also be found using a linear search.

## Bucket Sort Big O

Bucket Sort Big O notation is O(n + n * k), where n = number of elements in array and k = number of buckets.

## Bucket Sort Time Complexity

The time complexity of a bucket sort is a function of the number of buckets. A bucket sort with one bucket will take constant time, but bucket sorts with more than one bucket will have an additive running time because there are always two operations performed at each step: 1) checking whether the element has already been placed and 2) placing it in its correct location. The choice of that constant multiplicative factor is up to you – how big do you want your data structures to be? Generally speaking, optimizations such as this might not be necessary until we reach a very large number of items.

## Bucket Sort Java

To implement a bucket sort algorithm in java you’ll need to create the bucket data structure first – one that can hold at least as many elements as will be given to it. The easiest way would probably be through the use of an ArrayList with auto-expanding capabilities, but if you prefer linked lists or whatever else it’s up to you! I’m not here to tell you how to program your own sorting algorithms…

import java.util.*; class BucketSort { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here List<Integer> arr = new ArrayList<Integer>(); for (int i = 0; i < 100000; i++) { arr.add(i); } Bucket sortBucketSort = new BucketSort(); String bucketName = “”; int[] bucketArray = null; System.out.println(“Please input the number of buckets you would like to use.”); int bucketNumInput = Integer.parseInt( JOptionPane.showInputDialog(“How many buckets do you want?”)); if (bucketNumInput < 1) { JOptionPane.showMessageDialog(null, “Invalid Number”); return; } if (arr == null) { arr = new ArrayList<Integer>(); } bucketArray=new int [bucketNumInput]; for (int i=0; i<bucketNumInput ;i++){ bucketArray [i]=arr.size(); } Arrays.sort(bucketArray); for (int i=0; i<bucketArray.length-1; i++) { bucketName = Integer.toString(bucketArray [i]); System.out.println(“Working on bucket ” + bucketName); // inserts data from array to proper location int j = 0; while (j < arr.size()) { if (arr.get(j) > bucketArray [i+1]) { break; } else if (arr.get(j) <= bucketArray [i+1]) { continue; } else { arr.set(j, bucketArray [i+1]); j++; } } bucketArray [i] = arr.size(); // delete from array } System.out.println(“All done!”); for (int i=0; i<bucketArray.length-1; i++) { System.out.println(“Working on bucket ” + bucketName); System.out .print(bucketName + “, you have ” + arr.get(bucketArray [i])); if (arr.get(bucketArray [i]) != 0) { bucketName = Integer .toString(bucketArray [i]); System.out .print(“Removing ” + bucketName); arr .remove(bucketArray [i]); }

Phew! That’s a long line of code

## Bucket Sort Algorithm – Fastest Sorting Algorithm on the Block…

So, now you know what is the Bucket Sort Algorithm. It’s a really fast way to sort in particular large chunks of data and is especially useful in sorting in-memory data. Using the bucket sort algorithm you can quickly sort 100000 integers – an impossibility for all sorts of other sorting algorithms! What do you think? Will this be your go-to quick sort from now on?