-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.py
41 lines (28 loc) · 798 Bytes
/
main.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
import random
def merge_sort(arr):
if len(arr) < 2:
return arr
mid = len(arr) // 2
left, right = arr[:mid], arr[mid:]
return merge(merge_sort(left), merge_sort(right))
def merge(left, right):
result = []
while len(left) and len(right):
if left[0] < right[0]:
result.append(left.pop(0))
else:
result.append(right.pop(0))
if len(left) : result.extend(left)
if len(right): result.extend(right)
return result
def main():
num = 20
range_left = 0
range_right = 10000
arr = [random.randint(range_left, range_right) for _ in range(num)]
print('Original array:')
print(arr)
print('Sorted array:')
print(merge_sort(arr))
if __name__ == '__main__':
main()