冒泡排序
def bubble_sort(l: list):
for i in range(len(l) - 1):
flag = True
for j in range(len(l) - 1 - i):
if l[j] > l[j + 1]:
flag = False
l[j], l[j + 1] = l[j + 1], l[j]
if flag:
break
if __name__ == '__main__':
l = [random.randint(1, 30) for _ in range(12)]
print(l) # [13, 15, 10, 24, 17, 4, 16, 22, 5, 11, 5, 28]
bubble_sort(l)
print(l) # [4, 5, 5, 10, 11, 13, 15, 16, 17, 22, 24, 28]
选择排序
def select_sort(l: list):
for i in range(len(l) - 1):
for j in range(i + 1, len(l)):
if l[i] > l[j]:
l[i], l[j] = l[j], l[i]
if __name__ == '__main__':
l = [random.randint(1, 30) for _ in range(12)]
print(l) # [13, 20, 11, 7, 1, 29, 24, 17, 18, 28, 15, 19]
select_sort(l)
print(l) # [1, 7, 11, 13, 15, 17, 18, 19, 20, 24, 28, 29]
插入排序
def insert_sort(l: list):
for i in range(1, len(l)):
for j in range(i, 0, -1):
if l[j] < l[j - 1]:
l[j], l[j - 1] = l[j - 1], l[j]
else:
break
if __name__ == '__main__':
l = [random.randint(1, 30) for _ in range(12)]
print(l) # [24, 22, 30, 7, 6, 4, 29, 29, 23, 1, 9, 16]
insert_sort(l)
print(l) # [1, 4, 6, 7, 9, 16, 22, 23, 24, 29, 29, 30]
希尔排序
def shell_sort(l: list):
gap = len(l) // 2
while gap >= 1:
for i in range(gap, len(l)):
for j in range(i, 0, gap * -1):
if l[j] < l[j - 1]:
l[j], l[j - 1] = l[j - 1], l[j]
else:
break
gap = gap // 2
if __name__ == '__main__':
l = [random.randint(1, 30) for _ in range(12)]
print(l) # [22, 8, 6, 22, 4, 4, 27, 27, 17, 24, 22, 10]
shell_sort(l)
print(l) # [4, 4, 6, 8, 10, 17, 22, 22, 22, 24, 27, 27]
快速排序
def quick_sort(l: list, low, high):
left = low
right = high
if left >= right:
return
start = l[left]
while left < right:
while left < right and l[right] >= start:
right -= 1
l[left] = l[right]
while left < right and l[left] <= start:
left += 1
l[right] = l[left]
l[left] = start
quick_sort(l, left + 1, high)
quick_sort(l, 0, left - 1)
if __name__ == '__main__':
l = [random.randint(1, 30) for _ in range(12)]
print(l) # [8, 11, 16, 25, 28, 10, 10, 20, 9, 29, 3, 3]
quick_sort(l, 0, len(l) - 1)
print(l) # [3, 3, 8, 9, 10, 10, 11, 16, 20, 25, 28, 29]
堆排序
def heap_sort(l):
n = len(l)
for i in range(n // 2 - 1, -1, -1):
sift(l, i, n - 1)
for i in range(n - 1, -1, -1):
l[0], l[i] = l[i], l[0]
sift(l, 0, i - 1)
def sift(l, low, high):
i = low
j = 2 * i + 1
tmp = l[i]
while j <= high:
if j < high and l[j] < l[j + 1]:
j += 1
if l[j] > tmp:
l[i] = l[j]
i = j
j = 2 * i + 1
else:
break
l[i] = tmp
if __name__ == '__main__':
l = [random.randint(1, 30) for _ in range(12)]
print(l) # [1, 29, 15, 27, 1, 3, 23, 25, 11, 18, 25, 14]
heap_sort(l)
print(l) # [1, 1, 3, 11, 14, 15, 18, 23, 25, 25, 27, 29]
归并排序
def merge(l, low, mid, high):
i = low
j = mid + 1
l_tmp = []
while i <= mid and j <= high:
if l[i] <= l[j]:
l_tmp.append(l[i])
i += 1
else:
l_tmp.append(l[j])
j += 1
while i <= mid:
l_tmp.append(l[i])
i += 1
while j <= high:
l_tmp.append(l[j])
j += 1
l[low: high + 1] = l_tmp
def merge_sort(l, low, high):
if low < high:
mid = (low + high) // 2
merge_sort(l, low, mid)
merge_sort(l, mid + 1, high)
merge(l, low, mid, high)
if __name__ == '__main__':
l = [random.randint(1, 30) for _ in range(12)]
print(l) # [29, 23, 17, 8, 9, 30, 2, 10, 20, 15, 19, 22]
merge_sort(l, 0, len(l) - 1)
print(l) # [2, 8, 9, 10, 15, 17, 19, 20, 22, 23, 29, 30]