# Comparing Heap and Sleep Sorting Algorithms

Published on Saturday, February 24, 2024 (5 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 **heap** and **sleep**.

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

*Heap* Sort

The heap sorting algorithm was invented by **J. W. J. Williams** in *1964*, but later that same year, **Robert W. Floyd** imporved upon this, which made it able to sort the array in-place - meaning that this version did not require any extra memory to sort a given array.

Heap-sort has multiple proven real-world uses, one of these being in the data-compression field, as well as it being used in Dijkstra’s algorithm for finding the shortest path in a given graph.

The big-o of our heap-sort is $O(n \log n)$.

*Sleep* Sort

The sleep sorting algorithm is amongst some of the newer algorithms. It was first posted on the anonymous imageboard, “forum”-like, 4chan in *2011*. This has later gotten a lot attention on **Hacker News**.

It is mostly known to be a joke algorithm, and does not have many real-life use-cases. The original implementation is simply *“sorting”* by waiting the the amount of the current number at the current number in milliseconds.

Its big-o is definitely not one of the best, being $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 **heap sort**. This goes as follows.

And of course the **sleep 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

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

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

This means we can almost run the *heap* sort algorithm almost up to 7498 times in the time it takes *sleep* 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 **heap sort**, and this shall hence on be known as ”*The Heap Hero*“!

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

## Epilogue

Does this mean you should always choose the heap 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 **heap** and **sleep** sorting algorithms.

More posts are available on the blog.