Retranslate the complete English edition from Simplified Chinese (#2010)

* Retranslate the complete English edition from Simplified Chinese

* Polish three English explanations after reader-focused review
This commit is contained in:
Yudong Jin
2026-10-31 03:35:51 +08:00
committed by GitHub
parent ef46e2ed95
commit 14c314c08c
1502 changed files with 29459 additions and 23517 deletions

View File

@@ -6,7 +6,7 @@ Author: krahets (krahets@163.com)
def constant(n: int) -> int:
"""Constant order"""
"""Constant complexity"""
count = 0
size = 100000
for _ in range(size):
@@ -15,7 +15,7 @@ def constant(n: int) -> int:
def linear(n: int) -> int:
"""Linear order"""
"""Linear complexity"""
count = 0
for _ in range(n):
count += 1
@@ -23,18 +23,18 @@ def linear(n: int) -> int:
def array_traversal(nums: list[int]) -> int:
"""Linear order (traversing array)"""
"""Linear complexity (array traversal)"""
count = 0
# Number of iterations is proportional to the array length
# The number of iterations is proportional to the array length
for num in nums:
count += 1
return count
def quadratic(n: int) -> int:
"""Quadratic order"""
"""Quadratic complexity"""
count = 0
# Number of iterations is quadratically related to the data size n
# The number of iterations is proportional to the square of the input size n
for i in range(n):
for j in range(n):
count += 1
@@ -42,26 +42,26 @@ def quadratic(n: int) -> int:
def bubble_sort(nums: list[int]) -> int:
"""Quadratic order (bubble sort)"""
"""Quadratic complexity (bubble sort)"""
count = 0 # Counter
# Outer loop: unsorted range is [0, i]
# Outer loop: the unsorted range is [0, i]
for i in range(len(nums) - 1, 0, -1):
# Inner loop: swap the largest element in the unsorted range [0, i] to the rightmost end of that range
# Inner loop: move the largest element in the unsorted range [0, i] to its right end through swaps
for j in range(i):
if nums[j] > nums[j + 1]:
# Swap nums[j] and nums[j + 1]
tmp: int = nums[j]
nums[j] = nums[j + 1]
nums[j + 1] = tmp
count += 3 # Element swap includes 3 unit operations
count += 3 # Swapping two elements takes 3 elementary operations
return count
def exponential(n: int) -> int:
"""Exponential order (loop implementation)"""
"""Exponential complexity (iterative implementation)"""
count = 0
base = 1
# Cells divide into two every round, forming sequence 1, 2, 4, 8, ..., 2^(n-1)
# Each cell divides into two per round, giving the sequence 1, 2, 4, 8, ..., 2^(n-1)
for _ in range(n):
for _ in range(base):
count += 1
@@ -71,14 +71,14 @@ def exponential(n: int) -> int:
def exp_recur(n: int) -> int:
"""Exponential order (recursive implementation)"""
"""Exponential complexity (recursive implementation)"""
if n == 1:
return 1
return exp_recur(n - 1) + exp_recur(n - 1) + 1
def logarithmic(n: int) -> int:
"""Logarithmic order (loop implementation)"""
"""Logarithmic complexity (iterative implementation)"""
count = 0
while n > 1:
n = n / 2
@@ -87,67 +87,67 @@ def logarithmic(n: int) -> int:
def log_recur(n: int) -> int:
"""Logarithmic order (recursive implementation)"""
"""Logarithmic complexity (recursive implementation)"""
if n <= 1:
return 0
return log_recur(n / 2) + 1
def linear_log_recur(n: int) -> int:
"""Linearithmic order"""
"""Linearithmic complexity"""
if n <= 1:
return 1
# Divide into two, the scale of subproblems is reduced by half
# Split into two subproblems, each half the original size
count = linear_log_recur(n // 2) + linear_log_recur(n // 2)
# Current subproblem contains n operations
# The current subproblem includes n operations
for _ in range(n):
count += 1
return count
def factorial_recur(n: int) -> int:
"""Factorial order (recursive implementation)"""
"""Factorial complexity (recursive implementation)"""
if n == 0:
return 1
count = 0
# Split from 1 into n
# Split one problem into n subproblems
for _ in range(n):
count += factorial_recur(n - 1)
return count
"""Driver Code"""
"""Driver code"""
if __name__ == "__main__":
# You can modify n to run and observe the trend of the number of operations for various complexities
# Try different values of n to see how the operation count grows for each complexity
n = 8
print("Input data size n =", n)
print("Input size n =", n)
count = constant(n)
print("Number of operations of constant order =", count)
print("Operation count for constant complexity =", count)
count = linear(n)
print("Number of operations of linear order =", count)
print("Operation count for linear complexity =", count)
count = array_traversal([0] * n)
print("Number of operations of linear order (traversing array) =", count)
print("Operation count for linear complexity (array traversal) =", count)
count = quadratic(n)
print("Number of operations of quadratic order =", count)
print("Operation count for quadratic complexity =", count)
nums = [i for i in range(n, 0, -1)] # [n, n-1, ..., 2, 1]
count = bubble_sort(nums)
print("Number of operations of quadratic order (bubble sort) =", count)
print("Operation count for quadratic complexity (bubble sort) =", count)
count = exponential(n)
print("Number of operations of exponential order (loop implementation) =", count)
print("Operation count for exponential complexity (iterative implementation) =", count)
count = exp_recur(n)
print("Number of operations of exponential order (recursive implementation) =", count)
print("Operation count for exponential complexity (recursive implementation) =", count)
count = logarithmic(n)
print("Number of operations of logarithmic order (loop implementation) =", count)
print("Operation count for logarithmic complexity (iterative implementation) =", count)
count = log_recur(n)
print("Number of operations of logarithmic order (recursive implementation) =", count)
print("Operation count for logarithmic complexity (recursive implementation) =", count)
count = linear_log_recur(n)
print("Number of operations of linearithmic order (recursive implementation) =", count)
print("Operation count for linearithmic complexity (recursive implementation) =", count)
count = factorial_recur(n)
print("Number of operations of factorial order (recursive implementation) =", count)
print("Operation count for factorial complexity (recursive implementation) =", count)