http://play.golang.org/p/Xn3Qw7xAi3
很难理解 channel 。
我有
func main() {
in := make(chan int)
out := make(chan int)
go QuickSort(in, out)
for i := 0; i < 100; i++ {
in <- rand.Intn(1000)
}
close(in)
for i := range out {
fmt.Println(i)
}
}
这使得名为 in、out 和 goroutine 的两个 channel 成为函数 Quicksort。
1. QuickSort 如何将 in 和 out 作为参数?它是否从下面的线路接收?
in <- rand.Intn(1000)
2。 这种情况下使用 channel 是最佳的吗?动态地接收值看起来非常整洁……如果没有 channel 进行排序会有什么不同?这种情况更快?
最佳答案
I wrote the original version of that!
我认为我的原始文章回答了您的第二个问题...
Just for fun - a channel based quicksort.
Interesting that you can make a quicksort without being able to index your input. It may be O(n log n) for comparisons but it is O(n) for channels and go routines so perhaps not the most efficient quicksort ever ;-)
It also has the worst case complexity O(n²) if you feed it sorted input, so don't do that!
这确实有点有趣 - 但它使用了大量的 channel 和 goroutines,这将使其比传统的快速排序更慢并且使用更多的内存。
关于转到 : channel is necessary in this case?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19914133/