Login Register


Most beautiful algorithm? Its ART filter_list
Author
Message
Most beautiful algorithm? Its ART #1
Hello

Ive lately been really interested into algorithms and decided to show you guys a cool and simple algorithm.

Sleep sort algorithm

Original code: http://dis.4chan.org/read/prog/1295544154


#!/bin/bash
function f() {
sleep "$1"
echo "$1"
}
while [ -n "$1" ]
do
f "$1" &
shift
done
wait
----------------------------------------------------------------------

Now thats what I call art. :Laughing:

Sleep sort is a joke sorting algorithm that became popular on the 4chan board /prog/ [1]. The pseudocode for sleep sort is:

----------------------------------------------------------------------

procedure printNumber(n)
sleep n seconds
print n
end

for arg in args
run printNumber(arg) in background
end
wait for all processes to finish

----------------------------------------------------------------------

Ha-ha! Hilarious.

In other words, what it does is that sleep sort spawns off one process for each argument. Each process waits for n seconds, then prints out n, meaning it takes 1 second to print out "1", 2 seconds to print out "2", 100 seconds to print out "100". This means that for the most part, the numbers are printed out in the order of their size, thus "sorting" the arguments.

The complexity of this algorithm in a perfect world is O(max(args)), as it will take max(args) seconds to print out the biggest arg. In reality, the complexity is O(n^2 + max(args)), because maintaining multiple background processes relies on the operating system to manage the context switching and prioritization of the processes, and so the algorithm basically outsources the actual sorting to the kernel.

Be sure to share this algorithm to everyone! :Thumbs-Up:

Reply

Most beautiful algorithm? Its ART #2
Hello

Ive lately been really interested into algorithms and decided to show you guys a cool and simple algorithm.

Sleep sort algorithm

Original code: http://dis.4chan.org/read/prog/1295544154


#!/bin/bash
function f() {
sleep "$1"
echo "$1"
}
while [ -n "$1" ]
do
f "$1" &
shift
done
wait
----------------------------------------------------------------------

Now thats what I call art. :Laughing:

Sleep sort is a joke sorting algorithm that became popular on the 4chan board /prog/ [1]. The pseudocode for sleep sort is:

----------------------------------------------------------------------

procedure printNumber(n)
sleep n seconds
print n
end

for arg in args
run printNumber(arg) in background
end
wait for all processes to finish

----------------------------------------------------------------------

Ha-ha! Hilarious.

In other words, what it does is that sleep sort spawns off one process for each argument. Each process waits for n seconds, then prints out n, meaning it takes 1 second to print out "1", 2 seconds to print out "2", 100 seconds to print out "100". This means that for the most part, the numbers are printed out in the order of their size, thus "sorting" the arguments.

The complexity of this algorithm in a perfect world is O(max(args)), as it will take max(args) seconds to print out the biggest arg. In reality, the complexity is O(n^2 + max(args)), because maintaining multiple background processes relies on the operating system to manage the context switching and prioritization of the processes, and so the algorithm basically outsources the actual sorting to the kernel.

Be sure to share this algorithm to everyone! :Thumbs-Up:

Reply







Users browsing this thread: 1 Guest(s)