算法---天才排序算法---睡眠排序

這個事件起源於一個屌絲髮表了一個時間複雜度爲O(n)的排序算法,這個網址以下:http://dis.4chan.org/read/prog/1295544154你們有興趣的能夠看看。php

雖然使用價值不是很高,可是能找到這麼一個方法,成爲天才也不爲過。java

它的基本思想是,主要是根據CPU的調度算法實現的,對一組數據進行排序,不能存在負數值,這個數是多大,那麼就在線程裏睡眠它的10倍再加10,不是睡眠和它的數值同樣大的緣由是,當數值過小時,偏差太大,睡眠的時間不比輸出的時間少,那麼就會存在不正確的輸出結果。
算法

下面寫幾個此排序算法的版本bash

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


example usage:
./sleepsort.bash 5 3 6 3 6 3 1 4 7
ide

 

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
$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

 


下面這個是轉載的 性能

該貼有1000+回覆,我挑幾個有趣的回覆分享下:this


路人A:spa

Oh god, it works.

But I don't like to wait 218382 seconds to sort '(0 218382)

線程

哦,春哥,它竟然能用,但我不想用218382秒去排(0 218382)設計


路人B:

If the difference between any two of the numbers is too small, race conditions will fuck you up the ass.

若是兩個數之間的差距過小,競態條件就要爆你菊花了。


路人C:

What about 
./sleepsort -1 -2 -3 ?

If you slept exp(n) instead of n it could easily include negative integers too!

排-1 -2 -3怎麼辦?若是你睡exp(n)而不是n,它就能包含負數了。


路人D:

Someone email this to Knuth

你能夠給Knuth發郵件了


路人E:

I think thats brilliant :)

Would be fun to design a hardware sorter, based on this..

這招挺高,能夠根據這個設計一個硬件排序器


路人F:

This has a best case O(n) and an infinity high worst case. (because its 0(n * Constant) and the constant could be much greater than n)

它有一個最好的O(n)的時間複雜度和一個無窮大的最壞複雜度,由於這個常數可能比n大的多的多


路人G:

I heartily disagree with all the attempts to downplay the brilliance of the sleep sort algorithm. Many 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!

我由衷的不一樣意那些低估sleepsort這個天才算法的舉動,許多人可能忽略了一個重點那就是傳統的排序只能利用一個核心,而sleepsort有這個能力充分利用能夠作大量並行計算的環境。

在每一個線程中給出你幾乎不須要計算的部分,你能夠用低性能CPU搞定它們,因此事實上,這是一個「綠色計算」算法。

還有我提到的這個方法能在雲端運行不?

總之,你是個天才!


路人H:

pretty fucking cool !

真是太TM的cool了!

相關文章
相關標籤/搜索