-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththree_way_merge_sort.py
144 lines (116 loc) · 2.96 KB
/
three_way_merge_sort.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# Python Program to perform 3 way Merge Sort
""" Merge the sorted ranges [low, mid1), [mid1,mid2)
and [mid2, high). Mid1 is first midpoint
index in overall range to merge; mid2 is second
midpoint index in overall range to merge"""
def merge(gArray, low, mid1, mid2, high, destArray):
i = low
j = mid1
k = mid2
l = low
# Choose smaller of the smallest in the three ranges
while ((i < mid1) and (j < mid2) and (k < high)):
if(gArray[i] < gArray[j]):
if(gArray[i] < gArray[k]):
destArray[l] = gArray[i]
l += 1
i += 1
else:
destArray[l] = gArray[k]
l += 1
k += 1
else:
if(gArray[j] < gArray[k]):
destArray[l] = gArray[j]
l += 1
j += 1
else:
destArray[l] = gArray[k]
l += 1
k += 1
# Case where first and second ranges
# have remaining values
while ((i < mid1) and (j < mid2)):
if(gArray[i] < gArray[j]):
destArray[l] = gArray[i]
l += 1
i += 1
else:
destArray[l] = gArray[j]
l += 1
j += 1
# case where second and third ranges
# have remaining values
while ((j < mid2) and (k < high)):
if(gArray[j] < gArray[k]):
destArray[l] = gArray[j]
l += 1
j += 1
else:
destArray[l] = gArray[k]
l += 1
k += 1
# Case where first and third ranges have
# remaining values
while ((i < mid1) and (k < high)):
if(gArray[i] < gArray[k]):
destArray[l] = gArray[i]
l += 1
i += 1
else:
destArray[l] = gArray[k]
l += 1
k += 1
# Copy remaining values from the first range
while (i < mid1):
destArray[l] = gArray[i]
l += 1
i += 1
# Copy remaining values from the second range
while (j < mid2):
destArray[l] = gArray[j]
l += 1
j += 1
# Copy remaining values from the third range
while (k < high):
destArray[l] = gArray[k]
l += 1
k += 1
""" Performing the merge sort algorithm on the
given array of values in the rangeof indices
[low, high). low is minimum index, high is
maximum index (exclusive) """
def mergeSort3WayRec(gArray, low, high, destArray):
# If array size is 1 then do nothing
if (high - low < 2):
return
# Splitting array into 3 parts
mid1 = low + ((high - low) // 3)
mid2 = low + 2 * ((high - low) // 3) + 1
# Sorting 3 arrays recursively
mergeSort3WayRec(destArray, low, mid1, gArray)
mergeSort3WayRec(destArray, mid1, mid2, gArray)
mergeSort3WayRec(destArray, mid2, high, gArray)
# Merging the sorted arrays
merge(destArray, low, mid1, mid2, high, gArray)
def mergeSort3Way(gArray, n):
# if array size is zero return null
if (n == 0):
return
# creating duplicate of given array
fArray = []
# copying elements of given array into
# duplicate array
fArray = gArray.copy()
# sort function
mergeSort3WayRec(fArray, 0, n, gArray)
# copy back elements of duplicate array
# to given array
gArray = fArray.copy()
# return the sorted array
return gArray
data = [45, -2, -45, 78, 30, -42, 10, 19, 73, 93]
data = mergeSort3Way(data, 10)
print("After 3 way merge sort: ", end="")
for i in range(10):
print(f"{data[i]} ", end="")