Developer Blog

Most sorting algorithms, or at least their names, are widely known – quicksort, mergesort, etc. Some companies even like to discuss their nuances at their job interviews.

However, there’s a sorting algorithm that most developers haven’t heard of, meet sleepsort.

Unlike many other sorting algorithms it’s very straightforward: given a list of numbers, it waits for number of seconds depending on the arguments and prints the numbers in sorted order.

---
#!/usr/bin/env bash
for var in "$@" ; do
(sleep ${var} ; echo ${var}) &
done
wait
---

The output is:

---
$ bash sleepsort.sh 5 1 7
1
5
7
---

This algorithm can be used where others are not applicable: to cheer up your colleagues and earn some bonus points at an interview (it’s always good to know more sort algorithms than your interviewer does).

Bonus: Guess how the ‘randomsort’ algorithm works?

Leave a comment

Your email address will not be published. Required fields are marked *