归并排序的python实现
import math
def sort(arr):
num_count = len(arr)
n = 0
while True:
temp_arr = []
group_size = 2 ** n
for i in range(math.ceil(num_count/group_size/2)):
left = arr[group_size*(2*i):group_size*(2*i+1)]
right = arr[group_size*(2*i+1):group_size*(2*i+2)]
while left and right:
if left[0] > right[0]:
temp_arr.append(left.pop(0))
else:
temp_arr.append(right.pop(0))
temp_arr.extend(left)
temp_arr.extend(right)
arr = temp_arr
if group_size >= num_count:
break
n += 1
return arr
la = [5, 2, 7, 8, 6, 1, 4, 9, 10, 1, 2, 3, 4]
print(sort(la))
分享文章:归并排序的python实现
当前路径:http://pwwzsj.com/article/jodpie.html