熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

天才排序算法:睡眠排序

2022-06-13   來源: Java核心技術 

  今天看到一個帖子帖名叫《Genius sorting algorithm: Sleep sort》看過之後感覺雖然實用價值不高但挺受啟發的帖上來給大家分享下
    Man am I a genius Check out this sorting algorithm I just invented
    朋友我真是個天才快來看看我剛發明的排序算法
    #!/bin/bash
    function f() {
    sleep $
    echo $
    }
    while [ n $ ]
    do
    f $ &
    shift
    done
    wait
    example usage:
    /sleepsortbash
    再帖幾個其他常用版本以便擅長不同語言的朋友閱讀
JAVA版
    [java]
    public class SleepSort {
    public static void main(String[] args) {
    int[] ints = {};
    SortThread[] sortThreads = new SortThread[intslength];
    for (int i = ; i < sortThreadslength; i++) {
    sortThreads[i] = new SortThread(ints[i])
    }
    for (int i = ; i < sortThreadslength; i++) {
    sortThreads[i]start()
    }
    }
    }
    class SortThread extends Thread{
    int ms = ;
    public SortThread(int ms){
    thisms = ms;
    }
    public void run(){
    try {
    sleep(ms*+
    } catch (InterruptedException e) {
    // TODO Autogenerated catch block
    eprintStackTrace()
    }
    Systemoutprintln(ms)
    }
    }

  PHP版
    [php]
    <?php
    $pids = array()
    for ($i=; $i<$argc; $i++)
    {
    if (($pid = pcntl_fork()) ==
    {
    $sleep = intval($argv[$i])
    sleep($sleep)
    echo $sleep\n;
    exit()
    }
    else if ($pid ==
    {
    die()
    }
    else
    {
    $pids[] = $pid;
    }
    }
    foreach($pids as $pid)
    pcntl_waitpid($pid $status)
    ?>
    php sleepsortphp
JS版
    [javascript]
    // Javascript
    function lazySort(list callback) {
    var result = [];
    listforEach(function(i) {
    setTimeout(function() {
    resultpush(i)
    if(resultlength == listlength) {
    callback(result)
    }
    } i)
    })
    }
    lazySort([] alert)
Ruby版
    [ruby]
    ARGVeach { |e| fork { sleep(eto_f/ puts e } }
    還有很多冷門語言版本感興趣朋友可以點文後的鏈接
    該貼有+回復我挑幾個有趣的回復分享下
    路人A:
    Oh god it works
    But I dont like to wait seconds to sort
    哦春哥它居然能用但我不想用秒去排(
    路人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 ?
    If you slept exp(n) instead of n it could easily include negative integers too!
    排 怎麼辦?如果你睡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 (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 lowpower 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 youre a genius!
    我由衷的不同意那些低估sleepsort這個天才算法的舉動許多人可能忽略了一個重點那就是傳統的排序只能利用一個核心而sleepsort有這個能力充分利用可以做大量並行計算的環境
    在每個線程中給出你幾乎不需要計算的部分你可以用低性能CPU搞定它們所以事實上這是一個綠色計算算法
    還有我提到的這個方法能在雲端運行不?
    總之你是個天才!
    路人H:
    pretty fucking cool
    太TMD的cool了!


From:http://tw.wingwit.com/Article/program/Java/hx/201311/25620.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.