mirror of
https://github.com/krahets/hello-algo.git
synced 2025-12-19 07:17:54 +08:00
1. Add the building util of Python
for the markdown docs. 2. Update the deploy.sh
This commit is contained in:
33
docs/chapter_sorting/bubble_sort.md
Normal file → Executable file
33
docs/chapter_sorting/bubble_sort.md
Normal file → Executable file
@@ -15,31 +15,24 @@ comments: true
|
||||
完成此次冒泡操作后,**数组最大元素已在正确位置,接下来只需排序剩余 $n - 1$ 个元素**。
|
||||
|
||||
=== "Step 1"
|
||||
|
||||

|
||||
|
||||
=== "Step 2"
|
||||
|
||||

|
||||
|
||||
=== "Step 3"
|
||||
|
||||

|
||||
|
||||
=== "Step 4"
|
||||
|
||||

|
||||
|
||||
=== "Step 5"
|
||||
|
||||

|
||||
|
||||
=== "Step 6"
|
||||
|
||||

|
||||
|
||||
=== "Step 7"
|
||||
|
||||

|
||||
|
||||
<p align="center"> Fig. 冒泡操作 </p>
|
||||
@@ -96,16 +89,7 @@ comments: true
|
||||
=== "Python"
|
||||
|
||||
```python title="bubble_sort.py"
|
||||
""" 冒泡排序 """
|
||||
def bubble_sort(nums):
|
||||
n = len(nums)
|
||||
# 外循环:待排序元素数量为 n-1, n-2, ..., 1
|
||||
for i in range(n - 1, 0, -1):
|
||||
# 内循环:冒泡操作
|
||||
for j in range(i):
|
||||
if nums[j] > nums[j + 1]:
|
||||
# 交换 nums[j] 与 nums[j + 1]
|
||||
nums[j], nums[j + 1] = nums[j + 1], nums[j]
|
||||
[class]{}-[func]{bubble_sort}
|
||||
```
|
||||
|
||||
=== "Go"
|
||||
@@ -304,20 +288,7 @@ comments: true
|
||||
=== "Python"
|
||||
|
||||
```python title="bubble_sort.py"
|
||||
""" 冒泡排序(标志优化) """
|
||||
def bubble_sort_with_flag(nums):
|
||||
n = len(nums)
|
||||
# 外循环:待排序元素数量为 n-1, n-2, ..., 1
|
||||
for i in range(n - 1, 0, -1):
|
||||
flag = False # 初始化标志位
|
||||
# 内循环:冒泡操作
|
||||
for j in range(i):
|
||||
if nums[j] > nums[j + 1]:
|
||||
# 交换 nums[j] 与 nums[j + 1]
|
||||
nums[j], nums[j + 1] = nums[j + 1], nums[j]
|
||||
flag = True # 记录交换元素
|
||||
if not flag:
|
||||
break # 此轮冒泡未交换任何元素,直接跳出
|
||||
[class]{}-[func]{bubble_sort_with_flag}
|
||||
```
|
||||
|
||||
=== "Go"
|
||||
|
||||
12
docs/chapter_sorting/insertion_sort.md
Normal file → Executable file
12
docs/chapter_sorting/insertion_sort.md
Normal file → Executable file
@@ -63,17 +63,7 @@ comments: true
|
||||
=== "Python"
|
||||
|
||||
```python title="insertion_sort.py"
|
||||
""" 插入排序 """
|
||||
def insertion_sort(nums):
|
||||
# 外循环:base = nums[1], nums[2], ..., nums[n-1]
|
||||
for i in range(1, len(nums)):
|
||||
base = nums[i]
|
||||
j = i - 1
|
||||
# 内循环:将 base 插入到左边的正确位置
|
||||
while j >= 0 and nums[j] > base:
|
||||
nums[j + 1] = nums[j] # 1. 将 nums[j] 向右移动一位
|
||||
j -= 1
|
||||
nums[j + 1] = base # 2. 将 base 赋值到正确位置
|
||||
[class]{}-[func]{insertion_sort}
|
||||
```
|
||||
|
||||
=== "Go"
|
||||
|
||||
41
docs/chapter_sorting/merge_sort.md
Normal file → Executable file
41
docs/chapter_sorting/merge_sort.md
Normal file → Executable file
@@ -150,46 +150,9 @@ comments: true
|
||||
=== "Python"
|
||||
|
||||
```python title="merge_sort.py"
|
||||
"""
|
||||
合并左子数组和右子数组
|
||||
左子数组区间 [left, mid]
|
||||
右子数组区间 [mid + 1, right]
|
||||
"""
|
||||
def merge(nums, left, mid, right):
|
||||
# 初始化辅助数组 借助 copy模块
|
||||
tmp = nums[left:right + 1]
|
||||
# 左子数组的起始索引和结束索引
|
||||
left_start, left_end = left - left, mid - left
|
||||
# 右子数组的起始索引和结束索引
|
||||
right_start, right_end = mid + 1 - left, right - left
|
||||
# i, j 分别指向左子数组、右子数组的首元素
|
||||
i, j = left_start, right_start
|
||||
# 通过覆盖原数组 nums 来合并左子数组和右子数组
|
||||
for k in range(left, right + 1):
|
||||
# 若“左子数组已全部合并完”,则选取右子数组元素,并且 j++
|
||||
if i > left_end:
|
||||
nums[k] = tmp[j]
|
||||
j += 1
|
||||
# 否则,若“右子数组已全部合并完”或“左子数组元素 <= 右子数组元素”,则选取左子数组元素,并且 i++
|
||||
elif j > right_end or tmp[i] <= tmp[j]:
|
||||
nums[k] = tmp[i]
|
||||
i += 1
|
||||
# 否则,若“左右子数组都未全部合并完”且“左子数组元素 > 右子数组元素”,则选取右子数组元素,并且 j++
|
||||
else:
|
||||
nums[k] = tmp[j]
|
||||
j += 1
|
||||
[class]{}-[func]{merge}
|
||||
|
||||
""" 归并排序 """
|
||||
def merge_sort(nums, left, right):
|
||||
# 终止条件
|
||||
if left >= right:
|
||||
return # 当子数组长度为 1 时终止递归
|
||||
# 划分阶段
|
||||
mid = (left + right) // 2 # 计算中点
|
||||
merge_sort(nums, left, mid) # 递归左子数组
|
||||
merge_sort(nums, mid + 1, right) # 递归右子数组
|
||||
# 合并阶段
|
||||
merge(nums, left, mid, right)
|
||||
[class]{}-[func]{merge_sort}
|
||||
```
|
||||
|
||||
=== "Go"
|
||||
|
||||
59
docs/chapter_sorting/quick_sort.md
Normal file → Executable file
59
docs/chapter_sorting/quick_sort.md
Normal file → Executable file
@@ -98,20 +98,7 @@ comments: true
|
||||
=== "Python"
|
||||
|
||||
```python title="quick_sort.py"
|
||||
""" 哨兵划分 """
|
||||
def partition(self, nums, left, right):
|
||||
# 以 nums[left] 作为基准数
|
||||
i, j = left, right
|
||||
while i < j:
|
||||
while i < j and nums[j] >= nums[left]:
|
||||
j -= 1 # 从右向左找首个小于基准数的元素
|
||||
while i < j and nums[i] <= nums[left]:
|
||||
i += 1 # 从左向右找首个大于基准数的元素
|
||||
# 元素交换
|
||||
nums[i], nums[j] = nums[j], nums[i]
|
||||
# 将基准数交换至两子数组的分界线
|
||||
nums[i], nums[left] = nums[left], nums[i]
|
||||
return i # 返回基准数的索引
|
||||
[class]{QuickSort}-[func]{partition}
|
||||
```
|
||||
|
||||
=== "Go"
|
||||
@@ -316,16 +303,7 @@ comments: true
|
||||
=== "Python"
|
||||
|
||||
```python title="quick_sort.py"
|
||||
""" 快速排序 """
|
||||
def quick_sort(self, nums, left, right):
|
||||
# 子数组长度为 1 时终止递归
|
||||
if left >= right:
|
||||
return
|
||||
# 哨兵划分
|
||||
pivot = self.partition(nums, left, right)
|
||||
# 递归左子数组、右子数组
|
||||
self.quick_sort(nums, left, pivot - 1)
|
||||
self.quick_sort(nums, pivot + 1, right)
|
||||
[class]{QuickSort}-[func]{quick_sort}
|
||||
```
|
||||
|
||||
=== "Go"
|
||||
@@ -509,24 +487,9 @@ comments: true
|
||||
=== "Python"
|
||||
|
||||
```python title="quick_sort.py"
|
||||
""" 选取三个元素的中位数 """
|
||||
def median_three(self, nums, left, mid, right):
|
||||
# 使用了异或操作来简化代码
|
||||
# 异或规则为 0 ^ 0 = 1 ^ 1 = 0, 0 ^ 1 = 1 ^ 0 = 1
|
||||
if (nums[left] < nums[mid]) ^ (nums[left] < nums[right]):
|
||||
return left
|
||||
elif (nums[mid] < nums[left]) ^ (nums[mid] > nums[right]):
|
||||
return mid
|
||||
return right
|
||||
[class]{QuickSortMedian}-[func]{median_three}
|
||||
|
||||
""" 哨兵划分(三数取中值) """
|
||||
def partition(self, nums, left, right):
|
||||
# 以 nums[left] 作为基准数
|
||||
med = self.median_three(nums, left, (left + right) // 2, right)
|
||||
# 将中位数交换至数组最左端
|
||||
nums[left], nums[med] = nums[med], nums[left]
|
||||
# 以 nums[left] 作为基准数
|
||||
# 下同省略...
|
||||
[class]{QuickSortMedian}-[func]{partition}
|
||||
```
|
||||
|
||||
=== "Go"
|
||||
@@ -721,19 +684,7 @@ comments: true
|
||||
=== "Python"
|
||||
|
||||
```python title="quick_sort.py"
|
||||
""" 快速排序(尾递归优化) """
|
||||
def quick_sort(self, nums, left, right):
|
||||
# 子数组长度为 1 时终止
|
||||
while left < right:
|
||||
# 哨兵划分操作
|
||||
pivot = self.partition(nums, left, right)
|
||||
# 对两个子数组中较短的那个执行快排
|
||||
if pivot - left < right - pivot:
|
||||
self.quick_sort(nums, left, pivot - 1) # 递归排序左子数组
|
||||
left = pivot + 1 # 剩余待排序区间为 [pivot + 1, right]
|
||||
else:
|
||||
self.quick_sort(nums, pivot + 1, right) # 递归排序右子数组
|
||||
right = pivot - 1 # 剩余待排序区间为 [left, pivot - 1]
|
||||
[class]{QuickSortTailCall}-[func]{quick_sort}
|
||||
```
|
||||
|
||||
=== "Go"
|
||||
|
||||
Reference in New Issue
Block a user