Skip to content

Instantly share code, notes, and snippets.

@Spiikesan
Last active April 16, 2019 21:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Spiikesan/5d94f6def569c425daef3c63ad74d55d to your computer and use it in GitHub Desktop.
Save Spiikesan/5d94f6def569c425daef3c63ad74d55d to your computer and use it in GitHub Desktop.
Sort algorithm with no condition in golang.
package main
import (
"fmt"
"time"
)
func sleepSort(tab []int) ([]int) {
out := make([]int, len(tab))
waiter := make(chan int, len(tab))
ssort := func (i int) {
time.Sleep(time.Nanosecond * time.Duration(i))
waiter <- i
}
for _, i := range tab {
go ssort(i)
}
for n, _ := range tab {
out[n] = <-waiter
}
return out
}
func main() {
res := sleepSort([]int{8, 42, 38, 111, 2, 39, 1})
for _, n := range res {
fmt.Println(n)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment