-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergeSort
More file actions
257 lines (220 loc) · 6.79 KB
/
Copy pathMergeSort
File metadata and controls
257 lines (220 loc) · 6.79 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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
def mergeSort(arr):
if len(arr) > 1:
left_arr = arr[0:len(arr)//2]
right_arr = arr[len(arr)//2:len(arr)]
#recursion
mergeSort(left_arr)
mergeSort(right_arr)
#merge
i = 0 #left most
j = 0 #right most
k = 0 #merge array index
while i <len(left_arr) and j < len(right_arr): #while both arrays indices are not already looked at
if left_arr[i] < right_arr[j]:
arr[k] = left_arr[i] #set merge index value to left array value
i += 1
else: #if equal to or greater than right
arr[k] = right_arr[j]
j += 1
k += 1
while i < len(left_arr): #if the right array is done but the left arr hasn't fully been looked at yet.
arr[k] = left_arr[i]
i += 1
k += 1
while j < len(right_arr):
arr[k] = right_arr[j]
j += 1
k += 1
#test case
arr = [1,3,5,64,3,2,4,4,3,4,5,5,8,6]
mergeSort(arr)
print(arr)
#Runtime for regular Merge Sort
import random
import time
def randomNumberList(n): #create a random list
rand_list=[]
for i in range(n):
rand_list.append(random.randint(0,1001)) #numbers between 0-1000
print("Unsorted:")
print(rand_list)
starttime = time.time()
mergeSort(rand_list)
print("Sorted")
print(rand_list)
endtime = time.time()
totaltime = endtime - starttime
print("The total time in seconds for ")
print(n)
print("is")
print(totaltime)
randomNumberList(100) #n=100
randomNumberList(1000) #n=1000
randomNumberList(10000) #n=10000
randomNumberList(25000) #n=25000
randomNumberList(50000) #n=50000
#graph for merge sort
# importing the required module
import matplotlib.pyplot as plt
# x axis values
x = [100,1000,10000,25000,50000]
# corresponding y axis values
y = [0.001966238021850586, 0.004695415496826172, 0.04029560089111328, 0.09277176856994629, 0.2706151008605957]
# plotting the points
plt.plot(x, y)
# naming the x axis
plt.xlabel('Number of elements in Array')
# naming the y axis
plt.ylabel('Time in seconds')
# giving a title to my graph
plt.title('Regular Merge Sort')
# function to show the plot
plt.show()
#Reverse Merge Sort
def ReversemergeSort(arr):
if len(arr) > 1:
left_arr = arr[0:len(arr)//2]
right_arr = arr[len(arr)//2:len(arr)]
#recursion
mergeSort(left_arr)
mergeSort(right_arr)
#merge
i = 0 #left most
j = 0 #right most
k = 0 #merge array index
while i <len(left_arr) and j < len(right_arr): #while both arrays indices are not already looked at
if left_arr[i] > right_arr[j]: #do greater than for reverse merge sort
arr[k] = left_arr[i] #set merge index value to left array value
i += 1
else: #if equal to or greater than right
arr[k] = right_arr[j]
j += 1
k += 1
while i < len(left_arr): #if the right array is done but the left arr hasn't fully been looked at yet.
arr[k] = left_arr[i]
i += 1
k += 1
while j < len(right_arr):
arr[k] = right_arr[j]
j += 1
k += 1
#Runtime for reverse Merge Sort
import random
import time
def randomNumberList(n): #create a random list
rand_list=[]
for i in range(n):
rand_list.append(random.randint(0,1001)) #numbers between 0-1000
print("Unsorted:")
print(rand_list)
starttime = time.time()
ReversemergeSort(rand_list)
print("Sorted")
print(rand_list)
endtime = time.time()
totaltime = endtime - starttime
print("The total time in seconds for ")
print(n)
print("is")
print(totaltime)
randomNumberList(100) #n=100
randomNumberList(1000) #n=1000
randomNumberList(10000) #n=10000
randomNumberList(25000) #n=25000
randomNumberList(35000) #n=35000
randomNumberList(45000) #n=25000
randomNumberList(50000) #n=50000
#graph for reverse merge sort
# importing the required module
import matplotlib.pyplot as plt
# x axis values
x = [100,1000,10000,25000,35000, 45000, 50000]
# corresponding y axis values
y = [0.0009865760803222656, 0.006527900695800781, 0.03807806968688965, 0.10205554962158203, 0.20088529586791992, 0.2325303554534912, 0.35698795318603516]
# plotting the points
plt.plot(x, y)
# naming the x axis
plt.xlabel('Number of elements in Array')
# naming the y axis
plt.ylabel('Time in seconds')
# giving a title to my graph
plt.title('Reverse Merge Sort')
# function to show the plot
plt.show()
#Alphanumeric Merge Sort
def AlphaNumericmergeSort(arr):
if len(arr) > 1:
left_arr = arr[0:len(arr)//2]
right_arr = arr[len(arr)//2:len(arr)]
#recursion
mergeSort(left_arr)
mergeSort(right_arr)
#merge
i = 0 #left most
j = 0 #right most
k = 0 #merge array index
while i <len(left_arr) and j < len(right_arr): #while both arrays indices are not already looked at
if str(left_arr[i]) < str(right_arr[j]): #change to string class
arr[k] = left_arr[i] #set merge index value to left array value
i += 1
else: #if equal to or greater than right
arr[k] = right_arr[j]
j += 1
k += 1
while i < len(left_arr): #if the right array is done but the left arr hasn't fully been looked at yet.
arr[k] = left_arr[i]
i += 1
k += 1
while j < len(right_arr):
arr[k] = right_arr[j]
j += 1
k += 1
import random
import string
import time
def generateRandStringArray(n):
array = []
for i in range(0,n):
randstring = (random.choices(string.printable, k = random.randint(1,10))) #generating random strings length between 1 to 9
res = ''
for ele in randstring:
# Concatenating random elements
res += random.choice(ele)
array.insert(i,res) #insert into array
print("Unsorted: ")
print(array)
#insertionAlphaNumeric(array) #insertion sort
startTime = time.time()
AlphaNumericmergeSort(array) #calling to test here
endTime = time.time()
totalTime = endTime-startTime
print("When n equals")
print(n)
print("it took approximately")
print(totalTime)
print("in seconds")
generateRandStringArray(100) #testing 100
generateRandStringArray(1000) #calling to test 1000 here
generateRandStringArray(10000) #calling to test 10000 here
generateRandStringArray(15000)
generateRandStringArray(25000) #calling to test 25000 here
generateRandStringArray(35000)
generateRandStringArray(45000)
generateRandStringArray(50000)
#graph for alphanumeric merge sort
# importing the required module
import matplotlib.pyplot as plt
# x axis values
x = [100,1000,10000,15000, 25000,35000, 45000, 50000]
# corresponding y axis values
y = [0.0003311634063720703, 0.003058195114135742, 0.038448333740234375, 0.09008526802062988, 0.15652012825012207, 0.2315833568572998, 0.19575023651123047, 0.3014953136444092]
# plotting the points
plt.plot(x, y)
# naming the x axis
plt.xlabel('Number of elements in Array')
# naming the y axis
plt.ylabel('Time in seconds')
# giving a title to my graph
plt.title('Alphanumeric Merge Sort')
# function to show the plot
plt.show()