# Comparing Bubble and Bucket Sorting Algorithms

Published on Friday, August 11, 2023 (9 months ago)

Wether you’re a beginner or a greatly experienced developer, this comparison will come in handy to find the fastest of two sorting algorithms. Namely **bubble** and **bucket**.

Instead of a wall-of-text, we’ll get into the nitty-gritty right away!

*Bubble* Sort

The best fact about the bubble sorting algorithm is, arguably, the speculation on when it was invented. It was first described in *1955* (published *1956*) by **Edward Harry Friend**, he called this a *sorting exchange algorithm*. This went unnoticed for years, until **Kenneth E. Iverson** found it in *1962* and coined the name `Bubble`

sort.

The worst-case performance of this is $O(n^2)$.

*Bucket* Sort

The bucket sort, also known as `bin sort`

.

It uses the **Scatter-Gather** approach to sort the array. The merge sort uses this to divide (*scatter*) the provided array into separate groups - known as *buckets*. Then sorts each individual bucket, and finally concatenate, or *gather*, them together to its final form.

The worst-case performance of the bucket sort is $O(n^2)$.

## Comparison

We will start, with an array of `3500`

random (*hopefully* distinct) floating-point values, to test our two sorting algorithms.

Now that we have some data to test on, we want to add the algorithm for the **bubble sort**. This goes as follows.

And of course the **bucket sort** as well, otherwise we won’t have anything to compare against.

Now, let’s test the two against one another.

If you want to read about something else here is a collection of some different sorting algorithms. You can also navigate directly to the implementation of

bubble sortVS.bucket sort, this is espescially useful if you want to manipulate each of these algorithms yourself.

Running this benchmark against the **bubble** sort and **bucket** sort, we will notice that one is **16.35x** faster than the other, which is … drum roll, please! The great **bucket sorting algorithm**!

This means we can almost run the *bucket* sort algorithm almost up to 17 times in the time it takes *bubble* sort to run once!

## Artificial Intelligence

We asked a well-known *Artificial Intelligence* (*A.I.* for short, *AI* for the lazy and *ai* for the lazier) to nickname our winner, the **bucket sort**, and this shall hence on be known as ”*The Bucket Wrangler*“!

To be fair, of course, we also asked for one for our lesser speedy algorithm, the **bubble sort**, which is now known as ”*The Bubble Buster*“.

## Epilogue

Does this mean you should always choose the bucket sorting algorithm when you implement a new app? Well, not exactly… Please do remember that, as with everything in life, there are pros and cons as well, so choose wisely!

There are a lot of other sorting algorithms out there. Yet more to be discovered! Who knows, you might find the next one with an immersive speed increase or magnificent memory benefits!

Thanks for reading, and I truly hope this will better your understanding of the speed of **bubble** and **bucket** sorting algorithms.

More posts are available on the blog.