Created
December 29, 2013 20:29
-
-
Save mattyw/8174462 to your computer and use it in GitHub Desktop.
Sleep sort using channels:
http://archives.cazzaserver.com/SleepSortWiki/SleepSort.html
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
// Sleep sort | |
// http://archives.cazzaserver.com/SleepSortWiki/SleepSort.html | |
package main | |
import ( | |
"fmt" | |
"time" | |
) | |
func sorter(c chan int, value int) { | |
time.Sleep(time.Millisecond * time.Duration(value)) | |
c <- value | |
} | |
func main() { | |
unsorted := []int{2, 1, 4, 3} | |
c := make(chan int) | |
for _, x := range unsorted { | |
go sorter(c, x) | |
} | |
sorted := []int{} | |
for _ = range unsorted { | |
v := <-c | |
sorted = append(sorted, v) | |
} | |
fmt.Println(sorted) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment