Created
November 21, 2013 16:18
-
-
Save joshlf/7584713 to your computer and use it in GitHub Desktop.
Sleepsort (golang)
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 | |
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 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Run this code at play.golang.org.