mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-02 21:24:53 +08:00
Polish the chapter
introduction, computational complexity.
This commit is contained in:
@ -121,31 +121,31 @@ if __name__ == "__main__":
|
||||
print("输入数据大小 n =", n)
|
||||
|
||||
count: int = constant(n)
|
||||
print("常数阶的计算操作数量 =", count)
|
||||
print("常数阶的操作数量 =", count)
|
||||
|
||||
count: int = linear(n)
|
||||
print("线性阶的计算操作数量 =", count)
|
||||
print("线性阶的操作数量 =", count)
|
||||
count: int = array_traversal([0] * n)
|
||||
print("线性阶(遍历数组)的计算操作数量 =", count)
|
||||
print("线性阶(遍历数组)的操作数量 =", count)
|
||||
|
||||
count: int = quadratic(n)
|
||||
print("平方阶的计算操作数量 =", count)
|
||||
print("平方阶的操作数量 =", count)
|
||||
nums = [i for i in range(n, 0, -1)] # [n, n-1, ..., 2, 1]
|
||||
count: int = bubble_sort(nums)
|
||||
print("平方阶(冒泡排序)的计算操作数量 =", count)
|
||||
print("平方阶(冒泡排序)的操作数量 =", count)
|
||||
|
||||
count: int = exponential(n)
|
||||
print("指数阶(循环实现)的计算操作数量 =", count)
|
||||
print("指数阶(循环实现)的操作数量 =", count)
|
||||
count: int = exp_recur(n)
|
||||
print("指数阶(递归实现)的计算操作数量 =", count)
|
||||
print("指数阶(递归实现)的操作数量 =", count)
|
||||
|
||||
count: int = logarithmic(n)
|
||||
print("对数阶(循环实现)的计算操作数量 =", count)
|
||||
print("对数阶(循环实现)的操作数量 =", count)
|
||||
count: int = log_recur(n)
|
||||
print("对数阶(递归实现)的计算操作数量 =", count)
|
||||
print("对数阶(递归实现)的操作数量 =", count)
|
||||
|
||||
count: int = linear_log_recur(n)
|
||||
print("线性对数阶(递归实现)的计算操作数量 =", count)
|
||||
print("线性对数阶(递归实现)的操作数量 =", count)
|
||||
|
||||
count: int = factorial_recur(n)
|
||||
print("阶乘阶(递归实现)的计算操作数量 =", count)
|
||||
print("阶乘阶(递归实现)的操作数量 =", count)
|
||||
|
||||
@ -61,7 +61,7 @@ class MaxHeap:
|
||||
while True:
|
||||
# 获取节点 i 的父节点
|
||||
p = self.parent(i)
|
||||
# 当“越过根节点”或“节点无需修复”时,结束堆化
|
||||
# 当“越过根节点”或“节点无须修复”时,结束堆化
|
||||
if p < 0 or self.max_heap[i] <= self.max_heap[p]:
|
||||
break
|
||||
# 交换两节点
|
||||
@ -92,7 +92,7 @@ class MaxHeap:
|
||||
ma = l
|
||||
if r < self.size() and self.max_heap[r] > self.max_heap[ma]:
|
||||
ma = r
|
||||
# 若节点 i 最大或索引 l, r 越界,则无需继续堆化,跳出
|
||||
# 若节点 i 最大或索引 l, r 越界,则无须继续堆化,跳出
|
||||
if ma == i:
|
||||
break
|
||||
# 交换两节点
|
||||
|
||||
@ -11,7 +11,7 @@ def binary_search(nums: list[int], target: int) -> int:
|
||||
i, j = 0, len(nums) - 1
|
||||
# 循环,当搜索区间为空时跳出(当 i > j 时为空)
|
||||
while i <= j:
|
||||
# 理论上 Python 的数字可以无限大(取决于内存大小),无需考虑大数越界问题
|
||||
# 理论上 Python 的数字可以无限大(取决于内存大小),无须考虑大数越界问题
|
||||
m = (i + j) // 2 # 计算中点索引 m
|
||||
if nums[m] < target:
|
||||
i = m + 1 # 此情况说明 target 在区间 [m+1, j] 中
|
||||
|
||||
@ -16,7 +16,7 @@ def sift_down(nums: list[int], n: int, i: int):
|
||||
ma = l
|
||||
if r < n and nums[r] > nums[ma]:
|
||||
ma = r
|
||||
# 若节点 i 最大或索引 l, r 越界,则无需继续堆化,跳出
|
||||
# 若节点 i 最大或索引 l, r 越界,则无须继续堆化,跳出
|
||||
if ma == i:
|
||||
break
|
||||
# 交换两节点
|
||||
|
||||
@ -85,7 +85,7 @@ class AVLTree:
|
||||
# 先右旋后左旋
|
||||
node.right = self.__right_rotate(node.right)
|
||||
return self.__left_rotate(node)
|
||||
# 平衡树,无需旋转,直接返回
|
||||
# 平衡树,无须旋转,直接返回
|
||||
return node
|
||||
|
||||
def insert(self, val):
|
||||
|
||||
Reference in New Issue
Block a user