Sorting Algorithm Sleep Sort

Source: Internet
Author: User

Sorting algorithms seem to be the algorithms that programmers learn the most in programming, or they may be algorithms that algorithm researchers like most. There are many sorting algorithms, such as bubble, insert, selection, heap, fast, and merge. (You can refer to the previous articles on this site: Visual sorting and comparison of sorting algorithms, displays the python of the sorting process.) Here we will introduce a sorting algorithm named Sleep Sort for "giant NB.

For more information, see the following code (written using Shell scripts)

1234567891011 #! /Bin/bash function f () {sleep "$1" echo "$1"} while [-n "$1"] do f "$1" & shiftdonewait

The usage is as follows:

./Sleepsort. bash 5 3 6 3 6 3 1 4 7

I believe you can try this script, and after you try it, you will surely say, "I have wiped it. It's a TMD sort !", I still don't want to explain this piece of code. Too much explanation will be less direct than the code, and the interpretation will affect the NB-ability of this sorting algorithm. I just want to say-this is the multi-thread and multi-process sorting that is just being processed. Our Bogo sorting is also eclipsed.

Next, we need to analyze this algorithm --

1) Let's analyze the complexity of an algorithm for this program. It's too simple, not O (the maximum number of seconds. So if such a series appears, it will be a nightmare-2 1 4 3 2 1 99999999

2) This sorting is good, but there is a bug in negative or floating-point numbers. The negative number solution is: x/2 + MaxInt/2 (the time may be quite long, but it still works ). For floating point numbers, see the following code.

1234567891011 #! /Bin/bash function f () {sleep $ (echo "($2-1) + $1/10 ^ $2" | bc-l) echo "$1"} while [-n "$1"] do f "$1" $ (echo-n "$1" | wc-c) & shiftdonewait

3) let's take a look at the implementation of various language versions.
Java

123456789101112131415161718192021222324252627 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 );}}

Javascript

123456789 function sleepsort () {for (var I = 0, il = arguments. length; I <il; I ++) {(function (args, index) {setTimeout (function () {document. body. innerHTML + = args [index] +, ;}, args [index]) ;}( arguments, I ));}};

Brainfuck (for details about this language, refer to this Article)

, >,> ++ [<------>-]
<[> [> + <-]> [<+,>,> ++ [<------>-]
<[---------- [++> ----------] ++
> [> + <-]> [<+>-] <-]> ++ [<++ +++>-] <...> -] <-]
, ---------- [--------------------., ----------]
, --- <+> ------- [----------------------., ----------]
> ---------- [++> ----------] ++
> ++ [<++>-] <---------- [++> ----------] ++ ++
.>. ---------- [++> ----------] ++
>+>+ <-]> [<+>-] <-]> [> [> + <-]> [<---------- [++> ----------] ++
> ++,>,> ++, ++ [<------>-]
<

(Full text)

 

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.