Skip to main content
← All posts

Sleep Sort: Mechanism, Complexity Analysis & Interactive Visualizer

Md. Harun-Or-RoshidNov 5, 20268 min readArticle
Sleep Sort: Mechanism, Complexity Analysis & Interactive Visualizer
Rare & Educational · Article 62 of 62

Sleep Sort

A unique concurrency-based sorting algorithm: spawns a separate thread for each element, sleeping for a duration proportional to the element value before writing it to the output.

How it works

Sleep Sort was published anonymously on the 4chan imageboard in 2011. It quickly went viral in developer communities because of its hilarious design that abuses thread sleeping mechanisms to sort without performing a single element-to-element comparison:

  1. **Thread Spawning:** For each element \(x\) in the input array, spawn a concurrent background thread (or set an asynchronous timeout/timer).
  2. **Proportional Sleeping:** In each thread, put the execution to sleep for a duration proportional to the value of \(x\) (e.g. \(sleep(x)\) or \(sleep(x \cdot 10)\) milliseconds).
  3. **Wakeup Write:** When a thread wakes up, it immediately appends the value of \(x\) to a global output list.
  4. **Sorted Output:** Because smaller values wake up sooner than larger values, the elements write themselves to the output list in fully sorted order!

Complexity

Best time
O(max(val) + n)
Average time
O(max(val) + n)
Worst time
O(max(val) + n)
Extra space
O(n) (threads/stack)
Stable
No
In-place
No
The time complexity is **\(O(\max(A) + n)\)** where \(\max(A)\) is the maximum value in the array. This is because the algorithm must wait for the longest sleep duration to finish. If the array contains a value like \(1,000,000\), the sort will take a long time to complete!

Interactive visualization

Pseudocode

procedure sleepSort(A):
  outputList ← empty list
  
  parallel for each val in A:
    sleep(val)
    outputList.append(val)

JavaScript implementation

// Asynchronous JavaScript Sleep Sort
async function sleepSort(arr) {
  const sorted = [];
  
  const sleepPromise = (val) => {
    return new Promise(resolve => {
      setTimeout(() => {
        sorted.push(val);
        resolve();
      }, val * 10); // sleep proportional to val
    });
  };
  
  await Promise.all(arr.map(val => sleepPromise(val)));
  
  for (let i = 0; i < arr.length; i++) arr[i] = sorted[i];
  return arr;
}

Pros and cons

Pros

  • Extremely creative and highly humorous.
  • Performs zero element-to-element comparisons.
  • Naturally parallel.

Cons

  • Time complexity depends directly on element values.
  • Can fail (race condition) if elements sleep for too short or if system CPU scheduler experiences jitter.
  • Unstable sorting order.
  • Extremely resource-heavy (spawning threads).

When (not) to use it

  • Humorous presentations: Hilarious to demo, showing how concurrency can be abused to perform logical tasks.
  • Real systems: Never! CPU execution jitter will cause elements to write out-of-order, failing correctness.
You have reached the end of the sorting algorithm articles roadmap! Congratulations on completing all 62 sorting visualizations.
Share:
Share on X
Share on LinkedIn
Share on Facebook
Share on Reddit
Share on WhatsApp
Share on Telegram
Copy link

Related reading