The Sorting Algorithm Cup: Round 1

Batu Senturk
5 min readJul 25, 2024

--

Join us for the first round of the Sorting Algorithm Cup. Today we will see Bubble Sort, Cocktail Sort, Comb Sort and Cycle Sort.

Bracket for the Competition

Ladies and gentlemen, enthusiasts and code connoisseurs, welcome to The Sorting Algorithm Cup! Today, we embark on an exciting journey to discover which sorting algorithm reigns supreme. Buckle up, because I’m going to take you on a ride through the world of algorithms, where speed and efficiency are the names of the game.

In this first round, we have four fantastic contenders ready to strut their stuff and show off their sorting skills. Let’s meet our competitors!

Side Note: If you want to see any of the code check out my GitHub

Contestant #1: Bubble Sort

First up, we have the classic, the tried-and-true, Bubble Sort! This algorithm is like the grandparent of sorting algorithms, simple and straightforward. Bubble Sort works by repeatedly stepping through the list, comparing adjacent pairs of elements, and swapping them if the first one is greater. While it is easy to understand and implement, Bubble Sort is certainly not the most efficient, especially for large datasets.

  • Time Complexity: O(n²)

How it Works:

  1. Start at the beginning of the list.
  2. Compare the first two elements.
  3. If the first elements is greater than the second, swap them.
  4. Move to the next pair of elements and repeat step 3.
  5. Continue this process until the end of the list.
  6. Repeat steps 1–5 until the list is sorted.

Watch it in Action:

Bubble Sort sorting a list of 100 numbers from 0–99

Contestant #2: Cocktail Shaker Sort

Next, shaking things up with a bit more flair, is Cocktail Sort! This algorithm is like Bubble Sort’s cooler, more efficient cousin. Instead of just bubbling up in one direction, Cocktail Sort goes back and forth, sorting elements in both directions. This bidirectional approach helps to sorts the list a bit faster than Bubble Sort by tackling both ends of the list simultaneously.

  • Time Complexity: O(n²)

How it Works:

  1. Start at the beginning of the list.
  2. Move towards the end of the list, swapping adjacent elements if they are in the wrong order.
  3. When you reach the end, reverse direction and move toward the beginning, again swapping elements as needed.
  4. Continue this back-and-forth process until the list is sorted.

Watch it in Action:

Cocktail Sort sorting a list of 100 numbers from 0–99

Contestant #3: Comb Sort

Our third contender is the quirky and quick Comb Sort. This algorithm also improves on Bubble Sort. It starts by comparing elements far apart and gradually narrows down the gap, by the end turning into Bubble Sort. By starting with a large gap and shrinking it in each iteration, Comb Sort quickly reduces disorder, making it much more efficient than Bubble Sort.

  • Time Complexity: O(n²)

How it Works:

  1. Start with a large gap between elements.
  2. Compare elements that are the gap distance apart and swap them if they are in the wrong order.
  3. Reduce the gap and repeat step 2.
  4. Continue this process until the gap is 1, at which point it behaves like a final pass of Bubble Sort.

Watch it in Action:

Comb Sort sorting a list of 100 numbers from 0–99

Contestant #4: Cycle Sort

Last but definitely not least, we have the strategic Cycle Sort. This algorithm is all about minimalising the number of writes to an array, making it very memory efficient. Cycle Sort works by determining the correct position for each element and placing it there with as few writes as possible. This makes it very efficient in terms of write operations, which can be useful for scenarios where write operations are costly.

  • Time Complexity: O(n²)

How it Works:

  1. For each element in the list, determine the position where it should go.
  2. If the element is not already in its correct position, move it to its correct position.
  3. Repeat this process for all elements in the list

Watch it in Action:

Cycle Sort sorting a list of 100 numbers from 0–99

And They’re Off!

I’ve prepared an 1000 item array, shuffled and waiting to be sorted. Our contestants have raced against each other to sort this array and here are the results:

Drumroll, please

Here’s how each algorithm performed based on 5 repeats:

time taken for each algorithm to sort the same list in seconds

Where the range represents the consistency of the algorithm by showing the difference between the highest and lowest times, the smaller it is, the more consistent.

And the winner of this round is Comb Sort! With an impressive average time of 0.002545 seconds, Comb Sort takes the crown for the first race. Congratulations! This algorithm will move on to the grand finale to compete against the winners of our upcoming races.

Stay Tuned

Comb Sort might have won this round but there are many giants in the following rounds, like my favourite Radix Sort. Best of luck to our champion and stay tuned for the next round where four more algorithms will battle it out for a spot in the grand finale. Don’t forget to share your thoughts and predictions in the comments below. Until next time…

If you want the code for any of these projects, I have it in my GitHub:

Feel free to ask any questions. Let’s keep this conversation going!

Hi, I’m Batu. I write about Computer Science topics and explain different algorithms to give you potential project ideas. For my stories to pop up on your feed, I’d love for you to follow me (Batu Senturk)

--

--