排序算法 python实现 / 默写
# 汉诺塔
import copy
import random
def hanuo(n, a, b, c):
if n == 1:
print(f'{a} --> {c}')
return
hanuo(n - 1, a, c, b)
print(f'{a} --> {c}')
hanuo(n - 1, b, a, c)
hanuo(3, 'A', 'B', 'C')
# 冒泡排序
def bubble_sort(arr):
n = len(arr)
for i in range(n - 1):
for j in range(n - 1 - i):
if arr[j] > arr[j + 1]:
arr[j], arr[j + 1] = arr[j + 1], arr[j]
return arr
def jiWeiJiu(arr):
n = len(arr)
for i in range(n // 2):
flag = False
for m in range(i, n - 1 - i):
if arr[m] > arr[m + 1]:
arr[m], arr[m + 1] = arr[m + 1], arr[m]
flag = True
if not flag:
break
flag = False
for q in range(n - i - 2, i, -1):
if arr[q - 1] > arr[q]:
arr[q - 1], arr[q] = arr[q], arr[q - 1]
flag = True
if not flag:
break
return arr
def choose_sort(arr):
n = len(arr)
"""
0 1 2 3 4 5
n = 6
"""
for i in range(n - 1):
min_index = i
for j in range(i + 1, n):
if arr[min_index] > arr[j]:
min_index = j
if min_index != i:
arr[min_index], arr[i] = arr[i], arr[min_index]
return arr
# 3. 插入
def insert_sort(arr):
n = len(arr)
for i in range(1, n):
j = i - 1
tmp = arr[i]
while j >= 0 and tmp < arr[j]:
arr[j + 1] = arr[j]
j -= 1
arr[j + 1] = tmp
return arr
def merge(arr, left, mid, right):
i = left
j = mid + 1
tmp = []
while i <= mid and j <= right:
if arr[i] < arr[j]:
tmp.append(arr[i])
i += 1
else:
tmp.append(arr[j])
j += 1
while i <= mid:
tmp.append(arr[i])
i += 1
while j <= right:
tmp.append(arr[j])
j += 1
arr[left: right + 1] = tmp
def merge_sort(arr, left, right):
if left < right:
mid = (left + right) // 2
# 假设左边右边都已经排好了
merge_sort(arr, left, mid)
merge_sort(arr, mid + 1, right)
merge(arr, left, mid, right)
return arr
# 5. 快速
def partition(arr, left, right):
tmp = arr[left]
while left < right:
while left < right and arr[right] >= tmp:
right -= 1
arr[left] = arr[right]
while left < right and arr[left] <= tmp:
left += 1
arr[right] = arr[left]
arr[left] = tmp
return left
def quick_sort(arr, left, right):
if left < right:
mid = partition(arr, left, right)
quick_sort(arr, left, mid - 1)
quick_sort(arr, mid + 1, right)
return arr
# 定顶调整,大顶堆
# 以后不用 i j 用 p q
def sift(arr, low, high):
i = low
tmp = arr[low]
j = 2 * i + 1
# 最小三角
while j <= high:
if j + 1 <= high and arr[j] < arr[j + 1]:
j = j + 1
if tmp < arr[j]:
arr[i] = arr[j]
i = j
j = 2 * i + 1
else:
break
arr[i] = tmp
return arr
def heap_sort(arr):
n = len(arr)
# 农村包围城市
# 依次处理每个顶
for i in range((n - 2) // 2, -1, -1):
sift(arr, i, n - 1)
# 挨个出数
for i in range(n - 1, -1, -1):
arr[0], arr[i] = arr[i], arr[0]
sift(arr, 0, i - 1)
return arr
# 3. 插入
def insert_sort_gap(arr, gap):
n = len(arr)
for i in range(gap, n):
j = i - gap
tmp = arr[i]
while j >= 0 and tmp < arr[j]:
arr[j + gap] = arr[j]
j -= gap
arr[j + gap] = tmp
return arr
def shell_sort(arr):
n = len(arr)
gap = n // 2
while gap >= 1:
insert_sort_gap(arr, gap)
gap //= 2
return arr
# 8. 计数
"""
0 1 2 3 4 8
max_v = 8
0 ~ 8; [0 , 9)
"""
def count_sort(arr):
max_v = max(arr)
bucket = [0 for _ in range(max_v + 1)]
for item in arr:
bucket[item] += 1
arr.clear()
for i in range(len(bucket)):
while bucket[i] > 0:
arr.append(i)
bucket[i] -= 1
return arr
def radix_sort(arr):
if not arr:
return []
digit = len(str(max(arr)))
for i in range(digit):
bucket = [[] for _ in range(10)] # 0 ~ 9
for v in arr:
k = (v // (10 ** i)) % 10
bucket[k].append(v)
arr.clear()
for bu in bucket:
arr.extend(bu)
return arr
def bucket_sort(arr, n=100, max_v=10000):
bucket = [[] for _ in range(n)]
for v in arr:
i = min(v // (max_v // n), n - 1)
bucket[i].append(v)
for j in range(len(bucket[i]) - 1, 0, -1):
if bucket[i][j] < bucket[i][j - 1]:
bucket[i][j], bucket[i][j - 1] = bucket[i][j - 1], bucket[i][j]
else:
break
arr.clear()
for bu in bucket:
arr.extend(bu)
return arr
li = [random.randint(0, 100) for _ in range(10)]
li1 = copy.deepcopy(li)
li2 = copy.deepcopy(li)
li3 = copy.deepcopy(li)
li4 = copy.deepcopy(li)
li5 = copy.deepcopy(li)
li6 = copy.deepcopy(li)
li7 = copy.deepcopy(li)
li8 = copy.deepcopy(li)
li9 = copy.deepcopy(li)
li10 = copy.deepcopy(li)
li11 = copy.deepcopy(li)
print('冒泡:', bubble_sort(li1))
print('鸡尾:', jiWeiJiu(li2))
print('选择:', choose_sort(li3))
print('插入:', insert_sort(li4))
print('归并:', merge_sort(li5, 0, len(li5) - 1))
print('快速:', quick_sort(li6, 0, len(li5) - 1))
print('堆排:', heap_sort(li7))
print('希尔:', shell_sort(li8))
print('计数:', count_sort(li9))
print('基数:', radix_sort(li10))
print('桶排:', bucket_sort(li11))
最近有点水逆,心理有点低落
加油吧 🍀