Created
April 2, 2019 09:39
-
-
Save tpaschalis/d433f05c0b81b33af3c9e937a0f2ef8a to your computer and use it in GitHub Desktop.
"Comparison-free sorting of positive integers" aka "Sleepsort" in Go, using Goroutines
This file contains 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" | |
"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