Skip to content

Instantly share code, notes, and snippets.

@akimach
Last active November 12, 2017 11:31
Show Gist options
  • Save akimach/f754daf9ca4b27757d04f876dac31b31 to your computer and use it in GitHub Desktop.
Save akimach/f754daf9ca4b27757d04f876dac31b31 to your computer and use it in GitHub Desktop.
Sleep sort
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