An example analysis of sleep sorting algorithm in go language

  • 2020-05-27 05:48:45
  • OfStack

In this paper, the example of go language sleep sorting algorithm. Share with you for your reference. The specific analysis is as follows:

Sleep sorting algorithm was invented by a talented programmers, the idea is very simple, is aimed at the different number in the array open multiple threads, each thread according to the size of the number of sleep, natural sleep longer, the greater the number, ha ha, funny, this algorithm looks ridiculous, but it's actually very talented, it can make full use of multi-core cpu to calculate.

package main
import (
    "fmt"
    "time"
)
func main() {
    tab := []int{1, 3, 0, 5}
 
    ch := make(chan int)
    for _, value := range tab {
        go func(val int){
            time.Sleep( int64(val)*10000000 )
            fmt.Println(val)
            ch <-val
        }(value)
    }
    for _ = range tab {
         <-ch
    }
}

I hope this article has been helpful to your programming of Go language.


Related articles: