mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-10 16:00:36 +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 "../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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user