blob: 3ca45b2d82bba9ca154cc410ba84475ed59021bd (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
package sort
import (
"codeberg.org/snonux/algorithms/ds"
"sync"
"time"
)
func Sleep[V ds.Integer](a ds.ArrayList[V]) ds.ArrayList[V] {
sorted := ds.NewArrayList[V](len(a))
numCh := make(chan V)
var wg sync.WaitGroup
wg.Add(len(a))
go func() {
wg.Wait()
close(numCh)
}()
for _, num := range a {
go func(num V) {
defer wg.Done()
time.Sleep(time.Duration(num) * time.Second)
numCh <- num
}(num)
}
for num := range numCh {
sorted = append(sorted, num)
}
return sorted
}
|