mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-27 23:11:57 +08:00
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:
@@ -7,7 +7,7 @@
|
||||
namespace hello_algo.chapter_computational_complexity;
|
||||
|
||||
public class complexity_exercises {
|
||||
/* Iterative summation */
|
||||
/* Sum using iteration */
|
||||
int SumIter(int n) {
|
||||
int res = 0;
|
||||
for (int i = 1; i <= n; i++) {
|
||||
@@ -16,7 +16,7 @@ public class complexity_exercises {
|
||||
return res;
|
||||
}
|
||||
|
||||
/* Recursive summation */
|
||||
/* Sum using recursion */
|
||||
int SumRecur(int n) {
|
||||
if (n == 1) {
|
||||
return 1;
|
||||
@@ -24,7 +24,7 @@ public class complexity_exercises {
|
||||
return n + SumRecur(n - 1);
|
||||
}
|
||||
|
||||
/* Linear loop */
|
||||
/* Linear-time loop */
|
||||
int LinearLoop(int n) {
|
||||
int res = 0;
|
||||
for (int i = 0; i < n; i++) {
|
||||
@@ -33,7 +33,7 @@ public class complexity_exercises {
|
||||
return res;
|
||||
}
|
||||
|
||||
/* Quadratic loop */
|
||||
/* Quadratic-time loop */
|
||||
int QuadraticLoop(int n) {
|
||||
int res = 0;
|
||||
for (int i = 0; i < n; i++) {
|
||||
@@ -44,7 +44,7 @@ public class complexity_exercises {
|
||||
return res;
|
||||
}
|
||||
|
||||
/* Logarithmic loop */
|
||||
/* Logarithmic-time loop */
|
||||
int LogarithmicLoop(int n) {
|
||||
while (n > 1) {
|
||||
n /= 2;
|
||||
|
||||
@@ -10,7 +10,7 @@ public class iteration {
|
||||
/* for loop */
|
||||
int ForLoop(int n) {
|
||||
int res = 0;
|
||||
// Sum 1, 2, ..., n-1, n
|
||||
// Sum 1, 2, ..., n-1, n in a loop
|
||||
for (int i = 1; i <= n; i++) {
|
||||
res += i;
|
||||
}
|
||||
@@ -20,11 +20,11 @@ public class iteration {
|
||||
/* while loop */
|
||||
int WhileLoop(int n) {
|
||||
int res = 0;
|
||||
int i = 1; // Initialize condition variable
|
||||
// Sum 1, 2, ..., n-1, n
|
||||
int i = 1; // Initialize the loop control variable
|
||||
// Sum 1, 2, ..., n-1, n in a loop
|
||||
while (i <= n) {
|
||||
res += i;
|
||||
i += 1; // Update condition variable
|
||||
i += 1; // Update the loop control variable
|
||||
}
|
||||
return res;
|
||||
}
|
||||
@@ -32,23 +32,23 @@ public class iteration {
|
||||
/* while loop (two updates) */
|
||||
int WhileLoopII(int n) {
|
||||
int res = 0;
|
||||
int i = 1; // Initialize condition variable
|
||||
// Sum 1, 4, 10, ...
|
||||
int i = 1; // Initialize the loop control variable
|
||||
// Sum 1, 4, 10, ... in a loop
|
||||
while (i <= n) {
|
||||
res += i;
|
||||
// Update condition variable
|
||||
// Update the loop control variable
|
||||
i += 1;
|
||||
i *= 2;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/* Nested for loop */
|
||||
/* Nested for loops */
|
||||
string NestedForLoop(int n) {
|
||||
StringBuilder res = new();
|
||||
// Loop i = 1, 2, ..., n-1, n
|
||||
// Loop over i = 1, 2, ..., n-1, n
|
||||
for (int i = 1; i <= n; i++) {
|
||||
// Loop j = 1, 2, ..., n-1, n
|
||||
// Loop over j = 1, 2, ..., n-1, n
|
||||
for (int j = 1; j <= n; j++) {
|
||||
res.Append($"({i}, {j}), ");
|
||||
}
|
||||
@@ -56,22 +56,22 @@ public class iteration {
|
||||
return res.ToString();
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* Driver code */
|
||||
[Test]
|
||||
public void Test() {
|
||||
int n = 5;
|
||||
int res;
|
||||
|
||||
res = ForLoop(n);
|
||||
Console.WriteLine("\nfor loop sum result res = " + res);
|
||||
Console.WriteLine("\nSum from the for loop: res = " + res);
|
||||
|
||||
res = WhileLoop(n);
|
||||
Console.WriteLine("\nwhile loop sum result res = " + res);
|
||||
Console.WriteLine("\nSum from the while loop: res = " + res);
|
||||
|
||||
res = WhileLoopII(n);
|
||||
Console.WriteLine("\nwhile loop (two updates) sum result res = " + res);
|
||||
Console.WriteLine("\nSum from the while loop (two updates): res = " + res);
|
||||
|
||||
string resStr = NestedForLoop(n);
|
||||
Console.WriteLine("\nDouble for loop traversal result " + resStr);
|
||||
Console.WriteLine("\nTraversal result from the nested for loops: " + resStr);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,12 +9,12 @@ namespace hello_algo.chapter_computational_complexity;
|
||||
public class recursion {
|
||||
/* Recursion */
|
||||
int Recur(int n) {
|
||||
// Termination condition
|
||||
// Base case
|
||||
if (n == 1)
|
||||
return 1;
|
||||
// Recurse: recursive call
|
||||
// Recurse: make a recursive call
|
||||
int res = Recur(n - 1);
|
||||
// Return: return result
|
||||
// Unwind: return the result
|
||||
return n + res;
|
||||
}
|
||||
|
||||
@@ -23,14 +23,14 @@ public class recursion {
|
||||
// Use an explicit stack to simulate the system call stack
|
||||
Stack<int> stack = new();
|
||||
int res = 0;
|
||||
// Recurse: recursive call
|
||||
// Recurse: make a recursive call
|
||||
for (int i = n; i > 0; i--) {
|
||||
// Simulate "recurse" with "push"
|
||||
// Simulate recursive calls by pushing onto the stack
|
||||
stack.Push(i);
|
||||
}
|
||||
// Return: return result
|
||||
// Unwind: return the result
|
||||
while (stack.Count > 0) {
|
||||
// Simulate "return" with "pop"
|
||||
// Simulate unwinding by popping from the stack
|
||||
res += stack.Pop();
|
||||
}
|
||||
// res = 1+2+3+...+n
|
||||
@@ -39,40 +39,40 @@ public class recursion {
|
||||
|
||||
/* Tail recursion */
|
||||
int TailRecur(int n, int res) {
|
||||
// Termination condition
|
||||
// Base case
|
||||
if (n == 0)
|
||||
return res;
|
||||
// Tail recursive call
|
||||
// Tail-recursive call
|
||||
return TailRecur(n - 1, res + n);
|
||||
}
|
||||
|
||||
/* Fibonacci sequence: recursion */
|
||||
int Fib(int n) {
|
||||
// Termination condition f(1) = 0, f(2) = 1
|
||||
// Base cases: f(1) = 0, f(2) = 1
|
||||
if (n == 1 || n == 2)
|
||||
return n - 1;
|
||||
// Recursive call f(n) = f(n-1) + f(n-2)
|
||||
// Recursive calls: f(n) = f(n-1) + f(n-2)
|
||||
int res = Fib(n - 1) + Fib(n - 2);
|
||||
// Return result f(n)
|
||||
// Return the result f(n)
|
||||
return res;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* Driver code */
|
||||
[Test]
|
||||
public void Test() {
|
||||
int n = 5;
|
||||
int res;
|
||||
|
||||
res = Recur(n);
|
||||
Console.WriteLine("\nRecursive function sum result res = " + res);
|
||||
Console.WriteLine("\nSum from recursion: res = " + res);
|
||||
|
||||
res = ForLoopRecur(n);
|
||||
Console.WriteLine("\nUsing iteration to simulate recursive sum result res = " + res);
|
||||
Console.WriteLine("\nSum from simulating recursion with iteration: res = " + res);
|
||||
|
||||
res = TailRecur(n, 0);
|
||||
Console.WriteLine("\nTail recursive function sum result res = " + res);
|
||||
Console.WriteLine("\nSum from tail recursion: res = " + res);
|
||||
|
||||
res = Fib(n);
|
||||
Console.WriteLine("\nThe " + n + "th term of the Fibonacci sequence is " + res);
|
||||
Console.WriteLine("\nFibonacci term " + n + " is " + res);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,9 +13,9 @@ public class space_complexity {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Constant order */
|
||||
/* Constant complexity */
|
||||
void Constant(int n) {
|
||||
// Constants, variables, objects occupy O(1) space
|
||||
// Constants, variables, and objects occupy O(1) space
|
||||
int a = 0;
|
||||
int b = 0;
|
||||
int[] nums = new int[10000];
|
||||
@@ -24,40 +24,40 @@ public class space_complexity {
|
||||
for (int i = 0; i < n; i++) {
|
||||
int c = 0;
|
||||
}
|
||||
// Functions in the loop occupy O(1) space
|
||||
// Function calls in the loop occupy O(1) space
|
||||
for (int i = 0; i < n; i++) {
|
||||
Function();
|
||||
}
|
||||
}
|
||||
|
||||
/* Linear order */
|
||||
/* Linear complexity */
|
||||
void Linear(int n) {
|
||||
// Array of length n uses O(n) space
|
||||
// An array of length n uses O(n) space
|
||||
int[] nums = new int[n];
|
||||
// A list of length n occupies O(n) space
|
||||
List<ListNode> nodes = [];
|
||||
for (int i = 0; i < n; i++) {
|
||||
nodes.Add(new ListNode(i));
|
||||
}
|
||||
// A hash table of length n occupies O(n) space
|
||||
// A hash table with n entries occupies O(n) space
|
||||
Dictionary<int, string> map = [];
|
||||
for (int i = 0; i < n; i++) {
|
||||
map.Add(i, i.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
/* Linear order (recursive implementation) */
|
||||
/* Linear complexity (recursive implementation) */
|
||||
void LinearRecur(int n) {
|
||||
Console.WriteLine("Recursion n = " + n);
|
||||
Console.WriteLine("Recursive call: n = " + n);
|
||||
if (n == 1) return;
|
||||
LinearRecur(n - 1);
|
||||
}
|
||||
|
||||
/* Exponential order */
|
||||
/* Quadratic complexity */
|
||||
void Quadratic(int n) {
|
||||
// Matrix uses O(n^2) space
|
||||
// The matrix uses O(n^2) space
|
||||
int[,] numMatrix = new int[n, n];
|
||||
// 2D list uses O(n^2) space
|
||||
// The two-dimensional list occupies O(n^2) space
|
||||
List<List<int>> numList = [];
|
||||
for (int i = 0; i < n; i++) {
|
||||
List<int> tmp = [];
|
||||
@@ -68,15 +68,15 @@ public class space_complexity {
|
||||
}
|
||||
}
|
||||
|
||||
/* Quadratic order (recursive implementation) */
|
||||
/* Quadratic complexity (recursive implementation) */
|
||||
int QuadraticRecur(int n) {
|
||||
if (n <= 0) return 0;
|
||||
int[] nums = new int[n];
|
||||
Console.WriteLine("Recursion n = " + n + ", nums length = " + nums.Length);
|
||||
Console.WriteLine("Recursive call: n = " + n + " with nums length = " + nums.Length);
|
||||
return QuadraticRecur(n - 1);
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* Exponential complexity (building a perfect binary tree) */
|
||||
TreeNode? BuildTree(int n) {
|
||||
if (n == 0) return null;
|
||||
TreeNode root = new(0) {
|
||||
@@ -89,15 +89,15 @@ public class space_complexity {
|
||||
[Test]
|
||||
public void Test() {
|
||||
int n = 5;
|
||||
// Constant order
|
||||
// Constant complexity
|
||||
Constant(n);
|
||||
// Linear order
|
||||
// Linear complexity
|
||||
Linear(n);
|
||||
LinearRecur(n);
|
||||
// Exponential order
|
||||
// Quadratic complexity
|
||||
Quadratic(n);
|
||||
QuadraticRecur(n);
|
||||
// Exponential order
|
||||
// Exponential complexity
|
||||
TreeNode? root = BuildTree(n);
|
||||
PrintUtil.PrintTree(root);
|
||||
}
|
||||
|
||||
@@ -8,13 +8,13 @@ namespace hello_algo.chapter_computational_complexity;
|
||||
|
||||
public class time_complexity {
|
||||
void Algorithm(int n) {
|
||||
int a = 1; // +0 (technique 1)
|
||||
a += n; // +0 (technique 1)
|
||||
// +n (technique 2)
|
||||
int a = 1; // +0 (rule 1)
|
||||
a += n; // +0 (rule 1)
|
||||
// +n (rule 2)
|
||||
for (int i = 0; i < 5 * n + 1; i++) {
|
||||
Console.WriteLine(0);
|
||||
}
|
||||
// +n*n (technique 3)
|
||||
// +n*n (rule 3)
|
||||
for (int i = 0; i < 2 * n; i++) {
|
||||
for (int j = 0; j < n + 1; j++) {
|
||||
Console.WriteLine(0);
|
||||
@@ -41,7 +41,7 @@ public class time_complexity {
|
||||
}
|
||||
}
|
||||
|
||||
/* Constant order */
|
||||
/* Constant complexity */
|
||||
int Constant(int n) {
|
||||
int count = 0;
|
||||
int size = 100000;
|
||||
@@ -50,7 +50,7 @@ public class time_complexity {
|
||||
return count;
|
||||
}
|
||||
|
||||
/* Linear order */
|
||||
/* Linear complexity */
|
||||
int Linear(int n) {
|
||||
int count = 0;
|
||||
for (int i = 0; i < n; i++)
|
||||
@@ -58,20 +58,20 @@ public class time_complexity {
|
||||
return count;
|
||||
}
|
||||
|
||||
/* Linear order (traversing array) */
|
||||
/* Linear complexity (array traversal) */
|
||||
int ArrayTraversal(int[] nums) {
|
||||
int count = 0;
|
||||
// Number of iterations is proportional to the array length
|
||||
// The number of iterations is proportional to the array length
|
||||
foreach (int num in nums) {
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
/* Exponential order */
|
||||
/* Quadratic complexity */
|
||||
int Quadratic(int n) {
|
||||
int 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 (int i = 0; i < n; i++) {
|
||||
for (int j = 0; j < n; j++) {
|
||||
count++;
|
||||
@@ -80,27 +80,27 @@ public class time_complexity {
|
||||
return count;
|
||||
}
|
||||
|
||||
/* Quadratic order (bubble sort) */
|
||||
/* Quadratic complexity (bubble sort) */
|
||||
int BubbleSort(int[] nums) {
|
||||
int count = 0; // Counter
|
||||
// Outer loop: unsorted range is [0, i]
|
||||
// Outer loop: the unsorted range is [0, i]
|
||||
for (int i = nums.Length - 1; i > 0; i--) {
|
||||
// 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 (int j = 0; j < i; j++) {
|
||||
if (nums[j] > nums[j + 1]) {
|
||||
// Swap nums[j] and nums[j + 1]
|
||||
(nums[j + 1], nums[j]) = (nums[j], nums[j + 1]);
|
||||
count += 3; // Element swap includes 3 unit operations
|
||||
count += 3; // Swapping two elements takes 3 elementary operations
|
||||
}
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
/* Exponential order (loop implementation) */
|
||||
/* Exponential complexity (iterative implementation) */
|
||||
int Exponential(int n) {
|
||||
int count = 0, bas = 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 (int i = 0; i < n; i++) {
|
||||
for (int j = 0; j < bas; j++) {
|
||||
count++;
|
||||
@@ -111,13 +111,13 @@ public class time_complexity {
|
||||
return count;
|
||||
}
|
||||
|
||||
/* Exponential order (recursive implementation) */
|
||||
/* Exponential complexity (recursive implementation) */
|
||||
int ExpRecur(int n) {
|
||||
if (n == 1) return 1;
|
||||
return ExpRecur(n - 1) + ExpRecur(n - 1) + 1;
|
||||
}
|
||||
|
||||
/* Logarithmic order (loop implementation) */
|
||||
/* Logarithmic complexity (iterative implementation) */
|
||||
int Logarithmic(int n) {
|
||||
int count = 0;
|
||||
while (n > 1) {
|
||||
@@ -127,13 +127,13 @@ public class time_complexity {
|
||||
return count;
|
||||
}
|
||||
|
||||
/* Logarithmic order (recursive implementation) */
|
||||
/* Logarithmic complexity (recursive implementation) */
|
||||
int LogRecur(int n) {
|
||||
if (n <= 1) return 0;
|
||||
return LogRecur(n / 2) + 1;
|
||||
}
|
||||
|
||||
/* Linearithmic order */
|
||||
/* Linearithmic complexity */
|
||||
int LinearLogRecur(int n) {
|
||||
if (n <= 1) return 1;
|
||||
int count = LinearLogRecur(n / 2) + LinearLogRecur(n / 2);
|
||||
@@ -143,11 +143,11 @@ public class time_complexity {
|
||||
return count;
|
||||
}
|
||||
|
||||
/* Factorial order (recursive implementation) */
|
||||
/* Factorial complexity (recursive implementation) */
|
||||
int FactorialRecur(int n) {
|
||||
if (n == 0) return 1;
|
||||
int count = 0;
|
||||
// Split from 1 into n
|
||||
// Split one problem into n subproblems
|
||||
for (int i = 0; i < n; i++) {
|
||||
count += FactorialRecur(n - 1);
|
||||
}
|
||||
@@ -156,40 +156,40 @@ public class time_complexity {
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
// 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
|
||||
int n = 8;
|
||||
Console.WriteLine("Input data size n = " + n);
|
||||
Console.WriteLine("Input size n = " + n);
|
||||
|
||||
int count = Constant(n);
|
||||
Console.WriteLine("Constant order operation count = " + count);
|
||||
Console.WriteLine("Operation count for constant complexity = " + count);
|
||||
|
||||
count = Linear(n);
|
||||
Console.WriteLine("Linear order operation count = " + count);
|
||||
Console.WriteLine("Operation count for linear complexity = " + count);
|
||||
count = ArrayTraversal(new int[n]);
|
||||
Console.WriteLine("Linear order (array traversal) operation count = " + count);
|
||||
Console.WriteLine("Operation count for linear complexity (array traversal) = " + count);
|
||||
|
||||
count = Quadratic(n);
|
||||
Console.WriteLine("Quadratic order operation count = " + count);
|
||||
Console.WriteLine("Operation count for quadratic complexity = " + count);
|
||||
int[] nums = new int[n];
|
||||
for (int i = 0; i < n; i++)
|
||||
nums[i] = n - i; // [n,n-1,...,2,1]
|
||||
count = BubbleSort(nums);
|
||||
Console.WriteLine("Quadratic order (bubble sort) operation count = " + count);
|
||||
Console.WriteLine("Operation count for quadratic complexity (bubble sort) = " + count);
|
||||
|
||||
count = Exponential(n);
|
||||
Console.WriteLine("Exponential order (loop implementation) operation count = " + count);
|
||||
Console.WriteLine("Operation count for exponential complexity (iterative implementation) = " + count);
|
||||
count = ExpRecur(n);
|
||||
Console.WriteLine("Exponential order (recursive implementation) operation count = " + count);
|
||||
Console.WriteLine("Operation count for exponential complexity (recursive implementation) = " + count);
|
||||
|
||||
count = Logarithmic(n);
|
||||
Console.WriteLine("Logarithmic order (loop implementation) operation count = " + count);
|
||||
Console.WriteLine("Operation count for logarithmic complexity (iterative implementation) = " + count);
|
||||
count = LogRecur(n);
|
||||
Console.WriteLine("Logarithmic order (recursive implementation) operation count = " + count);
|
||||
Console.WriteLine("Operation count for logarithmic complexity (recursive implementation) = " + count);
|
||||
|
||||
count = LinearLogRecur(n);
|
||||
Console.WriteLine("Linearithmic order (recursive implementation) operation count = " + count);
|
||||
Console.WriteLine("Operation count for linearithmic complexity (recursive implementation) = " + count);
|
||||
|
||||
count = FactorialRecur(n);
|
||||
Console.WriteLine("Factorial order (recursive implementation) operation count = " + count);
|
||||
Console.WriteLine("Operation count for factorial complexity (recursive implementation) = " + count);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,15 +7,15 @@
|
||||
namespace hello_algo.chapter_computational_complexity;
|
||||
|
||||
public class worst_best_time_complexity {
|
||||
/* Generate an array with elements { 1, 2, ..., n }, order shuffled */
|
||||
/* Generate an array containing { 1, 2, ..., n } in shuffled order */
|
||||
int[] RandomNumbers(int n) {
|
||||
int[] nums = new int[n];
|
||||
// Generate array nums = { 1, 2, 3, ..., n }
|
||||
// Generate the array nums = { 1, 2, 3, ..., n }
|
||||
for (int i = 0; i < n; i++) {
|
||||
nums[i] = i + 1;
|
||||
}
|
||||
|
||||
// Randomly shuffle array elements
|
||||
// Randomly shuffle the array elements
|
||||
for (int i = 0; i < nums.Length; i++) {
|
||||
int index = new Random().Next(i, nums.Length);
|
||||
(nums[i], nums[index]) = (nums[index], nums[i]);
|
||||
@@ -23,11 +23,11 @@ public class worst_best_time_complexity {
|
||||
return nums;
|
||||
}
|
||||
|
||||
/* Find the index of number 1 in array nums */
|
||||
/* Find the index of the number 1 in array nums */
|
||||
int FindOne(int[] nums) {
|
||||
for (int i = 0; i < nums.Length; i++) {
|
||||
// When element 1 is at the head of the array, best time complexity O(1) is achieved
|
||||
// When element 1 is at the tail of the array, worst time complexity O(n) is achieved
|
||||
// When 1 is at the start of the array, the best-case time complexity is O(1)
|
||||
// When 1 is at the end of the array, the worst-case time complexity is O(n)
|
||||
if (nums[i] == 1)
|
||||
return i;
|
||||
}
|
||||
@@ -35,7 +35,7 @@ public class worst_best_time_complexity {
|
||||
}
|
||||
|
||||
|
||||
/* Driver Code */
|
||||
/* Driver code */
|
||||
[Test]
|
||||
public void Test() {
|
||||
for (int i = 0; i < 10; i++) {
|
||||
@@ -43,7 +43,7 @@ public class worst_best_time_complexity {
|
||||
int[] nums = RandomNumbers(n);
|
||||
int index = FindOne(nums);
|
||||
Console.WriteLine("\nArray [ 1, 2, ..., n ] after shuffling = " + string.Join(",", nums));
|
||||
Console.WriteLine("Index of number 1 is " + index);
|
||||
Console.WriteLine("The index of the number 1 is " + index);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user