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-01-21 8:01

>>40
Knuth doesn't do email anymore

Name: Anonymous 2011-01-21 8:05

>>41
That's only because he's DED.

Name: >>28 2011-01-21 8:31

-module(sleepsort).
-export([sort/2, spawn_waiters/3, wait/4]).

sort(Xs, Conv) ->
    spawn(?MODULE, spawn_waiters, [Xs, Conv, self()]),
    receive
        {spawned, N} -> queue(N)
    end.

queue(N) -> queue(N, []).
queue(0, Xs) ->
    lists:reverse(Xs);
queue(N, Xs) ->
    receive
        {item, X} ->
            queue(N - 1, [X|Xs])
    end.

spawn_waiters(Xs, Conv, P) -> spawn_waiters(P, Conv, Xs, 0).
spawn_waiters(P, _, [], N) ->
    P ! {spawned, N};
spawn_waiters(P, Conv, [X|Xs], N) ->
    spawn(?MODULE, wait, [X, Conv(X), P, self()]),
    receive
        monitored -> ok
    end,
    spawn_waiters(P, Conv, Xs, N + 1).

wait(X, T, Q, P) ->
    Ref = erlang:monitor(process, P),
    P ! monitored,
    receive
        {'DOWN', Ref, process, P, _} -> ok
    end,
    timer:sleep(T),
    Q ! {item, X}.


Also this. I'm an EARLY ADOPTER.

Name: Anonymous 2011-01-21 8:37


(module sleepsort racket
   (provide sleepsort)
   (define (sleepsort x)
      (map (lambda (x) (thread (lambda () (sleep x) (display x)))) x)))

Name: Anonymous 2011-01-21 8:40

>>44
display
Are you also one of the people that define their square as IO ()?

Name: Anonymous 2011-01-21 8:44

History in the making, guys. Turns out /prog/ can do good things.

Name: Anonymous 2011-01-21 8:47

>>43
Holy fuck Erlang in /prog/?!?!?!?!

Props.

Name: Anonymous 2011-01-21 8:47

>>45
Yes.
void CalculateAndPrintSquareEx(DWORD dwOperand, HANDLE hOutputDevice, ...) { fprintf(hOutputDevice, "%d", dwOperand*dwOperand); }
int main (int argc, char *argv[]) {
   CalculateAndPrintSquareEx(2, stdout, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL); }

Name: Anonymous 2011-01-21 8:54

>>48
That's :: Int -> IO ().
I was thinking more along the lines of ReadParseSquareAndOutput(...).

Name: Anonymous 2011-01-21 8:59

>>49
That's :: DoubleWord -> Handle -> Nothing -> Nothing -> Nothing -> Nothing -> Nothing -> Nothing -> Nothing -> Nothing -> Nothing -> Nothing -> Nothing -> Nothing -> Nothing -> Nothing -> Nothing -> Nothing -> Nothing -> Nothing -> Nothing -> Nothing -> Nothing -> Nothing -> Nothing -> Nothing -> Nothing -> Nothing -> Nothing -> Nothing -> Nothing -> Nothing -> Nothing -> Nothing -> IO ().

Name: Anonymous 2011-01-21 9:12

>>50
More like :: Int32 -> IORef -> Maybe a -> Maybe a -> Maybe a -> Maybe a -> Maybe a -> Maybe a -> Maybe a -> Maybe a -> Maybe a -> Maybe a -> Maybe a -> Maybe a -> Maybe a -> Maybe a -> Maybe a -> Maybe a -> Maybe a -> Maybe a -> Maybe a -> Maybe a -> Maybe a -> Maybe a -> Maybe a -> Maybe a -> Maybe a -> Maybe a -> Maybe a -> Maybe a -> Maybe a -> Maybe a -> Maybe a -> Maybe a -> Maybe a -> Maybe a -> Maybe a -> Maybe a -> Maybe a -> Maybe a -> Maybe a -> Maybe a -> Maybe a -> Maybe a -> Maybe a -> Maybe a -> Maybe a -> Maybe a -> Maybe a -> Maybe a -> Maybe a -> Maybe a -> Maybe a -> Maybe a -> Maybe a -> Maybe a -> Maybe a -> Maybe a -> Maybe a -> Maybe a -> Maybe a -> Maybe a -> Maybe a -> Maybe a -> Maybe a -> Maybe a -> Maybe a -> Maybe a -> Maybe a -> Maybe a -> Maybe a -> Maybe a -> Maybe a -> Maybe a -> Maybe a -> Maybe a -> Maybe a -> Maybe a -> Maybe a -> Maybe a -> Maybe a -> Maybe a -> Maybe a -> Maybe a -> Maybe a -> Maybe a -> Maybe a -> Maybe a -> Maybe a -> Maybe a -> Maybe a -> IO ()
(I counted.)

Name: Anonymous 2011-01-21 9:16

>>51
Maybe (Ptr a)

Name: Anonymous 2011-01-21 9:20

