mirror of
https://github.com/krahets/hello-algo.git
synced 2025-07-07 06:44:57 +08:00
Add typing annotations to Python codes. (#411)
This commit is contained in:
@ -8,56 +8,57 @@ import sys, os.path as osp
|
||||
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
|
||||
from modules import *
|
||||
|
||||
def constant(n):
|
||||
def constant(n: int) -> int:
|
||||
""" 常数阶 """
|
||||
count = 0
|
||||
size = 100000
|
||||
count: int = 0
|
||||
size: int = 100000
|
||||
for _ in range(size):
|
||||
count += 1
|
||||
return count
|
||||
|
||||
def linear(n):
|
||||
def linear(n: int) -> int:
|
||||
""" 线性阶 """
|
||||
count = 0
|
||||
count: int = 0
|
||||
for _ in range(n):
|
||||
count += 1
|
||||
return count
|
||||
|
||||
def array_traversal(nums):
|
||||
def array_traversal(nums: List[int]) -> int:
|
||||
""" 线性阶(遍历数组)"""
|
||||
count = 0
|
||||
count: int = 0
|
||||
# 循环次数与数组长度成正比
|
||||
for num in nums:
|
||||
count += 1
|
||||
return count
|
||||
|
||||
def quadratic(n):
|
||||
def quadratic(n: int) -> int:
|
||||
""" 平方阶 """
|
||||
count = 0
|
||||
count: int = 0
|
||||
# 循环次数与数组长度成平方关系
|
||||
for i in range(n):
|
||||
for j in range(n):
|
||||
count += 1
|
||||
return count
|
||||
|
||||
def bubble_sort(nums):
|
||||
def bubble_sort(nums: List[int]) -> int:
|
||||
""" 平方阶(冒泡排序)"""
|
||||
count = 0 # 计数器
|
||||
count: int = 0 # 计数器
|
||||
# 外循环:待排序元素数量为 n-1, n-2, ..., 1
|
||||
for i in range(len(nums) - 1, 0, -1):
|
||||
# 内循环:冒泡操作
|
||||
for j in range(i):
|
||||
if nums[j] > nums[j + 1]:
|
||||
# 交换 nums[j] 与 nums[j + 1]
|
||||
tmp = nums[j]
|
||||
tmp: int = nums[j]
|
||||
nums[j] = nums[j + 1]
|
||||
nums[j + 1] = tmp
|
||||
count += 3 # 元素交换包含 3 个单元操作
|
||||
return count
|
||||
|
||||
def exponential(n):
|
||||
def exponential(n: int) -> int:
|
||||
""" 指数阶(循环实现)"""
|
||||
count, base = 0, 1
|
||||
count: int = 0
|
||||
base: int = 1
|
||||
# cell 每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1)
|
||||
for _ in range(n):
|
||||
for _ in range(base):
|
||||
@ -66,37 +67,37 @@ def exponential(n):
|
||||
# count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1
|
||||
return count
|
||||
|
||||
def exp_recur(n):
|
||||
def exp_recur(n: int) -> int:
|
||||
""" 指数阶(递归实现)"""
|
||||
if n == 1: return 1
|
||||
return exp_recur(n - 1) + exp_recur(n - 1) + 1
|
||||
|
||||
def logarithmic(n):
|
||||
def logarithmic(n: float) -> int:
|
||||
""" 对数阶(循环实现)"""
|
||||
count = 0
|
||||
count: int = 0
|
||||
while n > 1:
|
||||
n = n / 2
|
||||
count += 1
|
||||
return count
|
||||
|
||||
def log_recur(n):
|
||||
def log_recur(n: float) -> int:
|
||||
""" 对数阶(递归实现)"""
|
||||
if n <= 1: return 0
|
||||
return log_recur(n / 2) + 1
|
||||
|
||||
def linear_log_recur(n):
|
||||
def linear_log_recur(n: float) -> int:
|
||||
""" 线性对数阶 """
|
||||
if n <= 1: return 1
|
||||
count = linear_log_recur(n // 2) + \
|
||||
linear_log_recur(n // 2)
|
||||
count: int = linear_log_recur(n // 2) + \
|
||||
linear_log_recur(n // 2)
|
||||
for _ in range(n):
|
||||
count += 1
|
||||
return count
|
||||
|
||||
def factorial_recur(n):
|
||||
def factorial_recur(n: int) -> int:
|
||||
""" 阶乘阶(递归实现)"""
|
||||
if n == 0: return 1
|
||||
count = 0
|
||||
count: int = 0
|
||||
# 从 1 个分裂出 n 个
|
||||
for _ in range(n):
|
||||
count += factorial_recur(n - 1)
|
||||
@ -109,32 +110,32 @@ if __name__ == "__main__":
|
||||
n = 8
|
||||
print("输入数据大小 n =", n)
|
||||
|
||||
count = constant(n)
|
||||
count: int = constant(n)
|
||||
print("常数阶的计算操作数量 =", count)
|
||||
|
||||
count = linear(n)
|
||||
count: int = linear(n)
|
||||
print("线性阶的计算操作数量 =", count)
|
||||
count = array_traversal([0] * n)
|
||||
count: int = array_traversal([0] * n)
|
||||
print("线性阶(遍历数组)的计算操作数量 =", count)
|
||||
|
||||
count = quadratic(n)
|
||||
count: int = quadratic(n)
|
||||
print("平方阶的计算操作数量 =", count)
|
||||
nums = [i for i in range(n, 0, -1)] # [n,n-1,...,2,1]
|
||||
count = bubble_sort(nums)
|
||||
nums: List[int] = [i for i in range(n, 0, -1)] # [n,n-1,...,2,1]
|
||||
count: int = bubble_sort(nums)
|
||||
print("平方阶(冒泡排序)的计算操作数量 =", count)
|
||||
|
||||
count = exponential(n)
|
||||
count: int = exponential(n)
|
||||
print("指数阶(循环实现)的计算操作数量 =", count)
|
||||
count = exp_recur(n)
|
||||
count: int = exp_recur(n)
|
||||
print("指数阶(递归实现)的计算操作数量 =", count)
|
||||
|
||||
count = logarithmic(n)
|
||||
count: int = logarithmic(n)
|
||||
print("对数阶(循环实现)的计算操作数量 =", count)
|
||||
count = log_recur(n)
|
||||
count: int = log_recur(n)
|
||||
print("对数阶(递归实现)的计算操作数量 =", count)
|
||||
|
||||
count = linear_log_recur(n)
|
||||
count: int = linear_log_recur(n)
|
||||
print("线性对数阶(递归实现)的计算操作数量 =", count)
|
||||
|
||||
count = factorial_recur(n)
|
||||
count: int = factorial_recur(n)
|
||||
print("阶乘阶(递归实现)的计算操作数量 =", count)
|
||||
|
Reference in New Issue
Block a user