mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-24 04:33:09 +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:
@@ -6,7 +6,7 @@
|
||||
|
||||
#include <cassert>
|
||||
|
||||
/* Iterative summation */
|
||||
/* Sum using iteration */
|
||||
int sumIter(int n) {
|
||||
int res = 0;
|
||||
for (int i = 1; i <= n; ++i) {
|
||||
@@ -15,7 +15,7 @@ int sumIter(int n) {
|
||||
return res;
|
||||
}
|
||||
|
||||
/* Recursive summation */
|
||||
/* Sum using recursion */
|
||||
int sumRecur(int n) {
|
||||
if (n == 1) {
|
||||
return 1;
|
||||
@@ -23,7 +23,7 @@ int sumRecur(int n) {
|
||||
return n + sumRecur(n - 1);
|
||||
}
|
||||
|
||||
/* Linear loop */
|
||||
/* Linear-time loop */
|
||||
int linearLoop(int n) {
|
||||
int res = 0;
|
||||
for (int i = 0; i < n; ++i) {
|
||||
@@ -32,7 +32,7 @@ int linearLoop(int n) {
|
||||
return res;
|
||||
}
|
||||
|
||||
/* Quadratic loop */
|
||||
/* Quadratic-time loop */
|
||||
int quadraticLoop(int n) {
|
||||
int res = 0;
|
||||
for (int i = 0; i < n; ++i) {
|
||||
@@ -43,7 +43,7 @@ int quadraticLoop(int n) {
|
||||
return res;
|
||||
}
|
||||
|
||||
/* Logarithmic loop */
|
||||
/* Logarithmic-time loop */
|
||||
int logarithmicLoop(int n) {
|
||||
while (n > 1) {
|
||||
n /= 2;
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
/* 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;
|
||||
}
|
||||
@@ -19,11 +19,11 @@ int forLoop(int n) {
|
||||
/* 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++; // Update condition variable
|
||||
i++; // Update the loop control variable
|
||||
}
|
||||
return res;
|
||||
}
|
||||
@@ -31,23 +31,23 @@ int whileLoop(int n) {
|
||||
/* 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++;
|
||||
i *= 2;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/* Nested for loop */
|
||||
/* Nested for loops */
|
||||
string nestedForLoop(int n) {
|
||||
ostringstream res;
|
||||
// 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 << "(" << i << ", " << j << "), ";
|
||||
}
|
||||
@@ -55,22 +55,22 @@ string nestedForLoop(int n) {
|
||||
return res.str();
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* Driver code */
|
||||
int main() {
|
||||
int n = 5;
|
||||
int res;
|
||||
|
||||
res = forLoop(n);
|
||||
cout << "\nfor loop sum result res = " << res << endl;
|
||||
cout << "\nSum from the for loop, res = " << res << endl;
|
||||
|
||||
res = whileLoop(n);
|
||||
cout << "\nwhile loop sum result res = " << res << endl;
|
||||
cout << "\nSum from the while loop, res = " << res << endl;
|
||||
|
||||
res = whileLoopII(n);
|
||||
cout << "\nwhile loop (two updates) sum result res = " << res << endl;
|
||||
cout << "\nSum from the while loop with two updates, res = " << res << endl;
|
||||
|
||||
string resStr = nestedForLoop(n);
|
||||
cout << "\nDouble for loop traversal result " << resStr << endl;
|
||||
cout << "\nTraversal result from the nested for loops: " << resStr << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -8,12 +8,12 @@
|
||||
|
||||
/* 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;
|
||||
}
|
||||
|
||||
@@ -22,14 +22,14 @@ int forLoopRecur(int n) {
|
||||
// Use an explicit stack to simulate the system call stack
|
||||
stack<int> stack;
|
||||
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.empty()) {
|
||||
// Simulate "return" with "pop"
|
||||
// Simulate unwinding by popping from the stack
|
||||
res += stack.top();
|
||||
stack.pop();
|
||||
}
|
||||
@@ -39,40 +39,40 @@ int forLoopRecur(int n) {
|
||||
|
||||
/* 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 */
|
||||
int main() {
|
||||
int n = 5;
|
||||
int res;
|
||||
|
||||
res = recur(n);
|
||||
cout << "\nRecursive function sum result res = " << res << endl;
|
||||
cout << "\nSum from the recursive function, res = " << res << endl;
|
||||
|
||||
res = forLoopRecur(n);
|
||||
cout << "\nUsing iteration to simulate recursive sum result res = " << res << endl;
|
||||
cout << "\nSum from simulating recursion with iteration, res = " << res << endl;
|
||||
|
||||
res = tailRecur(n, 0);
|
||||
cout << "\nTail recursive function sum result res = " << res << endl;
|
||||
cout << "\nSum from the tail-recursive function, res = " << res << endl;
|
||||
|
||||
res = fib(n);
|
||||
cout << "\nThe " << n << "th term of the Fibonacci sequence is " << res << endl;
|
||||
cout << "\nFibonacci term " << n << " is " << res << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -12,9 +12,9 @@ int func() {
|
||||
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
|
||||
const int a = 0;
|
||||
int b = 0;
|
||||
vector<int> nums(10000);
|
||||
@@ -23,39 +23,39 @@ void constant(int n) {
|
||||
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++) {
|
||||
func();
|
||||
}
|
||||
}
|
||||
|
||||
/* 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
|
||||
vector<int> nums(n);
|
||||
// A list of length n occupies O(n) space
|
||||
vector<ListNode> nodes;
|
||||
for (int i = 0; i < n; i++) {
|
||||
nodes.push_back(ListNode(i));
|
||||
}
|
||||
// A hash table of length n occupies O(n) space
|
||||
// A hash table with n entries occupies O(n) space
|
||||
unordered_map<int, string> map;
|
||||
for (int i = 0; i < n; i++) {
|
||||
map[i] = to_string(i);
|
||||
}
|
||||
}
|
||||
|
||||
/* Linear order (recursive implementation) */
|
||||
/* Linear complexity (recursive implementation) */
|
||||
void linearRecur(int n) {
|
||||
cout << "Recursion n = " << n << endl;
|
||||
cout << "Recursion: n = " << n << endl;
|
||||
if (n == 1)
|
||||
return;
|
||||
linearRecur(n - 1);
|
||||
}
|
||||
|
||||
/* Exponential order */
|
||||
/* Quadratic complexity */
|
||||
void quadratic(int n) {
|
||||
// 2D list uses O(n^2) space
|
||||
// The two-dimensional list occupies O(n^2) space
|
||||
vector<vector<int>> numMatrix;
|
||||
for (int i = 0; i < n; i++) {
|
||||
vector<int> tmp;
|
||||
@@ -66,16 +66,16 @@ void quadratic(int n) {
|
||||
}
|
||||
}
|
||||
|
||||
/* Quadratic order (recursive implementation) */
|
||||
/* Quadratic complexity (recursive implementation) */
|
||||
int quadraticRecur(int n) {
|
||||
if (n <= 0)
|
||||
return 0;
|
||||
vector<int> nums(n);
|
||||
cout << "In recursion n = " << n << ", nums length = " << nums.size() << endl;
|
||||
cout << "Recursion: n = " << n << " , length of nums = " << nums.size() << endl;
|
||||
return quadraticRecur(n - 1);
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* Exponential complexity (building a perfect binary tree) */
|
||||
TreeNode *buildTree(int n) {
|
||||
if (n == 0)
|
||||
return nullptr;
|
||||
@@ -85,18 +85,18 @@ TreeNode *buildTree(int n) {
|
||||
return root;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* Driver code */
|
||||
int main() {
|
||||
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);
|
||||
printTree(root);
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* Constant order */
|
||||
/* Constant complexity */
|
||||
int constant(int n) {
|
||||
int count = 0;
|
||||
int size = 100000;
|
||||
@@ -15,7 +15,7 @@ int constant(int n) {
|
||||
return count;
|
||||
}
|
||||
|
||||
/* Linear order */
|
||||
/* Linear complexity */
|
||||
int linear(int n) {
|
||||
int count = 0;
|
||||
for (int i = 0; i < n; i++)
|
||||
@@ -23,20 +23,20 @@ int linear(int n) {
|
||||
return count;
|
||||
}
|
||||
|
||||
/* Linear order (traversing array) */
|
||||
/* Linear complexity (array traversal) */
|
||||
int arrayTraversal(vector<int> &nums) {
|
||||
int count = 0;
|
||||
// Number of iterations is proportional to the array length
|
||||
// The number of iterations is proportional to the array length
|
||||
for (int num : 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++;
|
||||
@@ -45,29 +45,29 @@ int quadratic(int n) {
|
||||
return count;
|
||||
}
|
||||
|
||||
/* Quadratic order (bubble sort) */
|
||||
/* Quadratic complexity (bubble sort) */
|
||||
int bubbleSort(vector<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.size() - 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]
|
||||
int tmp = 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;
|
||||
}
|
||||
|
||||
/* Exponential order (loop implementation) */
|
||||
/* Exponential complexity (iterative implementation) */
|
||||
int exponential(int n) {
|
||||
int 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 (int i = 0; i < n; i++) {
|
||||
for (int j = 0; j < base; j++) {
|
||||
count++;
|
||||
@@ -78,14 +78,14 @@ int exponential(int n) {
|
||||
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) {
|
||||
@@ -95,14 +95,14 @@ int logarithmic(int n) {
|
||||
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;
|
||||
@@ -113,56 +113,56 @@ int linearLogRecur(int n) {
|
||||
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);
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* Driver code */
|
||||
int 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
|
||||
int n = 8;
|
||||
cout << "Input data size n = " << n << endl;
|
||||
cout << "Input size n = " << n << endl;
|
||||
|
||||
int count = constant(n);
|
||||
cout << "Constant order operation count = " << count << endl;
|
||||
cout << "Operation count for constant complexity = " << count << endl;
|
||||
|
||||
count = linear(n);
|
||||
cout << "Linear order operation count = " << count << endl;
|
||||
cout << "Operation count for linear complexity = " << count << endl;
|
||||
vector<int> arr(n);
|
||||
count = arrayTraversal(arr);
|
||||
cout << "Linear order (array traversal) operation count = " << count << endl;
|
||||
cout << "Operation count for linear complexity (array traversal) = " << count << endl;
|
||||
|
||||
count = quadratic(n);
|
||||
cout << "Quadratic order operation count = " << count << endl;
|
||||
cout << "Operation count for quadratic complexity = " << count << endl;
|
||||
vector<int> nums(n);
|
||||
for (int i = 0; i < n; i++)
|
||||
nums[i] = n - i; // [n,n-1,...,2,1]
|
||||
count = bubbleSort(nums);
|
||||
cout << "Quadratic order (bubble sort) operation count = " << count << endl;
|
||||
cout << "Operation count for quadratic complexity (bubble sort) = " << count << endl;
|
||||
|
||||
count = exponential(n);
|
||||
cout << "Exponential order (loop implementation) operation count = " << count << endl;
|
||||
cout << "Operation count for exponential complexity (iterative implementation) = " << count << endl;
|
||||
count = expRecur(n);
|
||||
cout << "Exponential order (recursive implementation) operation count = " << count << endl;
|
||||
cout << "Operation count for exponential complexity (recursive implementation) = " << count << endl;
|
||||
|
||||
count = logarithmic(n);
|
||||
cout << "Logarithmic order (loop implementation) operation count = " << count << endl;
|
||||
cout << "Operation count for logarithmic complexity (iterative implementation) = " << count << endl;
|
||||
count = logRecur(n);
|
||||
cout << "Logarithmic order (recursive implementation) operation count = " << count << endl;
|
||||
cout << "Operation count for logarithmic complexity (recursive implementation) = " << count << endl;
|
||||
|
||||
count = linearLogRecur(n);
|
||||
cout << "Linearithmic order (recursive implementation) operation count = " << count << endl;
|
||||
cout << "Operation count for linearithmic complexity (recursive implementation) = " << count << endl;
|
||||
|
||||
count = factorialRecur(n);
|
||||
cout << "Factorial order (recursive implementation) operation count = " << count << endl;
|
||||
cout << "Operation count for factorial complexity (recursive implementation) = " << count << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -6,32 +6,32 @@
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* Generate an array with elements { 1, 2, ..., n }, order shuffled */
|
||||
/* Generate an array containing { 1, 2, ..., n } in shuffled order */
|
||||
vector<int> randomNumbers(int n) {
|
||||
vector<int> nums(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;
|
||||
}
|
||||
// Use system time to generate random seed
|
||||
// Use the system time to generate a random seed
|
||||
unsigned seed = chrono::system_clock::now().time_since_epoch().count();
|
||||
// Randomly shuffle array elements
|
||||
// Randomly shuffle the array elements
|
||||
shuffle(nums.begin(), nums.end(), default_random_engine(seed));
|
||||
return nums;
|
||||
}
|
||||
|
||||
/* Find the index of number 1 in array nums */
|
||||
/* Find the index of the number 1 in array nums */
|
||||
int findOne(vector<int> &nums) {
|
||||
for (int i = 0; i < nums.size(); 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;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* Driver code */
|
||||
int main() {
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
int n = 100;
|
||||
@@ -39,7 +39,7 @@ int main() {
|
||||
int index = findOne(nums);
|
||||
cout << "\nArray [ 1, 2, ..., n ] after shuffling = ";
|
||||
printVector(nums);
|
||||
cout << "Index of number 1 is " << index << endl;
|
||||
cout << "The index of the number 1 is " << index << endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user