diff options
| author | Paul Buetow <paul@buetow.org> | 2023-04-09 22:25:37 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2023-04-09 22:25:37 +0300 |
| commit | 66aae6a827fa58eb753f51acb0a4205608f9bf09 (patch) | |
| tree | 9d84a83d2bc9c7413b28833604c1624c0bbc903b /sort/sleep.go | |
| parent | 198ceb7a3c1a6cb4416e861320c4a7c8033f2deb (diff) | |
add sleep sort
Diffstat (limited to 'sort/sleep.go')
| -rw-r--r-- | sort/sleep.go | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/sort/sleep.go b/sort/sleep.go new file mode 100644 index 0000000..3ca45b2 --- /dev/null +++ b/sort/sleep.go @@ -0,0 +1,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 +} |
