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
281
Name:
eddyb
2011-06-17 3:46
c'mon. can't someone clean this thread? lame, I like sleepsort, but now it's full of fags.
282
Name:
Anonymous
2011-06-17 4:00
At first I was like :0 but then I was like I dont even...so now I am convinced that you guys have invented a time machine
283
Name:
Alfe
2011-06-17 5:28
Shortest versions? The Ruby oneliner was nice already, but I think bash can be even smaller:
Ruby:
ARGV.each { |e| fork { sleep(e.to_f/1000); puts e } }
Bash:
for n; do { sleep $n; echo $n; } & done
(usage:
$ f() { echo $(for n; do { sleep $n; echo $n; } & done); }
$ f 3 2 1
1
2
3
$
)
284
Name:
Anonymous
2011-06-17 5:42
This is a hoot. Impressive, genius.
===Multi-threaded Perl version===
#! /usr/bin/perl -w
use strict;
use threads;
my ($i, @thrs) = (0);
foreach (@ARGV) {$thrs[$i++] = threads->create(\&sorter, $_)}
$i = 0;
foreach (@ARGV) {$thrs[$i++]->join()}
sub sorter {sleep($_[0]); print $_[0], "\n"}
===End of Sleep Sort===
If one uses Time::HiRes, one could sleep for sub-second times.
285
Name:
Java Suit
2011-06-17 6:31
JAVA
286
Name:
spacebat
2011-06-17 6:46
perl -E 'fork || do { sleep $_, say, exit } for @ARGV; until (wait<0) {}' 5 2 7 4 1
287
Name:
Anonymous
2011-06-17 8:40
Lua POSIX version:
require ('posix')
function sleep (n)
os.execute ('sleep ' .. n)
end
function sleepsort (arr)
for i = 1, #arr do
pid = posix.fork ()
if pid == 0 then
-- posix.sleep does not work with float
sleep (arr[i] / 10)
print (arr[i] )
return
end
end
end
sleepsort (arg)
288
Name:
Anonymous
2011-06-17 8:45
No one is posting code.
289
Name:
lolex
2011-06-17 8:57
posting on EPIC thread
290
Name:
druud
2011-06-17 9:27
perl -wle 'fork&&print(sleep$_)&&exit for@ARGV' 6 3 4 2 1 5 7
291
Name:
druud
2011-06-17 9:33
And for purists:
perl -wle 'fork||print(sleep$_)&&exit for@ARGV;1while-1!=wait' 6 3 4 2 1 5 7
292
Name:
Anonymous
2011-06-17 9:47
Jeepers, you could at least use [code]
tags, silly redditors.
293
Name:
Anonymous
2011-06-17 9:48
>>292
AND YOU SHOULD CHECK THE FUCKING SAGE BOX ASSHOLE
294
Name:
druud
2011-06-17 9:54
perl -wle'fork||(sleep$_,print,exit)for@ARGV' 6 3 4 7 2 1 5
295
Name:
Anonymous
2011-06-17 10:05
<- check'em dubs
296
Name:
Anonymous
2011-06-17 10:10
>>293
you can check my box
297
Name:
Anonymous
2011-06-17 10:24
>>293
Stop being an autist.
298
Name:
lbolla
2011-06-17 10:44
and a LISP implementation (SBCL only):
(use-package :sb-thread)
(defun show (item)
(sleep item)
(format t "~A~%" item))
(defun sleep-sort (lst)
(mapcar
#'(lambda (item) (make-thread #'(lambda () (show item))))
lst))
299
Name:
Anonymous
2011-06-17 10:45
>>298
mapcar
for side effects? Get out.
300
Name:
Anonymous
2011-06-17 11:04
>>299
my other mapcar is a mdpcdr
301
Name:
Anonymous
2011-06-17 13:20
????????????????????????????/
302
Name:
Anonymous
2011-06-17 13:37
Please use code tag when posting code to this forum!
303
Name:
Anonymous
2011-06-17 13:43
304
Name:
Anonymous
2011-06-17 13:44
Please code post tag when coding use to this forum!
305
Name:
Anonymous
2011-06-17 14:02
sleep sort in Objective-C using blocks:
@interface NSArray (SleepSort)
- (void)sleepSortObjectsUsingBlock:(void (^)(id obj))block;
@end
@implementation NSArray (SleepSort)
- (void)sleepSortObjectsUsingBlock:(void (^)(id obj))block
{
for (id obj in self) {
NSDictionary *info = [NSDictionary dictionaryWithObjectsAndKeys:obj, @"obj", block, @"block", nil];
[self performSelector:@selector(_handleSleepSortItemWithInfo:) withObject:info afterDelay:[obj intValue]];
}
}
- (void)_handleSleepSortItemWithInfo:(NSDictionary *)info
{
id obj = [info objectForKey:@"obj"];
void (^block)(id obj) = [info objectForKey:@"block"];
block(obj);
}
@end
To use:
NSArray *items = [NSArray arrayWithObjects:
[NSNumber numberWithInt:5],
[NSNumber numberWithInt:3],
[NSNumber numberWithInt:6],
[NSNumber numberWithInt:3],
[NSNumber numberWithInt:6],
[NSNumber numberWithInt:3],
[NSNumber numberWithInt:1],
[NSNumber numberWithInt:4],
[NSNumber numberWithInt:7],
nil];
[items sleepSortObjectsUsingBlock:^(id obj) { NSLog(@"obj = %@", obj); }];
306
Name:
Anonymous
2011-06-17 14:10
SPREAD THE FAIL WHALE
▄██████████████▄▐█▄▄▄▄█▌
██████▌▄▌▄▐▐▌███▌▀▀██▀▀
████▄█▌▄▌▄▐▐▌▀███▄▄█▌
▄▄▄▄▄██████████████▀
307
Name:
Anonymous
2011-06-17 15:03
>>133
Jesus Christ , don't they teach anything to kids these days‽
Sepples doesn't have type inference – why did you put that
i + 1
in there? That turns the whole functor into a monomorphism (instead of the usual polymorphism which you undoubtedly have heard about when learning Sepples). Good luck with referential transparency now.
Why do you pass
smallest
as a reference? 1..n relations are terrible
! practice.
Why the fixed loop iterations? Do you want to subject your code to a stack pointer monadic overflow?
You should rewrite your code after getting acquainted with the Sussman protocol, as defined in RFC (``
Request for Cudders '') 6001.
308
Name:
Anonymous
2011-06-17 15:58
309
Name:
Anonymous
2011-06-17 18:09
JESUS FUCKING CHRIST
USE CODE TAGS, YOU STUPID FUCKERS
I DON'T CARE ABOUT THIS SHIT ANYMORE
/prog/ RIDERS, YOU CAN CALL ME OFF TO /b/
BECAUSE YEAH
I MAD
FUCKING REDDIT SCUM
USE
MOTHERFUCKING
CODE
TAGS
YOU
DUMB
RETARDS
310
Name:
Anonymous
2011-06-17 18:58
311
Name:
Anonymous
2011-06-17 19:32
<-- check em dubz
312
Name:
Anonymous
2011-06-17 20:38
I didn't bother to read the thread but this shit is O(1) obviously.
OP your Turing Award is in the mail.
313
Name:
Anonymous
2011-06-18 21:56
>>312
this shit is O(1) obviously.
You're thinking RPCsort.
314
Name:
Anonymous
2011-06-19 5:40
315
Name:
Anonymous
2011-06-19 6:29
Nice thread, bros.
316
Name:
ADS the new Autism
2011-06-19 8:35
le discusion (yes im offten at reddit and its way better than prog ever was)
317
Name:
Lulz
2011-06-19 14:20
Algorithms of this type are quite normal in the hacking world. You guys need some more lulz gun powder it seems . I see more script kiddies here
318
Name:
Anonymous
2011-06-19 17:35
319
Name:
Anonymous
2011-06-20 3:04
Oh no, I didn't know this link would bring me to where all these 4chan weirdos hang out.
Note to the haters: Reddit rocks!
:)
320
Name:
2011-06-20 3:13
Note from the rockers: Reddit haters!
Newer Posts