Sorting algorithms/Sleep sort: Difference between revisions
Content added Content deleted
No edit summary |
(Add additional Go implementation using WaitGroups instead of channels) |
||
Line 743: | Line 743: | ||
9 |
9 |
||
</pre> |
</pre> |
||
=== Using sync.WaitGroup === |
|||
<lang go>package main |
|||
import ( |
|||
"fmt" |
|||
"log" |
|||
"os" |
|||
"strconv" |
|||
"sync" |
|||
"time" |
|||
) |
|||
func main() { |
|||
var wg sync.WaitGroup |
|||
wg.Add(len(os.Args[1:])) |
|||
for _,i := range os.Args[1:] { |
|||
x, err := strconv.ParseUint(i, 10, 64) |
|||
if err != nil { |
|||
log.Println(err) |
|||
} |
|||
wg.Add(1) |
|||
go func(i uint64, wg *sync.WaitGroup) { |
|||
defer wg.Done() |
|||
time.Sleep(time.Duration(i) * time.Second) |
|||
fmt.Println(i) |
|||
}(x, &wg) |
|||
} |
|||
wg.Wait() |
|||
}</lang> |
|||
Usage and output are the same as the version using channels. Note that the original version would sleep for increments of 1 full second, so I made my code do the same. |
|||
=={{header|Groovy}}== |
=={{header|Groovy}}== |