Skip to content

Instantly share code, notes, and snippets.

@joshlf
Created November 21, 2013 16:18
Show Gist options
  • Save joshlf/7584713 to your computer and use it in GitHub Desktop.
Save joshlf/7584713 to your computer and use it in GitHub Desktop.
Sleepsort (golang)
package main
import (
"fmt"
"math/rand"
"time"
)
func main() {
ints := rand.Perm(100)
fmt.Println(ints)
SleepSort(ints)
fmt.Println(ints)
}
func SleepSort(ints []int) {
c := make(chan int)
for _, i := range ints {
go func(i int) {
time.Sleep(time.Duration(i) * time.Millisecond)
c <- i
}(i)
}
for idx, _ := range ints {
ints[idx] = <-c
}
}
@joshlf
Copy link
Author

joshlf commented Nov 21, 2013

Run this code at play.golang.org.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment