Genius sorting algorithm: Sleep sort
1
Name:
Anonymous
2011-01-20 12:22
Man, am I a genius. Check out this sorting algorithm I just invented.
#!/bin/bash
function f() {
sleep "$1"
echo "$1"
}
while [ -n "$1" ]
do
f "$1" &
shift
done
wait
example usage:
./sleepsort.bash 5 3 6 3 6 3 1 4 7
121
Name:
Anonymous
2011-05-19 22:11
122
Name:
Anonymous
2011-05-19 22:17
123
Name:
Anonymous
2011-05-19 23:34
>>2 >>5
if accept that floating-point sleep and scrpipt have only to sort with natural numbers...
#!/bin/bash
function f() {
sleep $(echo "($2 - 1) + $1 / 10 ^ $2" | bc -l)
echo "$1"
}
while [ -n "$1" ]
do
f "$1" $(echo -n "$1" | wc -c) &
shift
done
wait
example: 218382 -> 5.218382 seconds.
124
Name:
Anonymous
2011-05-20 4:27
>>118
What are you talking about?
O(n*k) with fixed k is just O(n) for all nonzero finite k.
125
Name:
Anonymous
2011-05-20 23:24
Cosmological sort
Wait for the universe to collapse and be replaced by a universe in which the input is in sorted order
126
Name:
Anonymous
2011-05-20 23:28
127
Name:
Anonymous
2011-05-20 23:28
128
Name:
Anonymous
2011-05-20 23:34
>>125
What if the new universe would have the same initial conditions and thus the input will always be in unsorted order?
129
Name:
Anonymous
2011-05-20 23:46
>>128
Destroy the universe, take another one.
130
Name:
Anonymous
2011-05-20 23:51
I think of sleepsort as a deferred kernelspace-sort.
131
Name:
kissrobber
2011-05-28 0:50
Hello.
I invented a new sorting algorithm in the Cloud age.
It's called the "Cloud Sort(Google App Engine TaskQueue Sort)".
http://d.hatena.ne.jp/kissrobber/20110527/1306508583
This use GAE TaskQueue(eta) instead of Sleep.
(
http://code.google.com/appengine/docs/python/taskqueue/tasks.html )
Specifications:
・The order of the sort result is not guaranteed.
・Sometimes, a part of sorted data is duplicated.
132
Name:
Anonymous
2011-05-28 1:23
・The order of the sort result is not guaranteed.
・Sometimes, a part of sorted data is duplicated
( ≖‿≖)
133
Name:
Anonymous
2011-05-28 3:47
It's O (n 2 ) under the hood because the kernel has to scan the list of waiting threads every pass through the idle loop. It's only O (n ) if you have n CPUs, in which case you should be using n -way mergesort, since that has already been shown to give O (log n ) time.
Remember, guys: the OS is not magic, and just because you didn't take an OS design course doesn't make you a wizard.
Only LispM designers get to be wizards.
134
Name:
Anonymous
2011-05-28 3:52
>>133
Sorry, correction about the merge sort; I was remembering it incorrectly:
http://drdobbs.com/high-performance-computing/229400239
135
Name:
Anonymous
2011-05-28 4:01
Only LispM designers get to be faggots.
136
Name:
Anonymous
2011-05-28 18:12
The Jews are after me
137
Name:
Anonymous
2011-06-15 12:28
A very similar approach is MRSI sort using an array of AMOS delayed event generators.
138
Name:
jon
2011-06-15 13:19
>>94
Here's a quick (3-minute) attempt at Perl golf:
#!/usr/bin/perl -l -MTime::HiRes=sleep
$SIG{CHLD} = sub { };
push @p, (fork or sleep($_), print, exit) for @ARGV;
waitpid $_, 0 for @p;
>>44
Has flaws: first, you get the numbers without spacing, which is bad; second, you get them to stdout instead as the procedure's result, which is un-Racket-like. Here are some alternatives, indented/linebreaked for clarity, not brevity:
(define (sleepsort1 lst)
(with-input-from-string
(with-output-to-string
(λ () (map thread-wait
(map (λ (x) (thread (λ () (sleep x) (printf "~a " x)))) lst))))
(λ () (port->list read))))
(define (sleepsort2 lst)
(define me (current-thread))
(map thread-wait
(map (λ (n) (thread (λ () (sleep n) (thread-send me n))))
lst))
(sequence->list (in-producer thread-try-receive false?)))
(define (sleepsort3 lst)
(define-values (i o) (make-pipe-with-specials))
(map thread-wait
(map (λ (n) (thread (λ () (sleep n) (write-special n o))))
lst))
(close-output-port o)
(port->list read-char-or-special i))
The second is my favorite, I think, but it assumes nothing else will be sending to its thread. If you didn't mind (require racket/async-channel), you could replace (current-thread) with (make-async-channel), thread-send with async-channel-put, and thread-try-receive with (λ () (async-channel-get me)) which solves that problem.
139
Name:
Anonymous
2011-06-15 13:30
HI REDDIT!!!
140
Name:
Anonymous
2011-06-15 13:40
hi paul graham!!
141
Name:
jon
2011-06-15 13:40
142
Name:
Anonymous
2011-06-15 14:10
The fags code like cookies dipped in milk. Soggy and gross.
143
Name:
KalEl
2011-06-15 14:16
Fixed. Sorts including negative values, sorts any numbers within 1 second.
#!/bin/bash
function f() {
sleep $(echo "scale=10; 1/(1+1.1^(-$1))" | bc)
echo "$1"
}
while [ -n "$1" ]
do
f "$1" &
shift
done
wait
144
Name:
Anonymous
2011-06-15 15:11
Greetings from reddit.
Your creation is much better than anything I've ever seen on /r/programming before!
145
Name:
mike
2011-06-15 15:14
function sleepsort() {
for (var i = 0, il = arguments.length; i < il; i++) {
(function(args, index) {
setTimeout(function() {
document.body.innerHTML += args[index] + ', ';
}, args[index]);
}(arguments, i));
}
};
sleepsort(5, 26, 3, 845, 1276, 536, 825, 77);
146
Name:
foo
2011-06-15 15:20
Don't think there was a Python version, so here we go.
It's 'optimized' by waiting only 1/4th of the seconds
in each thread.
#!/usr/bin/python
import sys, time, threading
def sleepit(val):
time.sleep(val/4.0)
print val
[ threading.Thread(target=sleepit, args=[int(a)]).start() for a in sys.argv[1:] ]
147
Name:
Anonymous
2011-06-15 15:25
var numbers = process.argv.slice(2)
, output = []
, negative = []
for (var i=0, ln=numbers.length; i<ln; i++){
var n = +numbers[i]
setTimeout((function(n){ return function(){
if (n<0) negative.unshift(n)
else output.push(n)
if (output.length + negative.length === numbers.length) {
return console.log(negative.concat(output))
}
}})(n), Math.abs(n)/1000)
}
$ node sleepsort -2 1 0.1 4 -1 7 -3 9
[ -3, -2, -1, 0.1, 1, 4, 7, 9 ]
148
Name:
Anonymous
2011-06-15 15:32
DIGG SENT ME HERE!
149
Name:
Anonymous
2011-06-15 15:47
PLEASE DO NOT INCREASE THE POPULATION OF /prog/ BEYOND 3, THANKS!!!
150
Name:
Anonymous
2011-06-15 15:50
F#:
let sleepsort (xs: int list) =
for x in xs do Async.Start <| async { Threading.Thread.Sleep x; printfn "%d" x }
151
Name:
Anonymous
2011-06-15 15:54
All this talk of new different versions, yet I see no code
.
152
Name:
Anonymous
2011-06-15 15:54
it is vulnerable to race conditions
153
Name:
Anonymous
2011-06-15 16:09
Ruby:
def f(i)
sleep i
puts i
end
[3,2,3,4,2,1].each do |i|
Kernel.fork {
f(i)
}
end
154
Name:
Anonymous
2011-06-15 16:09
Hacker New up in this bitch
155
Name:
Anonymous
2011-06-15 16:14
>>152
That's been addressed at least 3 times already.
156
Name:
Anonymous
2011-06-15 16:46
vals = [100, 25, 3,2,1]
sorted = (vals.min..vals.max).to_a.select{ |i|
vals.include? i
}
157
Name:
Anonymous
2011-06-15 17:28
158
Name:
Anonymous
2011-06-15 17:31
you are sick
159
Name:
sharkdabark
2011-06-15 18:02
Doesn't work.
./sleepsort.bash 1.00001 1
1.00001
1
160
Name:
Anonymous
2011-06-15 18:08
>>159
Serious question, are you autistic or just retarded?
Newer Posts