Http://blog.csdn.net/wang6279026/article/details/9527415
This event originated from a diaosi publishing a sorting algorithm with a time complexity of O (n). The URL is as follows: http://dis.4chan.org/read/prog/1295544264.
Although the use value is not very high, it is impossible to find such a method to become a genius.
Its basic idea is to sort a group of data based on the CPU scheduling algorithm. There cannot be negative values, in the thread, sleep is 10 times more than 10 times, not because sleep is as big as its value. When the value is too small, the error is too large, if the sleep time is less than the output time, there will be incorrect output results.
Below are several versions of this sorting algorithm.
#!/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
[Java]
View plaincopyprint?
- Public class sleepsort {
- Public static void main (string [] ARGs ){
- Int [] ints = {, 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 );
- }
- }
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]
View plaincopyprint?
- <? 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
<?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
Below is the reprinted
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!