-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmergeSort.py
71 lines (57 loc) · 2.6 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
import random
def mergeSortFunction(listNum):
if len(listNum) > 1:
print("Length of the input list: ", str(len(listNum)) + "\n")
#Finds the middle of the array .
middleValue = len(listNum)//2
print("Middle value of the list", str(middleValue) + "\n")
#Dividing the array elements into two halves.
leftSide = listNum[:middleValue]
print("Current left side of the list:", leftSide)
rightSide = listNum[middleValue:]
print("Current right side of the list:", rightSide)
#Sorting the first half of the list given.
mergeSortFunction(leftSide)
print("Merge sort function left side: ", str(leftSide) + "\n")
#Sorting the second half of the list given.
mergeSortFunction(rightSide)
print("Merge sort function right side: ", str(rightSide) + "\n")
i = 0
j = 0
k = 0
print("Curren i j k values: ", i, j, k)
#Copy data to temporary lists leftSide[] and rightSide[].
#While 0 is < length of leftSide list and 0 is < length of rightSide list, keep running.
while i < len(leftSide) and j < len(rightSide):
#If leftSide list position 0 element is < rightSide list position 0 element listNum position 0 element equal to
#leftSide list positon 0 element.
#E.g. If 5 is < 4 listNum list index position 0 element will be set to leftSide index posotion 0 element
#which is 4. So first element in the listNum will be 4.
if leftSide[i] < rightSide[j]:
listNum[k] = leftSide[i]
print("Current1 listNum list: ", listNum)
#i will be added by 1.
i+=1
else:
#Or if leftSide index position 0 element is not < rightSide index position 0 element.
#listNum index position 0 element will be set to rightSide index position 0 element.
listNum[k] = rightSide[j]
print("Current2 listNum list: ", listNum)
#j is added by 1.
j+=1
#k is added by 1.
k+=1
#Checking if any list element was left out.
while i < len(leftSide):
listNum[k] = leftSide[i]
print("Current3 listNum list: ", listNum)
i+=1
k+=1
while j < len(rightSide):
listNum[k] = rightSide[j]
print("Current4 listNum list: ", listNum)
j+=1
k+=1
listNum = [5, 8, 0, 4, 7, 2]
mergeSortFunction(listNum)
print(listNum)