Last active
November 12, 2017 11:31
-
-
Save akimach/f754daf9ca4b27757d04f876dac31b31 to your computer and use it in GitHub Desktop.
Sleep sort
This file contains hidden or 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 | |
/* | |
$ go run main.go | |
[3 9 2 4 1] | |
[1 2 3 4 9] | |
*/ | |
import ( | |
"fmt" | |
"time" | |
) | |
func main() { | |
X := [5]int64{3, 9, 2, 4, 1} | |
var Y []int64 | |
ch := make(chan int64) | |
for _, v := range X { | |
go sleep(ch, v) | |
} | |
for { | |
if len(Y) == len(X) { | |
break | |
} | |
c := <-ch | |
Y = append(Y, c) | |
time.Sleep(time.Millisecond * 1) | |
} | |
fmt.Println(X) | |
fmt.Println(Y) | |
} | |
func sleep(ch chan int64, msec int64) { | |
time.Sleep(time.Millisecond * time.Duration(msec)) | |
ch <- msec | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment