-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathquick_sort.py
More file actions
47 lines (37 loc) · 1.09 KB
/
quick_sort.py
File metadata and controls
47 lines (37 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
from threading import Thread
import threading
import time
import thread
def qsort(sets,left,right):
i = left
j = right
print " the array: ", sets
print " left is ", left
print " right is", right
pivot = sets[(left + right)/2]
print("thread {0} is sorting {1} and pivot is {2}".format(threading.current_thread(), sets[left:right+1], pivot))
while(i <= j):
while(pivot > sets[i]):
i = i+1
while(pivot < sets[j]):
j = j-1
if(i <= j):
sets[i],sets[j] = sets[j], sets[i]
i = i + 1
j = j - 1
lthread = None
rthread = None
print "The array after pivot positioning ", sets
if (left < j):
lthread = Thread(target = lambda: qsort(sets,left,j))
lthread.start()
if (i < right):
rthread = Thread(target=lambda: qsort(sets,i,right))
rthread.start()
if lthread is not None: lthread.join()
if rthread is not None: rthread.join()
return sets
'''testing below'''
ls = [10,5,1,3,6,4,9,2,8,16,7]
res = qsort(ls, 0, len(ls) - 1)
print(res)