>>51
Wait, fuck. That should be:
CalculateAndPrintSquareEx :: Int32 -> IORef -> Maybe a -> Maybe b -> Maybe c -> Maybe d -> Maybe e -> Maybe f -> Maybe g -> Maybe h -> Maybe i -> Maybe j -> Maybe k -> Maybe l -> Maybe m -> Maybe n -> Maybe o -> Maybe p -> Maybe q -> Maybe r -> Maybe s -> Maybe t -> Maybe u -> Maybe v -> Maybe w -> Maybe x -> Maybe y -> Maybe z -> Maybe a' -> Maybe b' -> Maybe c' -> Maybe d' -> Maybe e' -> Maybe f' -> Maybe g' -> Maybe h' -> Maybe i' -> Maybe j' -> Maybe k' -> Maybe l' -> Maybe m' -> Maybe n' -> Maybe o' -> Maybe p' -> Maybe q' -> Maybe r' -> Maybe s' -> Maybe t' -> Maybe u' -> Maybe v' -> Maybe w' -> Maybe x' -> Maybe y' -> Maybe z' -> Maybe a'' -> Maybe b'' -> Maybe c'' -> Maybe d'' -> Maybe e'' -> Maybe f'' -> Maybe g'' -> Maybe h'' -> Maybe i'' -> Maybe j'' -> Maybe k'' -> Maybe l'' -> Maybe m'' -> Maybe n'' -> Maybe o'' -> Maybe p'' -> Maybe q'' -> Maybe r'' -> Maybe s'' -> Maybe t'' -> Maybe u'' -> Maybe v'' -> Maybe w'' -> Maybe x'' -> Maybe y'' -> Maybe z'' -> Maybe a''' -> Maybe b''' -> Maybe c''' -> Maybe d''' -> Maybe e''' -> Maybe f''' -> Maybe g''' -> Maybe h''' -> Maybe i''' -> Maybe j''' -> Maybe k''' -> IO ()

Name: Anonymous 2011-01-21 12:23

oh thread is officially shit now. congrats /prog/

Name: Anonymous 2011-01-21 12:31

>>54
np faggot

Name: Anonymous 2011-01-21 12:46

>>55
nobody likes you and nobody would miss you

Name: Anonymous 2011-01-21 13:10

>>56
girls dont like you

Name: Anonymous 2011-01-21 13:16

>>57
Apostrophes and capital letters don't like you.

Name: Anonymous 2011-01-21 15:03

>>58
girl's Dont like you

Name: Anonymous 2011-01-21 15:17

your gay

Name: Anonymous 2011-01-21 15:23

The Autism Consortium frowns upon this thread.

Name: Anonymous 2011-01-21 15:24

To the person reading this in the future, >>54-60 are from something which we call an ``imageboard'', more specifically the imageboard called ``/g/'', this means that they enjoy making bad posts with little to no-content.

Name: Anonymous 2011-01-21 15:26

>>62
I lol'd!

Name: Anonymous 2011-01-21 16:11

>>62
nice.

Name: Anonymous 2011-01-21 16:52

>>63,64
NOOOOooooooOOOOOOOOoooooooooooOOOOOOOOoooo

Name: Anonymous 2011-01-21 16:54

>>65
nice.

Name: Anonymous 2011-01-21 17:21

>>65
I lol'd

Name: Anonymous 2011-01-21 18:00

>>65
I bet you miss ``HAX MY ANUS'' now, motherfucker.

Name: Anonymous 2011-01-21 18:00

>>1
Good job. Now try writing it in an actual language, like C.

Name: Anonymous 2011-01-21 18:08

Hax Anii everyday.
Anii MUST be haxxed.

Name: Anonymous 2011-01-21 18:47

>>69

My implementations of sleep and echo are written in C.

Name: Anonymous 2011-01-21 18:54

>>69
ooh no i'd have to use pipe() and fork() !

Name: Anonymous 2011-01-21 19:02

>>71
That's like saying you wrote a hardware implementation of sleepsort because it's run on hardware.

Name: Anonymous 2011-01-21 19:05

>>73

Well most of the time in that script is spent in those programs, if your intention was to optimize it by writing it in C it wouldn't work.

Name: Anonymous 2011-01-21 19:23

>>72
pipe()
What for?  Sounds like this would be an actual challenge to you.

Name: Anonymous 2011-01-21 20:29

>>74
>>69 is just being a turd. A big part of the novelty here is that it's a shell script.

Name: Anonymous 2011-01-22 5:38

>>75
to set up a signalling channel

Name: Anonymous 2011-02-02 13:39

I think thats brilliant :)
Would be fun to design a hardware sorter, based on this..
hmm, something to think about after my partials.

Name: Anonymous 2011-02-02 14:33

#include <assert.h>
#include <stdio.h>
#include <unistd.h>

int main(int argc, char **argv)
{
        assert(argc >= 2);
        while (argc --> 2 && fork()) ;
        sleep(atoi(argv[argc]));
        puts(argv[argc]);
        while (wait(NULL) != -1) ;
}

Name: Anonymous 2011-02-02 14:49

>>79
+1 use of goes-to operator

Newer Posts