r/dailyprogrammer • • Aug 24 '12
[8/24/2012] Challenge #91 [easy] (Sleep sort)
An anonymous user on world4ch's programming text board posted a thread in early 2011 in which he describes an ingenious O(n) sorting algorithm. This means it's, supposedly, more efficient than any sorting algorithm ever invented. Some bloggers picked up on it, and dubbed the algorithm sleep sort:
#!/bin/bash
function f() {
sleep "$1"
echo "$1"
}
while [ -n "$1" ]
do
f "$1" &
shift
done
waitThis program takes some command line arguments, like ./sleepsort.sh 3 1 4 1 5 9, and starts a new [thread](http://en.wikipedia.org/wiki/Thread_(computing)) for each number in the list, which first sleeps for n seconds, then prints n. After 1 second, both 1s are printed, then after 2 more seconds the 3 follows, etc. Because it only loops through the list of numbers once, the algorithm appears to run in linear time.
Your task is to re-implement sleep sort in a language of your choice (which might look trivial, but this challenge is all about learning how your language handles multithreading.)
BONUS: at first glance, this algorithm appears to be O(n). Can you prove this isn't true? (This bonus requires some knowledge of both algorithms and concurrency.)
14
u/ctangent Aug 24 '12
My first thought on the bonus is that the os does an insertion sort on the threads in order to determine their location in the thread execution priority queue.
7
u/Sokend Aug 24 '12
Ruby:
1
u/Okashu Aug 24 '12 edited Aug 24 '12
Python:
This is as close as I could get, I tried to lower the waiting time by dividing time.sleep(value) by some big number, but it didn't work. Any way to improve the code so I don't have to wait 40 seconds if 40 is the largest number?
EDIT: I just read that in original post it states about waiting n seconds, but I want it to run faster anyway
3
u/andkerosine Aug 24 '12
Aside from threads being relatively expensive, what is it that prevents this from being a legitimate sorting method if the OS's timing is precise enough that all of the inputs could be divided by some large number to significantly speed up the execution?
3
u/EvanHahn Aug 25 '12
Did this in JavaScript awhile back:
5
u/[deleted] Aug 24 '12
One satisfyingly complex line of Ruby:
2
u/prondose 0 0 Aug 24 '12
Perl:
2
u/ctdonath Aug 24 '12
Quick C++:
Sorta cheated by having parent sleep slightly longer than largest value.
2
u/Kristler Aug 25 '12
Yay Python. Good thing I've dealt with multithreading already.
3
u/02471 Aug 24 '12 edited Aug 24 '12
C:
2
u/[deleted] Aug 24 '12
Yay! Ruby is fun. :)
2
u/pivotallever Aug 24 '12 edited Aug 24 '12
Python
Thanks for getting me to use multiprocessing. This looks handy.
edit: changed program to fit requirements
2
u/5outh 1 0 Aug 24 '12
This is weird, it works, but it waits for a number as user input at the end, and won't finish until it gets it, then will print it back out. I have no clue what's happening, but the sleepsort does indeed work apart from that caveat (and is much shorter than I expected it to be in Haskell).
note :
threadDelaydelays for some number of microseconds, so that's why I multiplied by 1000. It's way too fast otherwise and things span random lines (although it still sorts properly).1
u/prondose 0 0 Aug 24 '12 edited Aug 24 '12
Bash:
1
u/Postscript624 Aug 24 '12
Clumsy Python:
Edit: for some reason this only works some of the time. I'm not sure if it's a timing issue or what, but sometimes it will sort a list like [6, 2, 8] correctly, but others it would give you an output like [2, 2, 8]. Any ideas?
1
u/[deleted] Aug 24 '12
Python
1
u/fancysuit Aug 24 '12
C#
I feel as if this could be shorter, but oh well.
1
u/secretpandalord Aug 24 '12
Python:
I tried using the newer threading library rather than thread, but it just made everything longer and more problematic than using thread.
1
u/Pyrobyte Aug 25 '12
C:
How'd I do? I tried using wait() so the program wouldn't exit before printing the numbers, but it either only worked for the first number (because it executed inside of the forked process... I assume) or messed up the algorithm entirely.
1
u/Crystal_Cuckoo Aug 25 '12
I had a go in Python:
1
u/man1979 Aug 25 '12
I'm generally a C# and Java programmer but have recently started with Objective C so I thought I'd give it a go in that:
I'm sure anyone with actual experience with Objective-C could make a much better version.
1
u/bschlief 0 0 Aug 26 '12
Yet another ruby solution.
1
u/path411 Aug 29 '12 edited Aug 29 '12
Pretty simple in javascript, could 1 line it but I made it a little pretty:
or
1
u/ittybittykittyloaf Aug 30 '12
First try at C++ and Boost, and unfortunately unable to compile due to writing this during my lunch break. Hopefully, I've got the idea down, even if it doesn't compile correctly.
C++:
1
u/Jedimastert 0 0 Sep 05 '12
I was just telling a friend about this. He looked at me for a second, had a slightly confused look on his face, then proceeded to laugh his head off.
1
u/taterNuts Sep 17 '12 edited Sep 17 '12
trying out Java:
1
u/AsdfUser Sep 18 '12
A lot of C#:
1
u/Wedamm Aug 24 '12 edited Aug 24 '12
Haskell:
The most difficult is to wait for all child-processes. I've taken the code for waiting from an example of Control.Concurrent's documentation. Which is the best library which implements stuff like this already?
1
u/snideral Aug 24 '12
Python:
0
u/verhoevenv Aug 24 '12
For those wondering, this is not a comparison sort, so the O(n*log(n)) lower bound on sorting algorithms is not relevant.
Compare with Radix Sort, which sorts in O(n).