Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Genius sorting algorithm: Sleep sort

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

Name: Anonymous 2011-06-16 12:32

pretty fucking cool

Name: Anonymous 2011-06-16 12:35

Anyways, >>260, please listen to me. That it's really related to this thread.
I went to Yoshinoya a while ago; you know, Yoshinoya?
Well anyways there was an insane number of people there, and I couldn't get in.
Then, I looked at the banner hanging from the ceiling, and it had "150 yen off" written on it.
Oh, the stupidity. Those idiots.
You, don't come to Yoshinoya just because it's 150 yen off, fool.
It's only 150 yen, 1-5-0 YEN for crying out loud.
There're even entire families here. Family of 4, all out for some Yoshinoya, huh? How fucking nice.
"Alright, daddy's gonna order the extra-large." God I can't bear to watch.
You people, I'll give you 150 yen if you get out of those seats.
Yosinoya should be a bloody place.
That tense atmosphere, where two guys on opposite sides of the U-shaped table can start a fight at any time,
the stab-or-be-stabbed mentality, that's what's great about this place.
Women and children should screw off and stay home.
Anyways, I was about to start eating, and then the bastard beside me goes "extra-large, with extra sauce."
Who in the world orders extra sauce nowadays, you moron?
I want to ask him, "do you REALLY want to eat it with extra sauce?"
I want to interrogate him. I want to interrogate him for roughly an hour.
Are you sure you don't just want to try saying "extra sauce"?
Coming from a Yoshinoya veteran such as myself, the latest trend among us vets is this, extra green onion.
That's right, extra green onion. This is the vet's way of eating.
Extra green onion means more green onion than sauce. But on the other hand the price is a tad higher. This is the key.
And then, it's delicious. This is unbeatable.
However, if you order this then there is danger that you'll be marked by the employees from next time on; it's a double-edged sword.
I can't recommend it to amateurs.
What this all really means, though, is that you, >>260-kun, should just stick with today's special.

Name: James Herring 2011-06-16 12:39

Here's my more efficient rendition of the OP's code: http://bit.ly/97Q3tx

Name: Anonymous 2011-06-16 13:36



Why would you want to wait for 2,000 milliseconds just to sort this array: [2000 1]

Name: Anonymous 2011-06-16 14:01

>>263
THE SAME OBSERVATION WAS MADE IN THE FIRST REPLY, OVER 5 MONTHS AGO. PLEASE GO BACK TO REDDIT!

Name: Anonymous 2011-06-16 14:04

hbk

Name: Anonymous 2011-06-16 14:09

>>264
* HAS BEEN MADE

Name: Anonymous 2011-06-16 14:14

>>262
Stop - there might be a problem with the requested link

Name: Oliv 2011-06-16 14:20

>>4
In terms of operations, the complexity is always O(n), even in average and worse cases. That's something no other algorithm can do according to http://en.wikipedia.org/wiki/Sorting_algorithm#Comparison_of_algorithms

Name: Anonymous 2011-06-16 14:31

Thanks, Oliv.

Name: Anonymous 2011-06-16 14:45

>>267
troll

Name: Anonymous 2011-06-16 15:22

>>268
O(myanus)

Name: Anonymous 2011-06-16 15:29

Java-Fag posts java-impl:





import  java.util.ArrayList;

public class Main extends Thread{
    Long toSleep;
    ArrayList<Long> solution;
   
    public static void main(String[] args){
        ArrayList<Long> solution = new ArrayList<Long>();
        ArrayList<Thread> solverThreads = new ArrayList<Thread>();
       
        for(String val: args){
            Long valNum = Long.parseLong(val);
            Thread solverThread = new Main(valNum, solution);
           
            solverThread.start();
            solverThreads.add(solverThread);
        }
       
        for(Thread solverThread: solverThreads){
            try {
                solverThread.join();
            } catch (InterruptedException e) {
                // gtfo
            }
        }
       
        System.out.println(solution.toString());
    }
   
    public Main(Long toSleep, ArrayList<Long> solution){
        this.toSleep = toSleep;
        this.solution = solution;
    }
   
    public void run(){
        try {
            Thread.sleep(toSleep*1000);
        } catch (InterruptedException e) {
            // gtfo
        }
       
        solution.add(toSleep);
    }
}

Name: Anonymous 2011-06-16 16:00

>>262
i had to click though two consecutive malware warnings to se a parrot on a dick. the attempts to harm my pc are cute, but futile. (java? srsly?)

Name: Anonymous 2011-06-16 16:31

>>273
back to reddit please

Name: Anonymous 2011-06-16 17:14

where the fuck is all the python code?

all of these other shitty languages are getting attention and the only good one isn't

Name: Anonymous 2011-06-16 17:54

>>275
See >>274

Name: yiihsia 2011-06-17 1:14

haha,java:


