Today I saw a post named genius Sorting Algorithm: Sleep sort. After reading it, I feel that although the practical value is not high, it is very enlightening. The point is that it is so happy.
Author:
Man, am I a genius. Check out this sorting algorithm I just already Ted.
Friend, I am a genius. Come and see the 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
I will post several other frequently-used versions for users who are good at reading different languages:
Java version:
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); }}
PHP version:
<?php$pids = array();for ($i=1; $i<$argc; $i++){ if (($pid = pcntl_fork()) == 0) { $sleep = intval($argv[$i]); sleep($sleep); echo $sleep."\n"; exit(); } else if ($pid == -1) { die(); } else { $pids[] = $pid; }}foreach($pids as $pid) pcntl_waitpid($pid, $status);?>php sleepsort.php 1 3 5 6 2
JS version:
// Javascriptfunction lazySort(list, callback) { var result = []; list.forEach(function(i) { setTimeout(function() { result.push(i); if(result.length == list.length) { callback(result); } }, i); });}lazySort([4,5,7,1,2,4,5], alert);
Ruby version:
ARGV.each { |e| fork { sleep(e.to_f/1000); puts e } }
There are also many versions of unpopular languages. If you are interested, you can click the link after the article.
This post has more than 1000 replies. I will pick up some interesting replies and share them with you:
Passers-by:
Oh God, it works.
But I don't like to wait 218382 seconds to sort '(0 218382)
Oh, Chun Ge, it can be used, but I don't want to use 218382 seconds (0 218382)
Passers-by B:
If the difference between any two of the numbers is too small, race conditions will fuck you up the ass.
If the gap between the two numbers is too small, the race condition will blow you up.
Passers-by C:
What about
./sleepsort -1 -2 -3
?
If you sleptexp(n)
Insteadn
It cocould easily include negative
Integers too!
-1-2-3 what should I do? If you sleep exp (n) instead of N, it can contain negative numbers.
Passers-by D:
Someone email this to knuth
You can send an email to knuth.
Passers-by E:
I think thats brilliant
Wocould be fun to design a hardware Sorter, based on this ..
This is very high. You can design a hardware sequencer based on this.
Passers-by F:
This has a best case O (N) and an infinity high worst case. (because its 0 (N * constant) and the constant cocould be much greater than N)
It has the best O (n) time complexity and the worst complexity of an infinity, because this constant may be much larger than N.
Passers-by G:
I heartily disagree with all the attempts to downplay the brilliance of the sleep sort algorithm. Lost of you have missed the important
Point that while traditional sorting algorithms can only utilize one core, sleep sort has the capacity to use the full power of a massively parallel execution environment.
Given that you need nearly no computing in each of the threads, You can implement them using low-power CPUs, so this is in fact a green computing algorithm.
Oh, and did I mention that the algorithm can also run inside a cloud ...?
Sure, you're a genius!
I sincerely disagree with those actions that underestimate sleepsort's genius algorithm. Many people may ignore one key point, that is, traditional sorting can only use one core, sleepsort has the ability to make full use of a large number of parallel computing environments.
In each thread, you can use a low-performance CPU to handle the components that you almost do not need to compute. In fact, this is a "Green computing" algorithm.
Can this method be run on the cloud?
In short, you are a genius!
Passers-by H:
Pretty fucking cool!
It's really cool!
Original post address: http://dis.4chan.org/read/prog/1295544154
========================================================== ========================================================== ============================
Author: Nash _ Welcome to repost. sharing with others is the source of progress!
Reprinted Please retain the original address: http://blog.csdn.net/nash_/article/details/8514088
========================================================== ========================================================== ==============================