问一个多路归并的优化问题拜托各位大佬了

wty4427300 · · 1870 次点击
<a href="/user/azureki" title="@azureki">@azureki</a> 6核有超线程支持变成12逻辑cpu
#6
更多评论
package main import ( &#34;fmt&#34; &#34;math/rand&#34; &#34;math&#34; ) var arr []int var arrChan []chan chan int var exp int = 12 //sort 2**exp integers using exp goroutines func main() { initArr(int(math.Pow(2, float64(exp)))) for i:=1; i &lt; exp; i++ { go work(i) } boot(0) result := &lt;- arrChan[len(arrChan)-1] for i := range result { fmt.Println(i) } } func initArr(num int) { arr = make([]int, num) for i:=0; i &lt; num; i++ { arr[i]=rand.Intn(10000) } arrChan = make([]chan chan int, exp+1) for i:=0; i &lt; exp+1; i++{ arrChan[i] = make(chan chan int, 2) } } func boot(index int) { for i := 0; i &lt; len(arr); i += 2 { ch := make(chan int, int64(math.Pow(2, float64(index+1)))) arrChan[index+1] &lt;- ch min := i max := i+1 if arr[min] &gt; arr[max] { max = i min = i+1 } ch &lt;- arr[min] ch &lt;- arr[max] close(ch) } } func work(index int) { //fmt.Println(&#34;work:&#34;, index) in := arrChan[index] for { ch1, ok := &lt;- in if !ok { close(arrChan[index+1]) break } ch2 := &lt;- in ch := make(chan int, int64(math.Pow(2, float64(index+1)))) arrChan[index+1] &lt;- ch var v1, v2 int var v1min, init, v1closed, v2closed bool for { if !init { init = true v1 = &lt;- ch1 v2 = &lt;- ch2 if v1 &lt; v2 { v1min = true } } else { if v1min { v1, ok = &lt;-ch1 if !ok { v1closed = true v1min = false } else if !v2closed &amp;&amp; v1 &gt; v2 { v1min = false } } else { v2, ok = &lt;- ch2 if !ok { v2closed = true v1min = true } else if !v1closed &amp;&amp; v1 &lt; v2 { v1min = true } } } if v1closed &amp;&amp; v2closed { close(ch) break } if v1min { ch &lt;- v1 } else { ch &lt;- v2 } } } }
#1
```go package main import ( &#34;fmt&#34; &#34;math/rand&#34; &#34;math&#34; ) var arr []int var arrChan []chan chan int var exp int = 12 //sort 2**exp integers using exp goroutines func main() { initArr(int(math.Pow(2, float64(exp)))) for i:=1; i &lt; exp; i++ { go work(i) } boot(0) result := &lt;- arrChan[len(arrChan)-1] for i := range result { fmt.Println(i) } } func initArr(num int) { arr = make([]int, num) for i:=0; i &lt; num; i++ { arr[i]=rand.Intn(10000) } arrChan = make([]chan chan int, exp+1) for i:=0; i &lt; exp+1; i++{ arrChan[i] = make(chan chan int, 2) } } func boot(index int) { for i := 0; i &lt; len(arr); i += 2 { ch := make(chan int, int64(math.Pow(2, float64(index+1)))) arrChan[index+1] &lt;- ch min := i max := i+1 if arr[min] &gt; arr[max] { max = i min = i+1 } ch &lt;- arr[min] ch &lt;- arr[max] close(ch) } } func work(index int) { //fmt.Println(&#34;work:&#34;, index) in := arrChan[index] for { ch1, ok := &lt;- in if !ok { close(arrChan[index+1]) break } ch2 := &lt;- in ch := make(chan int, int64(math.Pow(2, float64(index+1)))) arrChan[index+1] &lt;- ch var v1, v2 int var v1min, init, v1closed, v2closed bool for { if !init { init = true v1 = &lt;- ch1 v2 = &lt;- ch2 if v1 &lt; v2 { v1min = true } } else { if v1min { v1, ok = &lt;-ch1 if !ok { v1closed = true v1min = false } else if !v2closed &amp;&amp; v1 &gt; v2 { v1min = false } } else { v2, ok = &lt;- ch2 if !ok { v2closed = true v1min = true } else if !v1closed &amp;&amp; v1 &lt; v2 { v1min = true } } } if v1closed &amp;&amp; v2closed { close(ch) break } if v1min { ch &lt;- v1 } else { ch &lt;- v2 } } } } ```
#2