public class SleepSort {
    public static void main(String[] args) {
        int[] ints = {1,4,7,3,8,9,2,6,5};
        SortThread[] sortThreads = new SortThread[ints.length];
        for (int i = 0; i < sortThreads.length; i++) {
            sortThreads[i] = new SortThread(ints[i]);
        }
        for (int i = 0; i < sortThreads.length; i++) {
            sortThreads[i].start();
        }
    }
}
class SortThread extends Thread{
    int ms = 0;
    public SortThread(int ms){
        this.ms = ms;
    }
    public void run(){
        try {
            sleep(ms*10+10);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println(ms);
    }
}

Name: Anonymous 2011-06-17 2:32

>>276
See >>279.

Name: Anonymous 2011-06-17 2:32

back to /b/

Name: Anonymous 2011-06-17 3:41

>>275
That's because your community is a bunch of cocksuckers who do nothing but praising python and bashing everything else.

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.

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

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
 $
)

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.

Name: Java Suit 2011-06-17 6:31

JAVA

Name: spacebat 2011-06-17 6:46

perl -E 'fork || do { sleep $_, say, exit } for @ARGV; until (wait<0) {}' 5 2 7 4 1

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)

Name: Anonymous 2011-06-17 8:45

No one is posting code.

Name: lolex 2011-06-17 8:57

posting on EPIC thread

Name: druud 2011-06-17 9:27

perl -wle 'fork&&print(sleep$_)&&exit for@ARGV' 6 3 4 2 1 5 7

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

Name: Anonymous 2011-06-17 9:47

Jeepers, you could at least use [code] tags, silly redditors.

Name: Anonymous 2011-06-17 9:48

>>292
AND YOU SHOULD CHECK THE FUCKING SAGE BOX ASSHOLE

Name: druud 2011-06-17 9:54

perl -wle'fork||(sleep$_,print,exit)for@ARGV' 6 3 4 7 2 1 5

Name: Anonymous 2011-06-17 10:05

<- check'em dubs

Name: Anonymous 2011-06-17 10:10

>>293
you can check my box

Name: Anonymous 2011-06-17 10:24

>>293
Stop being an autist.

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))

Name: Anonymous 2011-06-17 10:45

>>298
mapcar
for side effects? Get out.

Name: Anonymous 2011-06-17 11:04

>>299
my other mapcar is a mdpcdr

Name: Anonymous 2011-06-17 13:20

????????????????????????????/

Name: Anonymous 2011-06-17 13:37

Please use code tag when posting code to this forum!

Name: Anonymous 2011-06-17 13:43

>>302
fuck you fag

Name: Anonymous 2011-06-17 13:44

Please code post tag when coding use to this forum!

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); }];

Name: Anonymous 2011-06-17 14:10

SPREAD THE FAIL WHALE

▄██████████████▄▐█▄▄▄▄█▌
██████▌▄▌▄▐▐▌███▌▀▀██▀▀
████▄█▌▄▌▄▐▐▌▀███▄▄█▌
▄▄▄▄▄██████████████▀

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.

Name: Anonymous 2011-06-17 15:58

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

Name: Anonymous 2011-06-17 18:58

>>309
You can stay.

Name: Anonymous 2011-06-17 19:32

<-- check em dubz

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.

Name: Anonymous 2011-06-18 21:56

>>312
this shit is O(1) obviously.
You're thinking RPCsort.

Name: Anonymous 2011-06-19 5:40

>>311
nice dubz bro

Name: Anonymous 2011-06-19 6:29

Nice thread, bros.

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)

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

Name: Anonymous 2011-06-19 17:35

>>317
U JELLY

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!

:)

Name: 2011-06-20 3:13

Note from the rockers: Reddit haters!

Name: Anonymous 2011-06-20 6:21

>>319 notsureiftrolling.jpg

Name: Anonymous 2011-06-20 14:43

Ok, I've been thinking about this sleep sort thing for a few days now.  Thanks OP for nerd-sniping me.  First off, the command `usleep` should be used in place of `sleep` for a significant speedup.  But along this same vein, what we really want is to get to the granularity of the number of clock cycles it takes to print a number.  Furthermore, if we alter this for a real-time scheduler, then we can just schedule each print with a priority of i times a constant (the amount of time it would take to print i).  The scary part is that this would run in O(n) time -- which is supposed to be impossible.

Name: Anonymous 2011-06-20 14:58

i still dont get for what u should use that thing

Name: Anonymous 2011-06-20 15:03

sleepsort is a variation on loopsort which is O(n)

You first find the minimum and maximum O(n)

make a bitfield with size [maximum - minimum] O(1)

Loop through the input array again, setting a bit to 1 at [iterator - minimum] O(n)

Loop through the bitfield and push the value (iterator + minimum) on a stack. O(n)

O(n) + O(n) + O(n) + O(1) = O(n)

Name: Anonymous 2011-06-20 15:11

But isn't it a counting sort?

Name: Anonymous 2011-06-20 15:33

[code]#!/usr/bin/python2
import sys, time, threading

def ss(i):
    time.sleep(int(i))
    print i

for i in sys.argv[1:]:
    threading.Thread(target=ss, args=i).start()

Name: Anonymous 2011-06-20 16:15

>>326
reddit children, behave!

Name: Anonymous 2011-06-21 7:58

