Skip to content
DevMeme
1757 of 7590
CS Fundamentals Post #1963 · source on Telegram

Sleep Sort: The Genius of Inefficient Sorting

Description

A screenshot of a forum post, likely from an imageboard like 4chan, titled 'Genius sorting algorithm: Sleep sort'. The post, dated 2011-01-20, proudly presents a 'newly invented' sorting algorithm. The core of the post is a bash script. The script defines a function that takes a number as an argument, sleeps for that number of seconds, and then echoes the number. The main part of the script iterates through the command-line arguments, launching a background process of this function for each number. A 'wait' command ensures the main script doesn't exit until all background jobs are done. An example usage is shown: './sleepsort.bash 5 3 6 3 1 4 7'. The post concludes with a simple explanation: for each number, a new program sleeps for that duration and then prints it. The technical joke is that this is a famously impractical and inefficient 'sorting' algorithm. While it technically works for non-negative integers by leveraging the operating system's process scheduler, its runtime is determined by the largest number in the input array, it consumes a process for each element, and it's highly non-deterministic. For senior developers, it's a classic piece of computer science humor, a parody of overly clever but useless solutions

Comments

7
Anonymous ★ Top Pick Sleep Sort is the only algorithm whose performance is measured in 'time to get a coffee'. It's also a great way to stress-test your OS scheduler and your patience simultaneously
  1. Anonymous ★ Top Pick

    Sleep Sort is the only algorithm whose performance is measured in 'time to get a coffee'. It's also a great way to stress-test your OS scheduler and your patience simultaneously

  2. Anonymous

    Sleep sort: proof that with linear syntax, unbounded forks, and latency proportional to coffee breaks, you can trade O(n log n) for O(ulimit -n) Sev-1s and still call it “elegant concurrency.”

  3. Anonymous

    Sleep Sort: the only algorithm where your time complexity is O(max(n)) wall clock time, and explaining it in a technical interview guarantees you'll never have to explain why you left your last job

  4. Anonymous

    Ah yes, Sleep Sort - the algorithm with O(n) time complexity and O(max(input) seconds) wall-clock time. Perfect for when you need your array sorted by next Tuesday. It's technically correct (the best kind of correct), leveraging the OS scheduler as a comparison-free sorting mechanism. Just don't try explaining to your PM why the production deployment is sleeping on the job, or why your sort performance degrades linearly with input magnitude. Bonus points: it fails spectacularly with floating-point numbers and negative integers, making it the perfect interview answer for 'tell me about a time you thought outside the box... way, way outside.'

  5. Anonymous

    Sleepsort: O(n * MAX) time where 'parallelism' means your process table explodes before results arrive - prod-ready until scale hits

  6. Anonymous

    Sleep sort: delegating compare() to the kernel timer - O(n) processes, O(max(input)) wall clock, and an O(PagerDuty) alert when someone tries 86400

  7. Anonymous

    Sleep sort: trade O(n log n) for O(max(A)) and a production incident - outsourcing comparisons to the kernel scheduler and NTP, with undefined behavior for negatives unless you can fork into the past

Use J and K for navigation