并发快速排序(多线程) [英] Concurrent quick sort (multithreading)

查看:121
本文介绍了并发快速排序(多线程)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 threading 进行快速排序.但是当我用我的线程方法运行代码时,它只会递归地为所有分区运行相同的线程.

I am trying to make quick sort concurrent by using threading. But when i run the code with my threading approach, it only runs the same thread for all the partitions recursively.

这是我尝试过的.

from threading import Thread
import threading
import time
import thread

def qsort(sets,left,right):
    i = left
    j = right
    pivot = sets[(left + right)/2]
    temp = 0
    while(i <= j):
         while(pivot > sets[i]):
             i = i+1
         while(pivot < sets[j]):
             j = j-1
         if(i <= j):
             temp = sets[i]     
             sets[i] = sets[j]
             sets[j] = temp
             i = i + 1
             j = j - 1
    if (left < j):
        thread = Thread(target = qsort(sets,left,j))
        name =  threading.current_thread() 
        printp(sets,elements,name)
    if (i < right):
        thread1 = Thread(target=qsort(sets,i,right))
        name =  threading.current_thread()
        printp(sets,elements,name)
    return sets

推荐答案

Python 中没有线程并行执行.使用池或进程.fork 还是可以的.亚历克斯

There is no reel parallel execution with threads in Python. Use Pool or Processs. fork is still possible. Alex

这篇关于并发快速排序(多线程)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