# 1. 冒泡排序

# buble sort
def buble_sort(inList):
for i in range(len(inList)):
for j in range(len(inList)-i-1):
if inList[j] <= inList[j+1]:
continue
else:
inList[j], inList[j+1] = inList[j+1], inList[j]
return inList


# 2插入排序

#insertion sort
def insertion_sort(list1):
temp = []
for i in range(len(list1)):
# 每一个循环，往temp中放入一个数据
temp.append(list1[i])
j = len(temp) - 1
while True and j>0:
# 如果数组中有数据，则把数据放入放在第一个大于他的数字前面，从而保证排序
if temp[j] < temp[j-1]:
temp[j], temp[j-1] = temp[j-1], temp[j]
j -= 1
return temp


# 3归并排序

# merge ort
def merge(list1, list2):
temp = []
i = j = 0
while True:
if i== len(list1):
temp.extend(list2[j:])
break
if j== len(list2):
temp.extend(list1[i:])
break

if list1[i] <= list2[j]:
temp.append(list1[i])
i += 1
else:
temp.append(list2[j])
j += 1
return temp

def merge_sort(list1):
length = len(list1)
if length == 1:
return list1
elif length == 0:
return []
splitIndex = int(length/2)
left_arr = list1[:splitIndex]
right_arr = list1[splitIndex:]
return merge(merge_sort(left_arr), merge_sort(right_arr))


# 查找最大子数组

# find max sub array
def find_max_subarray(arr, low, high):
if low == high:
return low, heigh

mid = int((low+high)/2)
left_res = find_max_subarray(arr, low, mid)


©️2019 CSDN 皮肤主题: 数字20 设计师: CSDN官方博客