This commit is contained in:
krahets
2023-04-09 05:12:22 +08:00
parent 01d05cc1f0
commit 37f11aff68
27 changed files with 265 additions and 248 deletions
+3 -3
View File
@@ -90,7 +90,7 @@ comments: true
```python title="bubble_sort.py"
def bubble_sort(nums: list[int]) -> None:
""" 冒泡排序 """
"""冒泡排序"""
n: int = len(nums)
# 外循环:待排序元素数量为 n-1, n-2, ..., 1
for i in range(n - 1, 0, -1):
@@ -294,7 +294,7 @@ comments: true
```python title="bubble_sort.py"
def bubble_sort_with_flag(nums: list[int]) -> None:
""" 冒泡排序(标志优化) """
"""冒泡排序(标志优化)"""
n: int = len(nums)
# 外循环:待排序元素数量为 n-1, n-2, ..., 1
for i in range(n - 1, 0, -1):
@@ -306,7 +306,7 @@ comments: true
nums[j], nums[j + 1] = nums[j + 1], nums[j]
flag = True # 记录交换元素
if not flag:
break # 此轮冒泡未交换任何元素,直接跳出
break # 此轮冒泡未交换任何元素,直接跳出
```
=== "Go"
+1
View File
@@ -87,6 +87,7 @@ comments: true
```python title="bucket_sort.py"
def bucket_sort(nums: list[float]) -> None:
"""桶排序"""
# 初始化 k = n/2 个桶,预期向每个桶分配 2 个元素
k = len(nums) // 2
buckets = [[] for _ in range(k)]
+2 -2
View File
@@ -76,7 +76,7 @@ comments: true
```python title="counting_sort.py"
def counting_sort_naive(nums: list[int]) -> None:
""" 计数排序 """
"""计数排序"""
# 简单实现,无法用于排序对象
# 1. 统计数组最大元素 m
m = 0
@@ -346,7 +346,7 @@ $$
```python title="counting_sort.py"
def counting_sort(nums: list[int]) -> None:
""" 计数排序 """
"""计数排序"""
# 完整实现,可排序对象,并且是稳定排序
# 1. 统计数组最大元素 m
m = max(nums)
+3 -3
View File
@@ -66,8 +66,8 @@ comments: true
```python title="insertion_sort.py"
def insertion_sort(nums: list[int]) -> None:
""" 插入排序 """
# 外循环:base = nums[1], nums[2], ..., nums[n-1]
"""插入排序"""
# 外循环:base = nums[1], nums[2], ..., nums[n-1]
for i in range(1, len(nums)):
base: int = nums[i]
j: int = i - 1
@@ -75,7 +75,7 @@ comments: true
while j >= 0 and nums[j] > base:
nums[j + 1] = nums[j] # 1. 将 nums[j] 向右移动一位
j -= 1
nums[j + 1] = base # 2. 将 base 赋值到正确位置
nums[j + 1] = base # 2. 将 base 赋值到正确位置
```
=== "Go"
+6 -6
View File
@@ -147,11 +147,11 @@ comments: true
```python title="merge_sort.py"
def merge(nums: list[int], left: int, mid: int, right: int) -> None:
""" 合并左子数组和右子数组 """
"""合并左子数组和右子数组"""
# 左子数组区间 [left, mid]
# 右子数组区间 [mid + 1, right]
# 初始化辅助数组
tmp: list[int] = list(nums[left:right + 1])
tmp: list[int] = list(nums[left : right + 1])
# 左子数组的起始索引和结束索引
left_start: int = 0
left_end: int = mid - left
@@ -177,13 +177,13 @@ comments: true
j += 1
def merge_sort(nums: list[int], left: int, right: int) -> None:
""" 归并排序 """
"""归并排序"""
# 终止条件
if left >= right:
return # 当子数组长度为 1 时终止递归
return # 当子数组长度为 1 时终止递归
# 划分阶段
mid: int = (left + right) // 2 # 计算中点
merge_sort(nums, left, mid) # 递归左子数组
mid: int = (left + right) // 2 # 计算中点
merge_sort(nums, left, mid) # 递归左子数组
merge_sort(nums, mid + 1, right) # 递归右子数组
# 合并阶段
merge(nums, left, mid, right)
+7 -7
View File
@@ -101,7 +101,7 @@ comments: true
```python title="quick_sort.py"
def partition(self, nums: list[int], left: int, right: int) -> int:
""" 哨兵划分 """
"""哨兵划分"""
# 以 nums[left] 作为基准数
i, j = left, right
while i < j:
@@ -334,7 +334,7 @@ comments: true
```python title="quick_sort.py"
def quick_sort(self, nums: list[int], left: int, right: int) -> None:
""" 快速排序 """
"""快速排序"""
# 子数组长度为 1 时终止递归
if left >= right:
return
@@ -549,7 +549,7 @@ comments: true
```python title="quick_sort.py"
def median_three(self, nums: list[int], left: int, mid: int, right: int) -> int:
""" 选取三个元素的中位数 """
"""选取三个元素的中位数"""
# 此处使用异或运算来简化代码
# 异或规则为 0 ^ 0 = 1 ^ 1 = 0, 0 ^ 1 = 1 ^ 0 = 1
if (nums[left] < nums[mid]) ^ (nums[left] < nums[right]):
@@ -559,7 +559,7 @@ comments: true
return right
def partition(self, nums: list[int], left: int, right: int) -> int:
""" 哨兵划分(三数取中值) """
"""哨兵划分(三数取中值)"""
# 以 nums[left] 作为基准数
med: int = self.median_three(nums, left, (left + right) // 2, right)
# 将中位数交换至数组最左端
@@ -842,7 +842,7 @@ comments: true
```python title="quick_sort.py"
def quick_sort(self, nums: list[int], left: int, right: int) -> None:
""" 快速排序(尾递归优化) """
"""快速排序(尾递归优化)"""
# 子数组长度为 1 时终止
while left < right:
# 哨兵划分操作
@@ -850,10 +850,10 @@ comments: true
# 对两个子数组中较短的那个执行快排
if pivot - left < right - pivot:
self.quick_sort(nums, left, pivot - 1) # 递归排序左子数组
left = pivot + 1 # 剩余待排序区间为 [pivot + 1, right]
left = pivot + 1 # 剩余待排序区间为 [pivot + 1, right]
else:
self.quick_sort(nums, pivot + 1, right) # 递归排序右子数组
right = pivot - 1 # 剩余待排序区间为 [left, pivot - 1]
right = pivot - 1 # 剩余待排序区间为 [left, pivot - 1]
```
=== "Go"
+6 -6
View File
@@ -136,19 +136,19 @@ $$
```python title="radix_sort.py"
def digit(num: int, exp: int) -> int:
""" 获取元素 num 的第 k 位,其中 exp = 10^(k-1) """
"""获取元素 num 的第 k 位,其中 exp = 10^(k-1)"""
# 传入 exp 而非 k 可以避免在此重复执行昂贵的次方计算
return (num // exp) % 10
def counting_sort_digit(nums: list[int], exp: int) -> None:
""" 计数排序(根据 nums 第 k 位排序) """
"""计数排序(根据 nums 第 k 位排序)"""
# 十进制的位范围为 0~9 ,因此需要长度为 10 的桶
counter = [0] * 10
n = len(nums)
# 统计 0~9 各数字的出现次数
for i in range(n):
d = digit(nums[i], exp) # 获取 nums[i] 第 k 位,记为 d
counter[d] += 1 # 统计数字 d 的出现次数
counter[d] += 1 # 统计数字 d 的出现次数
# 求前缀和,将“出现个数”转换为“数组索引”
for i in range(1, 10):
counter[i] += counter[i - 1]
@@ -157,14 +157,14 @@ $$
for i in range(n - 1, -1, -1):
d = digit(nums[i], exp)
j = counter[d] - 1 # 获取 d 在数组中的索引 j
res[j] = nums[i] # 将当前元素填入索引 j
counter[d] -= 1 # 将 d 的数量减 1
res[j] = nums[i] # 将当前元素填入索引 j
counter[d] -= 1 # 将 d 的数量减 1
# 使用结果覆盖原数组 nums
for i in range(n):
nums[i] = res[i]
def radix_sort(nums: list[int]) -> None:
""" 基数排序 """
"""基数排序"""
# 获取数组的最大元素,用于判断最大位数
m = max(nums)
# 按照从低位到高位的顺序遍历