Last active
April 16, 2019 21:58
-
-
Save Spiikesan/5d94f6def569c425daef3c63ad74d55d to your computer and use it in GitHub Desktop.
Sort algorithm with no condition in golang.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"time" | |
) | |
func sleepSort(tab []int) ([]int) { | |
out := make([]int, len(tab)) | |
waiter := make(chan int, len(tab)) | |
ssort := func (i int) { | |
time.Sleep(time.Nanosecond * time.Duration(i)) | |
waiter <- i | |
} | |
for _, i := range tab { | |
go ssort(i) | |
} | |
for n, _ := range tab { | |
out[n] = <-waiter | |
} | |
return out | |
} | |
func main() { | |
res := sleepSort([]int{8, 42, 38, 111, 2, 39, 1}) | |
for _, n := range res { | |
fmt.Println(n) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment