forked from powerexploit/Awesome-Python-Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmerge_sort.py
More file actions
48 lines (33 loc) · 812 Bytes
/
Copy pathmerge_sort.py
File metadata and controls
48 lines (33 loc) · 812 Bytes
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
def merge_sort(lst_main):
if len(lst_main) <= 1:
return lst_main
else:
mid = len(lst_main) // 2
l = merge_sort(lst_main[:mid])
r = merge_sort(lst_main[mid:])
return merge(l, r)
def merge(lst_left, lst_right):
lst_ord = []
p_left = 0
p_right = 0
while p_left < len(lst_left) and p_right < len(lst_right):
if lst_left[p_left] > lst_right[p_right]:
lst_ord.append(lst_right[p_right])
p_right += 1
else:
lst_ord.append(lst_left[p_left])
p_left += 1
for i in range(p_left, len(lst_left)):
lst_ord.append(lst_left[i])
for i in range(p_right, len(lst_right)):
lst_ord.append(lst_right[i])
return lst_ord
import random
def main():
lst = []
for i in range(100):
lst.append(random.randrange(9))
print(merge_sort(lst))
return 0
if __name__ == '__main__':
main()