Genius sorting algorithm-sleep sorting

Source: Internet
Author: User

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?
  1. Public class sleepsort {
  2. Public static void main (string [] ARGs ){
  3. Int [] ints = {, 5 };
  4. Sortthread [] sortthreads = new sortthread [ints. Length];
  5. For (INT I = 0; I <sortthreads. length; I ++ ){
  6. Sortthreads [I] = new sortthread (ints [I]);
  7. }
  8. For (INT I = 0; I <sortthreads. length; I ++ ){
  9. Sortthreads [I]. Start ();
  10. }
  11. }
  12. }
  13. Class sortthread extends thread {
  14. Int MS = 0;
  15. Public sortthread (int ms ){
  16. This. Ms = MS;
  17. }
  18. Public void run (){
  19. Try {
  20. Sleep (MS * 10 + 10 );
  21. } Catch (interruptedexception e ){
  22. // Todo auto-generated Catch Block
  23. E. printstacktrace ();
  24. }
  25. System. Out. println (MS );
  26. }
  27. }
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?

  1. <? PHP
  2. $ PIDs = array ();
  3. For ($ I = 1; $ I <$ argc; $ I ++)
  4. {
  5. If ($ pid = pcntl_fork () = 0)
  6. {
  7. $ Sleep = intval ($ argv [$ I]);
  8. Sleep ($ sleep );
  9. Echo $ sleep. "\ n ";
  10. Exit ();
  11. }
  12. Else if ($ pid =-1)
  13. {
  14. Die ();
  15. }
  16. Else
  17. {
  18. $ PIDs [] = $ PID;
  19. }
  20. }
  21. Foreach ($ PIDs as $ PID)
  22. Pcntl_waitpid ($ PID, $ status );
  23. ?>
  24. 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)InsteadnIt 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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.