This commit is contained in:
krahets
2023-05-22 22:01:18 +08:00
parent 29807a3761
commit be77ba7a70
16 changed files with 60 additions and 58 deletions
+3 -3
View File
@@ -87,7 +87,7 @@ comments: true
```python title="bubble_sort.py"
def bubble_sort(nums: list[int]) -> None:
"""冒泡排序"""
n: int = len(nums)
n = len(nums)
# 外循环:待排序元素数量为 n-1, n-2, ..., 1
for i in range(n - 1, 0, -1):
# 内循环:冒泡操作
@@ -302,10 +302,10 @@ comments: true
```python title="bubble_sort.py"
def bubble_sort_with_flag(nums: list[int]) -> None:
"""冒泡排序(标志优化)"""
n: int = len(nums)
n = len(nums)
# 外循环:待排序元素数量为 n-1, n-2, ..., 1
for i in range(n - 1, 0, -1):
flag: bool = False # 初始化标志位
flag = False # 初始化标志位
# 内循环:冒泡操作
for j in range(i):
if nums[j] > nums[j + 1]: