-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmergesort.py
75 lines (59 loc) · 2.3 KB
/
mergesort.py
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import time
max_time = 0.250
swapCount = 0
##---to reset swap count
def mergeSort(arr, displayArray, speedInput, pauseBool):
global swapCount
swapCount = 0
start, end = 0, len(arr) - 1
_merge_sort(arr, displayArray, speedInput, pauseBool, start, end)
# divides the array recursively into two parts then sorts
def _merge_sort(arr, displayArray, speedInput, pauseBool, start, end):
if start < end:
mid = (start + end) // 2
_merge_sort(arr, displayArray, speedInput, pauseBool, start, mid)
_merge_sort(arr, displayArray, speedInput, pauseBool, mid + 1, end)
_merge(arr, displayArray, speedInput, pauseBool, start, mid, end)
def _merge(arr, displayArray, speedInput, pauseBool, start, mid, end):
global swapCount
N = len(arr)
#--highlight the left and the right parts of the array
colorArray = ['red'] * N
colorCoords = ((start, mid + 1, '#ffff00'), (mid + 1, end + 1, '#5200cc'))
for lower, upper, color in colorCoords:
colorArray[lower:upper] = [color] * (upper - lower)
displayArray(arr, colorArray, swapCount)
time.sleep(max_time - (speedInput() * max_time / 100))
arrL = arr[start:mid+1]
arrR = arr[mid + 1:end + 1]
i, j, k = 0, 0, start # i->arrL; j->arrR; k->arr
while (i < len(arrL) and j < len(arrR)):
if arrL[i] < arrR[j]:
arr[k] = arrL[i]
i += 1
else:
arr[k] = arrR[j]
j += 1
swapCount += 1
colorArray[start:k] = ['green'] * (k - start)
displayArray(arr, colorArray, swapCount)
time.sleep(max_time - (speedInput() * max_time / 100))
k += 1
#check if anything left
while i < len(arrL):
arr[k] = arrL[i]
i += 1
k += 1
swapCount += 1
colorArray[start:k] = ['green'] * (k - start)
displayArray(arr, colorArray, swapCount)
time.sleep(max_time - (speedInput() * max_time / 100))
while j < len(arrR):
arr[k] = arrR[j]
j += 1
k += 1
swapCount += 1
colorArray[start:k] = ['green'] * (k - start)
displayArray(arr, colorArray, swapCount)
time.sleep(max_time - (speedInput() * max_time / 100))
print("Sorted arr : ", arr)