所以我在go中实现了快速排序算法。我用go test
进行了测试,效果很好。现在我想让它并发并检查计算时间的差异。算法是这样的:并发快速排序
package mysort
import (
"math/rand"
)
// ConcurrentPartition - ConcurrentQuicksort function for partitioning the array (randomized choice of a pivot)
func ConcurrentPartition(A []int, p int, r int) int {
index := rand.Intn(r-p) + p
pivot := A[index]
A[index] = A[r]
A[r] = pivot
x := A[r]
j := p - 1
i := p
for i < r {
if A[i] <= x {
j++
tmp := A[j]
A[j] = A[i]
A[i] = tmp
}
i++
}
temp := A[j+1]
A[j+1] = A[r]
A[r] = temp
return j + 1
}
// ConcurrentQuicksort - a concurrent version of a quicksort algorithm
func ConcurrentQuicksort(A []int, p int, r int) {
if p < r {
q := ConcurrentPartition(A, p, r)
ConcurrentQuicksort(A, p, q-1)
ConcurrentQuicksort(A, q+1, r)
}
}
每ConcurrentQuicksort
是实际工作中的独立默认情况下,作为其divide and conquer
理念的构建。所以,我做的唯一的事情是添加go
关键字每次递归调用之前,就像这样:
go ConcurrentQuicksort(A, p, q-1)
go ConcurrentQuicksort(A, q+1, r)
我didnt't工作。正如我所见,它只是一个数组,甚至没有一次称为递归快速排序。所以我添加time
进口和这一行:
time.Sleep(time.Second * 2)
(那if
后)
和它的工作。所以我想这需要时间来完成所有的操作。我现在怎样才能不用等待呢?我应该使用频道,还是以不同的方式调用功能?
@Edit添加了这个:
func ConcurrentQuicksort(A []int, p int, r int, wg *sync.WaitGroup) {
if p < r {
q := ConcurrentPartition(A, p, r)
wg.Add(2)
go ConcurrentQuicksort(A, p, q-1, wg)
go ConcurrentQuicksort(A, q+1, r, wg)
}
}
你只是启动goroutines,但你必须等到两者都完成他们的工作:(子)数组不会被排序,直到两个goroutines完成。这里没有魔法。 – Volker
在基本情况下,您需要告诉WaitGroup您已完成等待。 – Pita