Keep, Because you editors keep behaving as nazis. I am a German citizen and when I was a young boy watched books being burned. You delete article of programming language because they are "not notable". By your definition, "Puff Daddy" are "not notable" to me. I should propose to delete their article. There is nothing wrong with information. You are nazis. To hell with you. 79.169.56.225 (talk) 11:58, 21 June 2011 (UTC)

Name: Anonymous 2011-06-21 8:59

>>328
VIPPER detected

Name: Anonymous 2011-06-21 13:48

Hey guys, It isn't any linear or quadratic time algorithm. Its an exponential time algorithm - "The worst" we can say in sorting algorithms. However the sleep thing is fantastic as CPU can perform another operations interim.

Name: Anonymous 2011-06-21 15:16

>>330
Hey guys, It isn't any linear or quadratic time algorithm. Its an exponential time algorithm

The one implementation can run in linear time. Now shut the fuck up and let the little jewish girls show us what they know.

Name: Anonymous 2011-06-21 15:25

<-- check em dubz

Name: Anonymous 2011-06-21 15:26

>>332
nice

Name: Anonymous 2011-06-21 17:21

OP here. Did someone submit this to reddit, or did they jsust randomly pick it up?   This has gone full lulz - especially the wikipedia afd discussion...also been googling, loads of blogs and stuff fapping over it - google translate is my friend for reading all the jap/chinny blogs

Name: Anonymous 2011-06-21 19:47

>>334
OP here.
Will save to disbelieve…success!

gone full lulz
We had a funny joke.  A bunch of idiots who hadn't heard a joke in 10 years eventually ran into it.  Now we have a funny joke and a bunch of idiots.

Name: Anonymous 2011-06-21 20:04

>>334
Wait, WAIT. It's on wikipedia too? Goddamn.

Name: Anonymous 2011-06-21 20:12

>>336
What did you expect?

Name: Anonymous 2011-06-21 22:56

It's on #scheme too.


<klutometis> cky: It turns out the pjb was actually talking about sleep-sort: <http://dis.4chan.org/read/prog/1295544154>;.

Name: Anonymous 2011-06-22 5:28

http://en.wikipedia.org/wiki/Sleep_sort
Although its origins are unclear, an internet forum thread from 2011 by an anonymous programmer is believed to be the earliest known reference to the algorithm.

Also this thread is an ``unreliable source''.

Name: Anonymous 2011-06-22 5:59

We Wikipedia now!

Name: Anonymous 2011-06-22 8:46

>>340

No, I'm Wikipedia!

Name: Anonymous 2011-06-22 8:53

requesting a picture of the sussman being peed on to undermine this site as a soruce

Name: Anonymous 2011-06-22 9:08

http://hackerne.ws/item?id=2657942
The mailto:sage links in some of the posts are essentially public downvotes; from the Japanese "下げ", meaning "down", and pronounced "sah ge".
Incorrect, sbierwagen.

Name: Anonymous 2011-06-22 9:26

>>343
DOWNVOTED! SAGE!

Name: Anonymous 2011-06-22 9:31

>>344
Downvoted.

Name: Anonymous 2011-06-22 9:38

<-- check em dubz

Name: Anonymous 2011-06-22 9:40

>>346
Good job.

Name: Anonymous 2011-06-22 9:47

Sageru in the Japanese language is the dictionary form of to lower. The case of applying sage to the email field will mean that the thread is not bumped to the top of the thread list, the thread position is maintained as opposed to the standard behaviour when the thread position is normally bumped to the top of the list.

Name: Anonymous 2011-06-22 10:01

>>348
Downvoteru.

Name: Anonymous 2011-06-22 10:26

>>348

upboat

Name: Anonymous 2011-06-22 11:02

DEY TURK ERRR DUBZ!!!!

Name: Anonymous 2011-06-22 16:54

I consider myself conversant with 4chan, and I had no idea that the dis subdomain existed.
I always wondered if there was a chan for hackers...now I realize there is. And oh boy, what a chan it is.

Boy is he in for a surprise when he finds out /prog/ is just a bunch of autistic undergrads with a hard-on for functional programming.

Name: Anonymous 2011-06-22 17:50

>>352
speak for yourself machumbo... i work as a developer and only use imperative languages

Name: Anonymous 2011-06-22 17:55

GTFO

Name: Anonymous 2011-06-22 18:40

>>352

You never realized the whole read SICP thing was just a joke?
Wow, you must be pretty stupid.

Name: Anonymous 2011-06-22 18:44

>>355

You never realized the whole ``You never realized the whole read SICP thing was just a joke? Wow, you must be pretty stupid.'' thing was just a joke?
Wow, you must be pretty stupid.

Name: Anonymous 2011-06-22 19:04

>>356

You never realized the whole ``You never realized the whole ``You never realized the whole read SICP thing was just a joke? Wow, you must be pretty stupid.'' thing was just a joke? Wow, you must be pretty stupid.'' thing was just a joke?
Wow, you must be pretty stupid.

Name: Anonymous 2011-06-22 19:54

>>356,357

You're not funny, please go back to /g/ or wherever you came from.

Name: Anonymous 2011-06-22 20:00

>>355-357
[m]Back to /prog/, please.[m]

Newer Posts