Algorithm Python中的并行快速排序

Algorithm Python中的并行快速排序,algorithm,parallel-processing,quicksort,Algorithm,Parallel Processing,Quicksort,我想用Python实现并行快速排序。 我知道快速排序,您必须选择一个轴心、分区,但如何在Python中将它们作为独立任务生成 以下是它的伪代码: QS(A[1:n]) if n=1 then return A[1] pivot <--any value from A (random) L <- A[A[:] < pivot] R <- A[A[:] > pivot] A(L) <- spawn QS(L) A(R) <- QS(R) sync retur

我想用Python实现并行快速排序。 我知道快速排序,您必须选择一个轴心、分区,但如何在Python中将它们作为独立任务生成

以下是它的伪代码:

QS(A[1:n])
if n=1 then return A[1]
pivot <--any value from A (random)
L <- A[A[:] < pivot]
R <- A[A[:] > pivot]
A(L) <- spawn QS(L)
A(R) <- QS(R)
sync
return A(L) ++ A(R)
QS(A[1:n])
如果n=1,则返回[1]
pivot您可以这样做,但它不太可能加快代码的速度。您可以使用
ThreadPoolExecutor
创建线程并从中获取结果。下面是一个简单的示例,其中包含一个对数组求和的函数:

from concurrent.futures import ThreadPoolExecutor
pool = ThreadPoolExecutor(max_workers=1)
def add(arr):
    if len(arr)<2:
        return sum(arr)  #cheating a little
    mid = len(arr)//2
    f = pool.submit(add,arr[:mid])
    y = add(arr[mid:])
    return y+f.result()
从concurrent.futures导入ThreadPoolExecutor
池=线程池执行器(最大工作线程数=1)
def添加(arr):
if len(arr)