mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-02 04:31:55 +08:00
1. Add the building util of Python
for the markdown docs. 2. Update the deploy.sh
This commit is contained in:
111
docs/chapter_computational_complexity/time_complexity.md
Normal file → Executable file
111
docs/chapter_computational_complexity/time_complexity.md
Normal file → Executable file
@ -821,13 +821,7 @@ $$
|
||||
=== "Python"
|
||||
|
||||
```python title="time_complexity.py"
|
||||
""" 常数阶 """
|
||||
def constant(n):
|
||||
count = 0
|
||||
size = 100000
|
||||
for _ in range(size):
|
||||
count += 1
|
||||
return count
|
||||
[class]{}-[func]{constant}
|
||||
```
|
||||
|
||||
=== "Go"
|
||||
@ -958,12 +952,7 @@ $$
|
||||
=== "Python"
|
||||
|
||||
```python title="time_complexity.py"
|
||||
""" 线性阶 """
|
||||
def linear(n):
|
||||
count = 0
|
||||
for _ in range(n):
|
||||
count += 1
|
||||
return count
|
||||
[class]{}-[func]{linear}
|
||||
```
|
||||
|
||||
=== "Go"
|
||||
@ -1091,13 +1080,7 @@ $$
|
||||
=== "Python"
|
||||
|
||||
```python title="time_complexity.py"
|
||||
""" 线性阶(遍历数组)"""
|
||||
def array_traversal(nums):
|
||||
count = 0
|
||||
# 循环次数与数组长度成正比
|
||||
for num in nums:
|
||||
count += 1
|
||||
return count
|
||||
[class]{}-[func]{array_traversal}
|
||||
```
|
||||
|
||||
=== "Go"
|
||||
@ -1239,14 +1222,7 @@ $$
|
||||
=== "Python"
|
||||
|
||||
```python title="time_complexity.py"
|
||||
""" 平方阶 """
|
||||
def quadratic(n):
|
||||
count = 0
|
||||
# 循环次数与数组长度成平方关系
|
||||
for i in range(n):
|
||||
for j in range(n):
|
||||
count += 1
|
||||
return count
|
||||
[class]{}-[func]{quadratic}
|
||||
```
|
||||
|
||||
=== "Go"
|
||||
@ -1425,20 +1401,7 @@ $$
|
||||
=== "Python"
|
||||
|
||||
```python title="time_complexity.py"
|
||||
""" 平方阶(冒泡排序)"""
|
||||
def bubble_sort(nums):
|
||||
count = 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]
|
||||
nums[j] = nums[j + 1]
|
||||
nums[j + 1] = tmp
|
||||
count += 3 # 元素交换包含 3 个单元操作
|
||||
return count
|
||||
[class]{}-[func]{bubble_sort}
|
||||
```
|
||||
|
||||
=== "Go"
|
||||
@ -1658,16 +1621,7 @@ $$
|
||||
=== "Python"
|
||||
|
||||
```python title="time_complexity.py"
|
||||
""" 指数阶(循环实现)"""
|
||||
def exponential(n):
|
||||
count, base = 0, 1
|
||||
# cell 每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1)
|
||||
for _ in range(n):
|
||||
for _ in range(base):
|
||||
count += 1
|
||||
base *= 2
|
||||
# count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1
|
||||
return count
|
||||
[class]{}-[func]{exponential}
|
||||
```
|
||||
|
||||
=== "Go"
|
||||
@ -1836,10 +1790,7 @@ $$
|
||||
=== "Python"
|
||||
|
||||
```python title="time_complexity.py"
|
||||
""" 指数阶(递归实现)"""
|
||||
def exp_recur(n):
|
||||
if n == 1: return 1
|
||||
return exp_recur(n - 1) + exp_recur(n - 1) + 1
|
||||
[class]{}-[func]{exp_recur}
|
||||
```
|
||||
|
||||
=== "Go"
|
||||
@ -1957,13 +1908,7 @@ $$
|
||||
=== "Python"
|
||||
|
||||
```python title="time_complexity.py"
|
||||
""" 对数阶(循环实现)"""
|
||||
def logarithmic(n):
|
||||
count = 0
|
||||
while n > 1:
|
||||
n = n / 2
|
||||
count += 1
|
||||
return count
|
||||
[class]{}-[func]{logarithmic}
|
||||
```
|
||||
|
||||
=== "Go"
|
||||
@ -2099,10 +2044,7 @@ $$
|
||||
=== "Python"
|
||||
|
||||
```python title="time_complexity.py"
|
||||
""" 对数阶(递归实现)"""
|
||||
def log_recur(n):
|
||||
if n <= 1: return 0
|
||||
return log_recur(n / 2) + 1
|
||||
[class]{}-[func]{log_recur}
|
||||
```
|
||||
|
||||
=== "Go"
|
||||
@ -2220,14 +2162,7 @@ $$
|
||||
=== "Python"
|
||||
|
||||
```python title="time_complexity.py"
|
||||
""" 线性对数阶 """
|
||||
def linear_log_recur(n):
|
||||
if n <= 1: return 1
|
||||
count = linear_log_recur(n // 2) + \
|
||||
linear_log_recur(n // 2)
|
||||
for _ in range(n):
|
||||
count += 1
|
||||
return count
|
||||
[class]{}-[func]{linear_log_recur}
|
||||
```
|
||||
|
||||
=== "Go"
|
||||
@ -2387,14 +2322,7 @@ $$
|
||||
=== "Python"
|
||||
|
||||
```python title="time_complexity.py"
|
||||
""" 阶乘阶(递归实现)"""
|
||||
def factorial_recur(n):
|
||||
if n == 0: return 1
|
||||
count = 0
|
||||
# 从 1 个分裂出 n 个
|
||||
for _ in range(n):
|
||||
count += factorial_recur(n - 1)
|
||||
return count
|
||||
[class]{}-[func]{factorial_recur}
|
||||
```
|
||||
|
||||
=== "Go"
|
||||
@ -2587,22 +2515,9 @@ $$
|
||||
=== "Python"
|
||||
|
||||
```python title="worst_best_time_complexity.py"
|
||||
""" 生成一个数组,元素为: 1, 2, ..., n ,顺序被打乱 """
|
||||
def random_numbers(n):
|
||||
# 生成数组 nums =: 1, 2, 3, ..., n
|
||||
nums = [i for i in range(1, n + 1)]
|
||||
# 随机打乱数组元素
|
||||
random.shuffle(nums)
|
||||
return nums
|
||||
[class]{}-[func]{random_numbers}
|
||||
|
||||
""" 查找数组 nums 中数字 1 所在索引 """
|
||||
def find_one(nums):
|
||||
for i in range(len(nums)):
|
||||
# 当元素 1 在数组头部时,达到最佳时间复杂度 O(1)
|
||||
# 当元素 1 在数组尾部时,达到最差时间复杂度 O(n)
|
||||
if nums[i] == 1:
|
||||
return i
|
||||
return -1
|
||||
[class]{}-[func]{find_one}
|
||||
```
|
||||
|
||||
=== "Go"
|
||||
|
||||
Reference in New Issue
Block a user