Genius Sorting Algorithm: Sleep sorting

Source: Internet
Author: User

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. I will share it with you. Poster: Man, am I a genius. Check out this sorting algorithm I just already Ted. a friend, I am really a genius. Come and see the sorting algorithm I just invented. #! /Bin/bashfunction f () {sleep "$1" echo "$1"} while [-n "$1"] do f "$1" & shiftdonewait example usage :. /sleepsort. bash 5 3 6 3 6 3 1 4 7 post a few other common versions, so that friends who are good at different languages can read: JAVA: [java] 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);} PHP: [php] <? 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: [javascript] // Javascript function lazySort (list, callback) {var result = []; list. forEach (function (I) {setTimeout (function () {result. push (I); if (result. length = list. length) {callback (result) ;}}, I) ;});} lazySort ([, 1, 5], alert); Ruby: [ruby] ARGV. each {| e | fork {sleep (e. to_f/1000); puts e} There are many other popular language versions. If you are interested, you can click the link after the article. The post has more than 1000 replies. I will pick up a few interesting replies and share them: Road Person A: 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 to sort (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 slept exp (n) instead of n 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 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 an infinite worst complexity, because this constant may be much larger than n for multiple passers-by G: I heartily disagree with all the attempts to downpl Ay the brilliance of the sleep sort algorithm. failed 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 paralleexecution 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 COMPUTIN G 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! Passerby H: pretty fucking cool is too TMD cool!

Related Article

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.