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

@@ -4,7 +4,7 @@
* Author: RiverTwilight (contact@rene.wang)
*/
/* Constant order */
/* Constant complexity */
function constant(n) {
let count = 0;
const size = 100000;
@@ -12,27 +12,27 @@ function constant(n) {
return count;
}
/* Linear order */
/* Linear complexity */
function linear(n) {
let count = 0;
for (let i = 0; i < n; i++) count++;
return count;
}
/* Linear order (traversing array) */
/* Linear complexity (array traversal) */
function arrayTraversal(nums) {
let count = 0;
// Number of iterations is proportional to the array length
// The number of iterations is proportional to the array length
for (let i = 0; i < nums.length; i++) {
count++;
}
return count;
}
/* Exponential order */
/* Quadratic complexity */
function quadratic(n) {
let 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 (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++) {
count++;
@@ -41,30 +41,30 @@ function quadratic(n) {
return count;
}
/* Quadratic order (bubble sort) */
/* Quadratic complexity (bubble sort) */
function bubbleSort(nums) {
let count = 0; // Counter
// Outer loop: unsorted range is [0, i]
// Outer loop: the unsorted range is [0, i]
for (let 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 (let j = 0; j < i; j++) {
if (nums[j] > nums[j + 1]) {
// Swap nums[j] and nums[j + 1]
let 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) */
function exponential(n) {
let 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 (let i = 0; i < n; i++) {
for (let j = 0; j < base; j++) {
count++;
@@ -75,13 +75,13 @@ function exponential(n) {
return count;
}
/* Exponential order (recursive implementation) */
/* Exponential complexity (recursive implementation) */
function expRecur(n) {
if (n === 1) return 1;
return expRecur(n - 1) + expRecur(n - 1) + 1;
}
/* Logarithmic order (loop implementation) */
/* Logarithmic complexity (iterative implementation) */
function logarithmic(n) {
let count = 0;
while (n > 1) {
@@ -91,13 +91,13 @@ function logarithmic(n) {
return count;
}
/* Logarithmic order (recursive implementation) */
/* Logarithmic complexity (recursive implementation) */
function logRecur(n) {
if (n <= 1) return 0;
return logRecur(n / 2) + 1;
}
/* Linearithmic order */
/* Linearithmic complexity */
function linearLogRecur(n) {
if (n <= 1) return 1;
let count = linearLogRecur(n / 2) + linearLogRecur(n / 2);
@@ -107,49 +107,49 @@ function linearLogRecur(n) {
return count;
}
/* Factorial order (recursive implementation) */
/* Factorial complexity (recursive implementation) */
function factorialRecur(n) {
if (n === 0) return 1;
let count = 0;
// Split from 1 into n
// Split one problem into n subproblems
for (let i = 0; i < n; i++) {
count += factorialRecur(n - 1);
}
return count;
}
/* Driver Code */
// You can modify n to run and observe the trend of the number of operations for various complexities
/* Driver code */
// Try different values of n to see how the operation count grows for each complexity
const n = 8;
console.log('Input data size n = ' + n);
console.log('Input size n = ' + n);
let count = constant(n);
console.log('Constant order operation count = ' + count);
console.log('Operation count for constant complexity = ' + count);
count = linear(n);
console.log('Linear order operation count = ' + count);
console.log('Operation count for linear complexity = ' + count);
count = arrayTraversal(new Array(n));
console.log('Linear order (array traversal) operation count = ' + count);
console.log('Operation count for linear complexity (array traversal) = ' + count);
count = quadratic(n);
console.log('Quadratic order operation count = ' + count);
console.log('Operation count for quadratic complexity = ' + count);
let nums = new Array(n);
for (let i = 0; i < n; i++) nums[i] = n - i; // [n,n-1,...,2,1]
count = bubbleSort(nums);
console.log('Quadratic order (bubble sort) operation count = ' + count);
console.log('Operation count for quadratic complexity (bubble sort) = ' + count);
count = exponential(n);
console.log('Exponential order (loop implementation) operation count = ' + count);
console.log('Operation count for exponential complexity (iterative implementation) = ' + count);
count = expRecur(n);
console.log('Exponential order (recursive implementation) operation count = ' + count);
console.log('Operation count for exponential complexity (recursive implementation) = ' + count);
count = logarithmic(n);
console.log('Logarithmic order (loop implementation) operation count = ' + count);
console.log('Operation count for logarithmic complexity (iterative implementation) = ' + count);
count = logRecur(n);
console.log('Logarithmic order (recursive implementation) operation count = ' + count);
console.log('Operation count for logarithmic complexity (recursive implementation) = ' + count);
count = linearLogRecur(n);
console.log('Linearithmic order (recursive implementation) operation count = ' + count);
console.log('Operation count for linearithmic complexity (recursive implementation) = ' + count);
count = factorialRecur(n);
console.log('Factorial order (recursive implementation) operation count = ' + count);
console.log('Operation count for factorial complexity (recursive implementation) = ' + count);