-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmerge_thread.py
More file actions
39 lines (39 loc) · 1.03 KB
/
merge_thread.py
File metadata and controls
39 lines (39 loc) · 1.03 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
import threading as th
def mergeSort(arr):
if len(arr)>1:
mid = len(arr)/2
lefthalf = arr[:mid]
righthalf = arr[mid:]
t1 = th.Thread(target=mergeSort, args=(lefthalf,))
t2 = th.Thread(target=mergeSort, args=(righthalf,))
t1.start()
t2.start()
t1.join()
t2.join()
i=0
j=0
k=0
# print "lefthalf: ",lefthalf
# print "righthalf: ",righthalf
while i<len(lefthalf) and j<len(righthalf):
# print lefthalf[i]," ",righthalf[j]
if lefthalf[i]<righthalf[j]:
arr[k]=lefthalf[i]
i=i+1
k=k+1
else:
arr[k]=righthalf[j]
j=j+1
k=k+1
while i < len(lefthalf):
arr[k]=lefthalf[i]
i=i+1
k=k+1
while j < len(righthalf):
arr[k]=righthalf[j]
j=j+1
k=k+1
# print "merging", arr
#
# arr = [13,4,1,7,90,24,113,63]
# mergeSort(arr)