关于python的一些简单排序
立即下载
资源介绍:
一些简单的排序方法,包括:冒泡排序、选择排序、插入排序、归并排序、快速排序
def sort4(list):
num = len(list)
index = num // 2
if index == 0:
return list
left = list[:index]
right = list[index:]
return sort4_2(sort4(left), sort4(right))
def sort4_2(left, right):
temp = []
num1 = num2 = 0
for i in range(len(left) + len(right)):
if num2 < len(right) and num1 < len(left):
if left[num1] >= right[num2]:
temp.append(left[num1])
num1 += 1
else:
temp.append(right[num2])
num2 += 1
else:
if num2 == len(right):
temp.append(left[num1])
num1 += 1
elif num1 == len(left):
temp.append(right[num2])
num2 += 1
return temp
s1 = [2, 44, 55, 4, 2, 435, 234]
sort4(s1)
print(sort4(s1))