转到 : channel is necessary in this case?

标签 go quicksort channel

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/

相关文章:

elixir - 如何从 Phoenix Controller 向 channel 广播消息?

go - 为什么不能在赋值中使用 (type func(string)) 作为 type func(interface{})

installation - 在 OSX 上安装 go 调试器

go - 关于 "sudo go run main.go"

go - 共享内存与 Go channel 通信

c - 在 QuickSort 中交换两个变量时会发生奇怪的事情

java - 快速排序困惑

language-agnostic - O(N log N) 复杂度 - 类似于线性?

go - 在 Go 中通过多个 channel 广播一个 channel

go - Go 中如何比较来自 channel 的值