This commit is contained in:
krahets
2023-07-25 16:42:55 +08:00
parent 0760e0865e
commit 902087ec81
23 changed files with 154 additions and 177 deletions

View File

@@ -84,7 +84,7 @@ comments: true
=== "Python"
```python title="bubble_sort.py"
def bubble_sort(nums: list[int]) -> None:
def bubble_sort(nums: list[int]):
"""冒泡排序"""
n = len(nums)
# 外循环:未排序区间为 [0, i]
@@ -311,7 +311,7 @@ comments: true
=== "Python"
```python title="bubble_sort.py"
def bubble_sort_with_flag(nums: list[int]) -> None:
def bubble_sort_with_flag(nums: list[int]):
"""冒泡排序(标志优化)"""
n = len(nums)
# 外循环:未排序区间为 [0, i]

View File

@@ -86,7 +86,7 @@ comments: true
=== "Python"
```python title="bucket_sort.py"
def bucket_sort(nums: list[float]) -> None:
def bucket_sort(nums: list[float]):
"""桶排序"""
# 初始化 k = n/2 个桶,预期向每个桶分配 2 个元素
k = len(nums) // 2

View File

@@ -75,7 +75,7 @@ comments: true
=== "Python"
```python title="counting_sort.py"
def counting_sort_naive(nums: list[int]) -> None:
def counting_sort_naive(nums: list[int]):
"""计数排序"""
# 简单实现,无法用于排序对象
# 1. 统计数组最大元素 m
@@ -416,7 +416,7 @@ $$
=== "Python"
```python title="counting_sort.py"
def counting_sort(nums: list[int]) -> None:
def counting_sort(nums: list[int]):
"""计数排序"""
# 完整实现,可排序对象,并且是稳定排序
# 1. 统计数组最大元素 m

View File

@@ -66,7 +66,7 @@ comments: true
=== "Python"
```python title="insertion_sort.py"
def insertion_sort(nums: list[int]) -> None:
def insertion_sort(nums: list[int]):
"""插入排序"""
# 外循环:已排序区间为 [0, i-1]
for i in range(1, len(nums)):

View File

@@ -146,7 +146,7 @@ comments: true
=== "Python"
```python title="merge_sort.py"
def merge(nums: list[int], left: int, mid: int, right: int) -> None:
def merge(nums: list[int], left: int, mid: int, right: int):
"""合并左子数组和右子数组"""
# 左子数组区间 [left, mid]
# 右子数组区间 [mid + 1, right]
@@ -176,7 +176,7 @@ comments: true
nums[k] = tmp[j]
j += 1
def merge_sort(nums: list[int], left: int, right: int) -> None:
def merge_sort(nums: list[int], left: int, right: int):
"""归并排序"""
# 终止条件
if left >= right:

View File

@@ -382,7 +382,7 @@ comments: true
=== "Python"
```python title="quick_sort.py"
def quick_sort(self, nums: list[int], left: int, right: int) -> None:
def quick_sort(self, nums: list[int], left: int, right: int):
"""快速排序"""
# 子数组长度为 1 时终止递归
if left >= right:
@@ -1027,7 +1027,7 @@ comments: true
=== "Python"
```python title="quick_sort.py"
def quick_sort(self, nums: list[int], left: int, right: int) -> None:
def quick_sort(self, nums: list[int], left: int, right: int):
"""快速排序(尾递归优化)"""
# 子数组长度为 1 时终止
while left < right:

View File

@@ -141,7 +141,7 @@ $$
# 传入 exp 而非 k 可以避免在此重复执行昂贵的次方计算
return (num // exp) % 10
def counting_sort_digit(nums: list[int], exp: int) -> None:
def counting_sort_digit(nums: list[int], exp: int):
"""计数排序(根据 nums 第 k 位排序)"""
# 十进制的位范围为 0~9 ,因此需要长度为 10 的桶
counter = [0] * 10
@@ -164,7 +164,7 @@ $$
for i in range(n):
nums[i] = res[i]
def radix_sort(nums: list[int]) -> None:
def radix_sort(nums: list[int]):
"""基数排序"""
# 获取数组的最大元素,用于判断最大位数
m = max(nums)