def bubble_sort(lists):
count = len(lists)
for i in range(0, count):
for j in range(i + 1, count):
if lists[i] > lists[j]:
#判断后值是否比前置大,如果大就将其交换
lists[i], lists[j] = lists[j], lists[i]
return lists
res=bubble_sort([1,209,31,4,555,6,765,9,5,4,7,89,6,5,34,3,57,96])
print(res)
def merge_sort(li):
n = len(li)
if n == 1:
return li
mid = n
left = li[:mid]
right = li[mid:]
left_res = merge_sort(left)
right_res = merge_sort(right)
result = merge(left_res, right_res)
return result
def merge(left, right):
result = []
left_index = 0
right_index = 0
while left_index < len(left) and right_index < len(right):
if left[left_index] <= right[right_index]:
result.append(left[left_index])
left_index += 1
else:
result.append(right[right_index])
right_index += 1
result += right[right_index:]
result += left[left_index:]
return result
if __name__ == '__main__':
list_demo = [6, 5, 7, 4, 3, 1]
print(merge_sort(list_demo))
def select_sort(lists):
# 选择排序
count = len(lists)
for i in range(0, count):
min = i
for j in range(i + 1, count):
if lists[min] > lists[j]:
min = j
lists[min], lists[i] = lists[i], lists[min]
return lists
res=select_sort([1,209,31,4,555,6,765,9,5,4,7,89,6,5,34,3,57,96])
print(res)