-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmerge_sorting.py
More file actions
38 lines (33 loc) · 1.09 KB
/
Copy pathmerge_sorting.py
File metadata and controls
38 lines (33 loc) · 1.09 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
#!python3
from numpy import random
from time import perf_counter
# *TODO can be improved for possible erronious inputs
# otherwise pretty happy.
# prioritizing left array in case of key equality makes the sort stable.
a = random.randint(1_000, size=2_000_000)
print(a)
def merge_sort(array):
if len(array) <= 1:
return array
if len(array) > 1:
larray, rarray = array[:len(array)//2], array[len(array)//2:]
larray = merge_sort(larray)
rarray = merge_sort(rarray)
i=j=0
sorted_array = []
while i < len(larray) and j < len(rarray):
if larray[i] <= rarray[j]:
sorted_array.append(larray[i])
i += 1
else:
sorted_array.append(rarray[j])
j +=1
if i == len(larray):
sorted_array.extend(rarray[j:])
else:
sorted_array.extend(larray[i:])
return sorted_array
if __name__ == "__main__":
start = perf_counter()
print(merge_sort(a))
print(f"\nsorted {len(a):,} items in {(perf_counter() - start):.2f} seconds")