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

wty4427300 · · 1870 次点击
package main import ( "fmt" "math/rand" "math" ) 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 < exp; i++ { go work(i) } boot(0) result := <- arrChan[len(arrChan)-1] for i := range result { fmt.Println(i) } } func initArr(num int) { arr = make([]int, num) for i:=0; i < num; i++ { arr[i]=rand.Intn(10000) } arrChan = make([]chan chan int, exp+1) for i:=0; i < exp+1; i++{ arrChan[i] = make(chan chan int, 2) } } func boot(index int) { for i := 0; i < len(arr); i += 2 { ch := make(chan int, int64(math.Pow(2, float64(index+1)))) arrChan[index+1] <- ch min := i max := i+1 if arr[min] > arr[max] { max = i min = i+1 } ch <- arr[min] ch <- arr[max] close(ch) } } func work(index int) { //fmt.Println("work:", index) in := arrChan[index] for { ch1, ok := <- in if !ok { close(arrChan[index+1]) break } ch2 := <- in ch := make(chan int, int64(math.Pow(2, float64(index+1)))) arrChan[index+1] <- ch var v1, v2 int var v1min, init, v1closed, v2closed bool for { if !init { init = true v1 = <- ch1 v2 = <- ch2 if v1 < v2 { v1min = true } } else { if v1min { v1, ok = <-ch1 if !ok { v1closed = true v1min = false } else if !v2closed && v1 > v2 { v1min = false } } else { v2, ok = <- ch2 if !ok { v2closed = true v1min = true } else if !v1closed && v1 < v2 { v1min = true } } } if v1closed && v2closed { close(ch) break } if v1min { ch <- v1 } else { ch <- v2 } } } }
#1
更多评论
```go package main import ( "fmt" "math/rand" "math" ) 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 < exp; i++ { go work(i) } boot(0) result := <- arrChan[len(arrChan)-1] for i := range result { fmt.Println(i) } } func initArr(num int) { arr = make([]int, num) for i:=0; i < num; i++ { arr[i]=rand.Intn(10000) } arrChan = make([]chan chan int, exp+1) for i:=0; i < exp+1; i++{ arrChan[i] = make(chan chan int, 2) } } func boot(index int) { for i := 0; i < len(arr); i += 2 { ch := make(chan int, int64(math.Pow(2, float64(index+1)))) arrChan[index+1] <- ch min := i max := i+1 if arr[min] > arr[max] { max = i min = i+1 } ch <- arr[min] ch <- arr[max] close(ch) } } func work(index int) { //fmt.Println("work:", index) in := arrChan[index] for { ch1, ok := <- in if !ok { close(arrChan[index+1]) break } ch2 := <- in ch := make(chan int, int64(math.Pow(2, float64(index+1)))) arrChan[index+1] <- ch var v1, v2 int var v1min, init, v1closed, v2closed bool for { if !init { init = true v1 = <- ch1 v2 = <- ch2 if v1 < v2 { v1min = true } } else { if v1min { v1, ok = <-ch1 if !ok { v1closed = true v1min = false } else if !v2closed && v1 > v2 { v1min = false } } else { v2, ok = <- ch2 if !ok { v2closed = true v1min = true } else if !v1closed && v1 < v2 { v1min = true } } } if v1closed && v2closed { close(ch) break } if v1min { ch <- v1 } else { ch <- v2 } } } } ```
#2
显示出来的内容和原始内容不一样,麻烦管理员看看
#3