Skip to content

Instantly share code, notes, and snippets.

@tpaschalis
Created April 2, 2019 09:39
Show Gist options
  • Save tpaschalis/d433f05c0b81b33af3c9e937a0f2ef8a to your computer and use it in GitHub Desktop.
Save tpaschalis/d433f05c0b81b33af3c9e937a0f2ef8a to your computer and use it in GitHub Desktop.
"Comparison-free sorting of positive integers" aka "Sleepsort" in Go, using Goroutines
package main
import (
"fmt"
"os"
"sync"
"time"
"strconv"
)
func main() {
args := os.Args
var wg sync.WaitGroup
for _, arg := range args[1:] {
wg.Add(1) // Add a wait group for each of the arguments
n, _ := strconv.Atoi(arg)
go func(n int) {
defer wg.Done()
time.Sleep(time.Duration(n) * time.Second)
fmt.Println(n)
}(n)
}
wg.Wait() // Wait for all goroutines to finish before continuing
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment