mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-13 18:00:18 +08:00
Translate all code to English (#1836)
* Review the EN heading format. * Fix pythontutor headings. * Fix pythontutor headings. * bug fixes * Fix headings in **/summary.md * Revisit the CN-to-EN translation for Python code using Claude-4.5 * Revisit the CN-to-EN translation for Java code using Claude-4.5 * Revisit the CN-to-EN translation for Cpp code using Claude-4.5. * Fix the dictionary. * Fix cpp code translation for the multipart strings. * Translate Go code to English. * Update workflows to test EN code. * Add EN translation for C. * Add EN translation for CSharp. * Add EN translation for Swift. * Trigger the CI check. * Revert. * Update en/hash_map.md * Add the EN version of Dart code. * Add the EN version of Kotlin code. * Add missing code files. * Add the EN version of JavaScript code. * Add the EN version of TypeScript code. * Fix the workflows. * Add the EN version of Ruby code. * Add the EN version of Rust code. * Update the CI check for the English version code. * Update Python CI check. * Fix cmakelists for en/C code. * Fix Ruby comments
This commit is contained in:
@@ -9,7 +9,7 @@
|
||||
/* for loop */
|
||||
int forLoop(int n) {
|
||||
int res = 0;
|
||||
// Loop sum 1, 2, ..., n-1, n
|
||||
// Sum 1, 2, ..., n-1, n
|
||||
for (int i = 1; i <= n; ++i) {
|
||||
res += i;
|
||||
}
|
||||
@@ -20,7 +20,7 @@ int forLoop(int n) {
|
||||
int whileLoop(int n) {
|
||||
int res = 0;
|
||||
int i = 1; // Initialize condition variable
|
||||
// Loop sum 1, 2, ..., n-1, n
|
||||
// Sum 1, 2, ..., n-1, n
|
||||
while (i <= n) {
|
||||
res += i;
|
||||
i++; // Update condition variable
|
||||
@@ -32,7 +32,7 @@ int whileLoop(int n) {
|
||||
int whileLoopII(int n) {
|
||||
int res = 0;
|
||||
int i = 1; // Initialize condition variable
|
||||
// Loop sum 1, 4, 10, ...
|
||||
// Sum 1, 4, 10, ...
|
||||
while (i <= n) {
|
||||
res += i;
|
||||
// Update condition variable
|
||||
@@ -42,7 +42,7 @@ int whileLoopII(int n) {
|
||||
return res;
|
||||
}
|
||||
|
||||
/* Double for loop */
|
||||
/* Nested for loop */
|
||||
string nestedForLoop(int n) {
|
||||
ostringstream res;
|
||||
// Loop i = 1, 2, ..., n-1, n
|
||||
@@ -61,16 +61,16 @@ int main() {
|
||||
int res;
|
||||
|
||||
res = forLoop(n);
|
||||
cout << "\nSum result of the for loop res = " << res << endl;
|
||||
cout << "\nfor loop sum result res = " << res << endl;
|
||||
|
||||
res = whileLoop(n);
|
||||
cout << "\nSum result of the while loop res = " << res << endl;
|
||||
cout << "\nwhile loop sum result res = " << res << endl;
|
||||
|
||||
res = whileLoopII(n);
|
||||
cout << "\nSum result of the while loop (with two updates) res = " << res << endl;
|
||||
cout << "\nwhile loop (two updates) sum result res = " << res << endl;
|
||||
|
||||
string resStr = nestedForLoop(n);
|
||||
cout << "\nResult of the double for loop traversal = " << resStr << endl;
|
||||
cout << "\nDouble for loop traversal result " << resStr << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -11,25 +11,25 @@ int recur(int n) {
|
||||
// Termination condition
|
||||
if (n == 1)
|
||||
return 1;
|
||||
// Recursive: recursive call
|
||||
// Recurse: recursive call
|
||||
int res = recur(n - 1);
|
||||
// Return: return result
|
||||
return n + res;
|
||||
}
|
||||
|
||||
/* Simulate recursion with iteration */
|
||||
/* Simulate recursion using iteration */
|
||||
int forLoopRecur(int n) {
|
||||
// Use an explicit stack to simulate the system call stack
|
||||
stack<int> stack;
|
||||
int res = 0;
|
||||
// Recursive: recursive call
|
||||
// Recurse: recursive call
|
||||
for (int i = n; i > 0; i--) {
|
||||
// Simulate "recursive" by "pushing onto the stack"
|
||||
// Simulate "recurse" with "push"
|
||||
stack.push(i);
|
||||
}
|
||||
// Return: return result
|
||||
while (!stack.empty()) {
|
||||
// Simulate "return" by "popping from the stack"
|
||||
// Simulate "return" with "pop"
|
||||
res += stack.top();
|
||||
stack.pop();
|
||||
}
|
||||
@@ -46,7 +46,7 @@ int tailRecur(int n, int res) {
|
||||
return tailRecur(n - 1, res + n);
|
||||
}
|
||||
|
||||
/* Fibonacci sequence: Recursion */
|
||||
/* Fibonacci sequence: recursion */
|
||||
int fib(int n) {
|
||||
// Termination condition f(1) = 0, f(2) = 1
|
||||
if (n == 1 || n == 2)
|
||||
@@ -63,16 +63,16 @@ int main() {
|
||||
int res;
|
||||
|
||||
res = recur(n);
|
||||
cout << "\nSum result of the recursive function res = " << res << endl;
|
||||
cout << "\nRecursive function sum result res = " << res << endl;
|
||||
|
||||
res = forLoopRecur(n);
|
||||
cout << "\nSum result using iteration to simulate recursion res = " << res << endl;
|
||||
cout << "\nUsing iteration to simulate recursive sum result res = " << res << endl;
|
||||
|
||||
res = tailRecur(n, 0);
|
||||
cout << "\nSum result of the tail-recursive function res = " << res << endl;
|
||||
cout << "\nTail recursive function sum result res = " << res << endl;
|
||||
|
||||
res = fib(n);
|
||||
cout << "The " << n << "th number in the Fibonacci sequence is " << res << endl;
|
||||
cout << "\nThe " << n << "th term of the Fibonacci sequence is " << res << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -12,26 +12,26 @@ int func() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Constant complexity */
|
||||
/* Constant order */
|
||||
void constant(int n) {
|
||||
// Constants, variables, objects occupy O(1) space
|
||||
const int a = 0;
|
||||
int b = 0;
|
||||
vector<int> nums(10000);
|
||||
ListNode node(0);
|
||||
// Variables in a loop occupy O(1) space
|
||||
// Variables in the loop occupy O(1) space
|
||||
for (int i = 0; i < n; i++) {
|
||||
int c = 0;
|
||||
}
|
||||
// Functions in a loop occupy O(1) space
|
||||
// Functions in the loop occupy O(1) space
|
||||
for (int i = 0; i < n; i++) {
|
||||
func();
|
||||
}
|
||||
}
|
||||
|
||||
/* Linear complexity */
|
||||
/* Linear order */
|
||||
void linear(int n) {
|
||||
// Array of length n occupies O(n) space
|
||||
// Array of length n uses O(n) space
|
||||
vector<int> nums(n);
|
||||
// A list of length n occupies O(n) space
|
||||
vector<ListNode> nodes;
|
||||
@@ -45,7 +45,7 @@ void linear(int n) {
|
||||
}
|
||||
}
|
||||
|
||||
/* Linear complexity (recursive implementation) */
|
||||
/* Linear order (recursive implementation) */
|
||||
void linearRecur(int n) {
|
||||
cout << "Recursion n = " << n << endl;
|
||||
if (n == 1)
|
||||
@@ -53,9 +53,9 @@ void linearRecur(int n) {
|
||||
linearRecur(n - 1);
|
||||
}
|
||||
|
||||
/* Quadratic complexity */
|
||||
/* Exponential order */
|
||||
void quadratic(int n) {
|
||||
// A two-dimensional list occupies O(n^2) space
|
||||
// 2D list uses 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 complexity (recursive implementation) */
|
||||
/* Quadratic order (recursive implementation) */
|
||||
int quadraticRecur(int n) {
|
||||
if (n <= 0)
|
||||
return 0;
|
||||
vector<int> nums(n);
|
||||
cout << "Recursive n = " << n << ", length of nums = " << nums.size() << endl;
|
||||
cout << "In recursion n = " << n << ", nums length = " << nums.size() << endl;
|
||||
return quadraticRecur(n - 1);
|
||||
}
|
||||
|
||||
/* Exponential complexity (building a full binary tree) */
|
||||
/* Driver Code */
|
||||
TreeNode *buildTree(int n) {
|
||||
if (n == 0)
|
||||
return nullptr;
|
||||
@@ -88,15 +88,15 @@ TreeNode *buildTree(int n) {
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
int n = 5;
|
||||
// Constant complexity
|
||||
// Constant order
|
||||
constant(n);
|
||||
// Linear complexity
|
||||
// Linear order
|
||||
linear(n);
|
||||
linearRecur(n);
|
||||
// Quadratic complexity
|
||||
// Exponential order
|
||||
quadratic(n);
|
||||
quadraticRecur(n);
|
||||
// Exponential complexity
|
||||
// Exponential order
|
||||
TreeNode *root = buildTree(n);
|
||||
printTree(root);
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* Constant complexity */
|
||||
/* Constant order */
|
||||
int constant(int n) {
|
||||
int count = 0;
|
||||
int size = 100000;
|
||||
@@ -15,7 +15,7 @@ int constant(int n) {
|
||||
return count;
|
||||
}
|
||||
|
||||
/* Linear complexity */
|
||||
/* Linear order */
|
||||
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 complexity (traversing an array) */
|
||||
/* Linear order (traversing array) */
|
||||
int arrayTraversal(vector<int> &nums) {
|
||||
int count = 0;
|
||||
// Loop count is proportional to the length of the array
|
||||
// Number of iterations is proportional to the array length
|
||||
for (int num : nums) {
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
/* Quadratic complexity */
|
||||
/* Exponential order */
|
||||
int quadratic(int n) {
|
||||
int count = 0;
|
||||
// Loop count is squared in relation to the data size n
|
||||
// Number of iterations is quadratically related to the data 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 complexity (bubble sort) */
|
||||
/* Quadratic order (bubble sort) */
|
||||
int bubbleSort(vector<int> &nums) {
|
||||
int count = 0; // Counter
|
||||
// Outer loop: 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 right end of the range
|
||||
// Inner loop: swap the largest element in the unsorted range [0, i] to the rightmost end of that range
|
||||
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 individual operations
|
||||
count += 3; // Element swap includes 3 unit operations
|
||||
}
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
/* Exponential complexity (loop implementation) */
|
||||
/* Exponential order (loop implementation) */
|
||||
int exponential(int n) {
|
||||
int count = 0, base = 1;
|
||||
// Cells split into two every round, forming the sequence 1, 2, 4, 8, ..., 2^(n-1)
|
||||
// Cells divide into two every round, forming 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 complexity (recursive implementation) */
|
||||
/* Exponential order (recursive implementation) */
|
||||
int expRecur(int n) {
|
||||
if (n == 1)
|
||||
return 1;
|
||||
return expRecur(n - 1) + expRecur(n - 1) + 1;
|
||||
}
|
||||
|
||||
/* Logarithmic complexity (loop implementation) */
|
||||
/* Logarithmic order (loop implementation) */
|
||||
int logarithmic(int n) {
|
||||
int count = 0;
|
||||
while (n > 1) {
|
||||
@@ -95,14 +95,14 @@ int logarithmic(int n) {
|
||||
return count;
|
||||
}
|
||||
|
||||
/* Logarithmic complexity (recursive implementation) */
|
||||
/* Logarithmic order (recursive implementation) */
|
||||
int logRecur(int n) {
|
||||
if (n <= 1)
|
||||
return 0;
|
||||
return logRecur(n / 2) + 1;
|
||||
}
|
||||
|
||||
/* Linear logarithmic complexity */
|
||||
/* Linearithmic order */
|
||||
int linearLogRecur(int n) {
|
||||
if (n <= 1)
|
||||
return 1;
|
||||
@@ -113,12 +113,12 @@ int linearLogRecur(int n) {
|
||||
return count;
|
||||
}
|
||||
|
||||
/* Factorial complexity (recursive implementation) */
|
||||
/* Factorial order (recursive implementation) */
|
||||
int factorialRecur(int n) {
|
||||
if (n == 0)
|
||||
return 1;
|
||||
int count = 0;
|
||||
// From 1 split into n
|
||||
// Split from 1 into n
|
||||
for (int i = 0; i < n; i++) {
|
||||
count += factorialRecur(n - 1);
|
||||
}
|
||||
@@ -127,42 +127,42 @@ int factorialRecur(int n) {
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
// Can modify n to experience the trend of operation count changes under various complexities
|
||||
// You can modify n to run and observe the trend of the number of operations for various complexities
|
||||
int n = 8;
|
||||
cout << "Input data size n = " << n << endl;
|
||||
|
||||
int count = constant(n);
|
||||
cout << "Number of constant complexity operations = " << count << endl;
|
||||
cout << "Constant order operation count = " << count << endl;
|
||||
|
||||
count = linear(n);
|
||||
cout << "Number of linear complexity operations = " << count << endl;
|
||||
cout << "Linear order operation count = " << count << endl;
|
||||
vector<int> arr(n);
|
||||
count = arrayTraversal(arr);
|
||||
cout << "Number of linear complexity operations (traversing the array) = " << count << endl;
|
||||
cout << "Linear order (array traversal) operation count = " << count << endl;
|
||||
|
||||
count = quadratic(n);
|
||||
cout << "Number of quadratic order operations = " << count << endl;
|
||||
cout << "Quadratic order operation count = " << 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 << "Number of quadratic order operations (bubble sort) = " << count << endl;
|
||||
cout << "Quadratic order (bubble sort) operation count = " << count << endl;
|
||||
|
||||
count = exponential(n);
|
||||
cout << "Number of exponential complexity operations (implemented by loop) = " << count << endl;
|
||||
cout << "Exponential order (loop implementation) operation count = " << count << endl;
|
||||
count = expRecur(n);
|
||||
cout << "Number of exponential complexity operations (implemented by recursion) = " << count << endl;
|
||||
cout << "Exponential order (recursive implementation) operation count = " << count << endl;
|
||||
|
||||
count = logarithmic(n);
|
||||
cout << "Number of logarithmic complexity operations (implemented by loop) = " << count << endl;
|
||||
cout << "Logarithmic order (loop implementation) operation count = " << count << endl;
|
||||
count = logRecur(n);
|
||||
cout << "Number of logarithmic complexity operations (implemented by recursion) = " << count << endl;
|
||||
cout << "Logarithmic order (recursive implementation) operation count = " << count << endl;
|
||||
|
||||
count = linearLogRecur(n);
|
||||
cout << "Number of linear logarithmic complexity operations (implemented by recursion) = " << count << endl;
|
||||
cout << "Linearithmic order (recursive implementation) operation count = " << count << endl;
|
||||
|
||||
count = factorialRecur(n);
|
||||
cout << "Number of factorial complexity operations (implemented by recursion) = " << count << endl;
|
||||
cout << "Factorial order (recursive implementation) operation count = " << count << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -6,14 +6,14 @@
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* Generate an array with elements {1, 2, ..., n} in a randomly shuffled order */
|
||||
/* Generate an array with elements { 1, 2, ..., n }, order shuffled */
|
||||
vector<int> randomNumbers(int n) {
|
||||
vector<int> nums(n);
|
||||
// Generate array nums = { 1, 2, 3, ..., n }
|
||||
for (int i = 0; i < n; i++) {
|
||||
nums[i] = i + 1;
|
||||
}
|
||||
// Generate a random seed using system time
|
||||
// Use system time to generate random seed
|
||||
unsigned seed = chrono::system_clock::now().time_since_epoch().count();
|
||||
// Randomly shuffle array elements
|
||||
shuffle(nums.begin(), nums.end(), default_random_engine(seed));
|
||||
@@ -23,8 +23,8 @@ vector<int> randomNumbers(int n) {
|
||||
/* Find the index of number 1 in array nums */
|
||||
int findOne(vector<int> &nums) {
|
||||
for (int i = 0; i < nums.size(); i++) {
|
||||
// When element 1 is at the start of the array, achieve best time complexity O(1)
|
||||
// When element 1 is at the end of the array, achieve worst time complexity O(n)
|
||||
// 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
|
||||
if (nums[i] == 1)
|
||||
return i;
|
||||
}
|
||||
@@ -37,9 +37,9 @@ int main() {
|
||||
int n = 100;
|
||||
vector<int> nums = randomNumbers(n);
|
||||
int index = findOne(nums);
|
||||
cout << "\nThe array [ 1, 2, ..., n ] after being shuffled = ";
|
||||
cout << "\nArray [ 1, 2, ..., n ] after shuffling = ";
|
||||
printVector(nums);
|
||||
cout << "The index of number 1 is " << index << endl;
|
||||
cout << "Index of number 1 is " << index << endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user