-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmergesort
More file actions
42 lines (38 loc) · 1.02 KB
/
Copy pathmergesort
File metadata and controls
42 lines (38 loc) · 1.02 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
import random
def GenerateRandomList(number, size):
temp = []
random_legth = random.randint(5, size)
current_length = 0
while current_length < random_legth:
temp.append(random.randint(1, number))
current_length += 1
return temp
def MergeSort(arr):
length = len(arr)
if length <= 1:
return arr
mid = length // 2
left = MergeSort(arr[:mid])
right = MergeSort(arr[mid:])
res = Merge(left, right)
return res
def Merge(left_arr, right_arr):
length_left, length_right = len(left_arr), len(right_arr)
temp = []
p1, p2 = 0, 0
while p1 < length_left and p2 < length_right:
if left_arr[p1] < right_arr[p2]:
temp.append(left_arr[p1])
p1 += 1
else:
temp.append(right_arr[p2])
p2 += 1
temp += left_arr[p1:]
temp += right_arr[p2:]
return temp
for i in range(100):
l = GenerateRandomList(200,100)
l_standard = sorted(l)
if (l_standard != MergeSort(l)):
print('Wrong!')
print('Done!')