-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmerge_sort.py
40 lines (30 loc) · 840 Bytes
/
merge_sort.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
#归并排序
import random
def MergeSort(list,left,right):
if right - left <= 1:
return
mid = (left + right) // 2
MergeSort(list,left,mid)
MergeSort(list,mid,right)
tem_list = []
a_index = 0
b_index = 0
while a_index < mid- left and b_index < right-mid:
if list[left+a_index] < list[mid+b_index]:
tem_list.append(list[left+a_index])
a_index += 1
else:
tem_list.append(list[mid+b_index])
b_index += 1
if a_index < mid -left:
tem_list += list[left+a_index:mid]
else:
tem_list += list[mid+b_index:right]
list[left:right] = tem_list
return list
if __name__ == '__main__':
a = [11,11]
for i in range(10):
a.append(random.randint(1,100))
print(a)
print(MergeSort(a,0,len(a)))