From c94101a365a7b22f9457c9d18dccd29290adbc1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Wang?= <52880665+RiverTwilight@users.noreply.github.com> Date: Sun, 1 Jan 2023 19:29:45 +0800 Subject: [PATCH 01/31] docs: added Typescript and Javascript examples Not sure whether these formats meet the requirement. If everything is okay I will continue to transcribe more:-) --- .../time_complexity.md | 51 +++++++++++++++++-- 1 file changed, 48 insertions(+), 3 deletions(-) diff --git a/docs/chapter_computational_complexity/time_complexity.md b/docs/chapter_computational_complexity/time_complexity.md index 539c9b5cb..75f2dd31e 100644 --- a/docs/chapter_computational_complexity/time_complexity.md +++ b/docs/chapter_computational_complexity/time_complexity.md @@ -79,13 +79,27 @@ $$ === "JavaScript" ```js title="" - + function algorithm(n) { + var a = 2; // 1 ns + a = a + 1; // 1 ns + a = a * 2; // 10 ns + for(var i = 0; i < n; i++) { // 1 ns ,每轮都要执行 i++ + console.log(0); // 5 ns + } + } ``` === "TypeScript" ```typescript title="" - + function algorithm(n: number): void { + var a: number = 2; // 1 ns + a = a + 1; // 1 ns + a = a * 2; // 10 ns + for(var i = 0; i < n; i++) { // 1 ns ,每轮都要执行 i++ + console.log(0); // 5 ns + } + } ``` === "C" @@ -220,13 +234,44 @@ $$ === "JavaScript" ```js title="" + // 算法 A 时间复杂度:常数阶 + function algorithm_A(n) { + console.log(0); + } + // 算法 B 时间复杂度:线性阶 + function algorithm_B(n) { + for (var i = 0; i < n; i++) { + console.log(0); + } + } + // 算法 C 时间复杂度:常数阶 + function algorithm_C(n) { + for (var i = 0; i < 1000000; i++) { + console.log(0); + } + } ``` === "TypeScript" ```typescript title="" - + // 算法 A 时间复杂度:常数阶 + function algorithm_A(n: number): void { + console.log(0); + } + // 算法 B 时间复杂度:线性阶 + function algorithm_B(n: number): void { + for (var i = 0; i < n; i++) { + console.log(0); + } + } + // 算法 C 时间复杂度:常数阶 + function algorithm_C(n: number): void { + for (var i = 0; i < 1000000; i++) { + console.log(0); + } + } ``` === "C" From 3f00aa39fb69198901728cceec66fcfe69e57850 Mon Sep 17 00:00:00 2001 From: RiverTwilight Date: Mon, 2 Jan 2023 18:47:36 +0800 Subject: [PATCH 02/31] code: added code for time complexity chapter --- .../time_complexity.js | 21 +++++++++++++ .../time_complexity.ts | 22 ++++++++++++++ .../time_complexity.md | 30 +++++++++---------- 3 files changed, 58 insertions(+), 15 deletions(-) create mode 100644 codes/javascript/chapter_computational_complexity/time_complexity.js create mode 100644 codes/typescript/chapter_computational_complexity/time_complexity.ts diff --git a/codes/javascript/chapter_computational_complexity/time_complexity.js b/codes/javascript/chapter_computational_complexity/time_complexity.js new file mode 100644 index 000000000..1ce3ffbf0 --- /dev/null +++ b/codes/javascript/chapter_computational_complexity/time_complexity.js @@ -0,0 +1,21 @@ +/** + * File: time_complexity.js + * Created Time: 2023-01-02 + * Author: RiverTwilight (contact@rene.wang) + */ + +function algorithm_A(n) { + console.log(0); +} +// 算法 B 时间复杂度:线性阶 +function algorithm_B(n) { + for (var i = 0; i < n; i++) { + console.log(0); + } +} +// 算法 C 时间复杂度:常数阶 +function algorithm_C(n) { + for (var i = 0; i < 1000000; i++) { + console.log(0); + } +} diff --git a/codes/typescript/chapter_computational_complexity/time_complexity.ts b/codes/typescript/chapter_computational_complexity/time_complexity.ts new file mode 100644 index 000000000..cc183265b --- /dev/null +++ b/codes/typescript/chapter_computational_complexity/time_complexity.ts @@ -0,0 +1,22 @@ +/** + * File: time_complexity.ts + * Created Time: 2023-01-02 + * Author: RiverTwilight (contact@rene.wang) + */ + +// 算法 A 时间复杂度:常数阶 +function algorithm_A(n: number): void { + console.log(0); +} +// 算法 B 时间复杂度:线性阶 +function algorithm_B(n: number): void { + for (var i = 0; i < n; i++) { + console.log(0); + } +} +// 算法 C 时间复杂度:常数阶 +function algorithm_C(n: number): void { + for (var i = 0; i < 1000000; i++) { + console.log(0); + } +} diff --git a/docs/chapter_computational_complexity/time_complexity.md b/docs/chapter_computational_complexity/time_complexity.md index 75f2dd31e..dd82b6a2a 100644 --- a/docs/chapter_computational_complexity/time_complexity.md +++ b/docs/chapter_computational_complexity/time_complexity.md @@ -148,9 +148,9 @@ $$ “时间增长趋势”这个概念比较抽象,我们借助一个例子来理解。设输入数据大小为 $n$ ,给定三个算法 `A` , `B` , `C` 。 -- 算法 `A` 只有 $1$ 个打印操作,算法运行时间不随着 $n$ 增大而增长。我们称此算法的时间复杂度为「常数阶」。 -- 算法 `B` 中的打印操作需要循环 $n$ 次,算法运行时间随着 $n$ 增大成线性增长。此算法的时间复杂度被称为「线性阶」。 -- 算法 `C` 中的打印操作需要循环 $1000000$ 次,但运行时间仍与输入数据大小 $n$ 无关。因此 `C` 的时间复杂度和 `A` 相同,仍为「常数阶」。 +- 算法 `A` 只有 $1$ 个打印操作,算法运行时间不随着 $n$ 增大而增长。我们称此算法的时间复杂度为「常数阶」。 +- 算法 `B` 中的打印操作需要循环 $n$ 次,算法运行时间随着 $n$ 增大成线性增长。此算法的时间复杂度被称为「线性阶」。 +- 算法 `C` 中的打印操作需要循环 $1000000$ 次,但运行时间仍与输入数据大小 $n$ 无关。因此 `C` 的时间复杂度和 `A` 相同,仍为「常数阶」。 === "Java" @@ -233,7 +233,7 @@ $$ === "JavaScript" - ```js title="" + ```js title="time_complexity.js" // 算法 A 时间复杂度:常数阶 function algorithm_A(n) { console.log(0); @@ -255,7 +255,7 @@ $$ === "TypeScript" - ```typescript title="" + ```typescript title="time_complexity.ts" // 算法 A 时间复杂度:常数阶 function algorithm_A(n: number): void { console.log(0); @@ -343,7 +343,7 @@ $$ ## 函数渐近上界 -设算法「计算操作数量」为 $T(n)$ ,其是一个关于输入数据大小 $n$ 的函数。例如,以下算法的操作数量为 +设算法「计算操作数量」为 $T(n)$ ,其是一个关于输入数据大小 $n$ 的函数。例如,以下算法的操作数量为 $$ T(n) = 3 + 2n @@ -399,7 +399,7 @@ $$ // 循环 n 次 for i := 0; i < n; i++ { // +1 fmt.Println(a) // +1 - } + } } ``` @@ -640,7 +640,7 @@ $$
-| 操作数量 $T(n)$ | 时间复杂度 $O(f(n))$ | +| 操作数量 $T(n)$ | 时间复杂度 $O(f(n))$ | | ---------------------- | -------------------- | | $100000$ | $O(1)$ | | $3n + 2$ | $O(n)$ | @@ -1696,7 +1696,7 @@ $$ /* 线性对数阶 */ int linearLogRecur(float n) { if (n <= 1) return 1; - int count = linearLogRecur(n / 2) + + int count = linearLogRecur(n / 2) + linearLogRecur(n / 2); for (int i = 0; i < n; i++) { count++; @@ -1711,7 +1711,7 @@ $$ /* 线性对数阶 */ int linearLogRecur(float n) { if (n <= 1) return 1; - int count = linearLogRecur(n / 2) + + int count = linearLogRecur(n / 2) + linearLogRecur(n / 2); for (int i = 0; i < n; i++) { count++; @@ -1935,8 +1935,8 @@ $$ **某些算法的时间复杂度不是恒定的,而是与输入数据的分布有关。** 举一个例子,输入一个长度为 $n$ 数组 `nums` ,其中 `nums` 由从 $1$ 至 $n$ 的数字组成,但元素顺序是随机打乱的;算法的任务是返回元素 $1$ 的索引。我们可以得出以下结论: -- 当 `nums = [?, ?, ..., 1]`,即当末尾元素是 $1$ 时,则需完整遍历数组,此时达到 **最差时间复杂度 $O(n)$** ; -- 当 `nums = [1, ?, ?, ...]` ,即当首个数字为 $1$ 时,无论数组多长都不需要继续遍历,此时达到 **最佳时间复杂度 $\Omega(1)$** ; +- 当 `nums = [?, ?, ..., 1]`,即当末尾元素是 $1$ 时,则需完整遍历数组,此时达到 **最差时间复杂度 $O(n)$** ; +- 当 `nums = [1, ?, ?, ...]` ,即当首个数字为 $1$ 时,无论数组多长都不需要继续遍历,此时达到 **最佳时间复杂度 $\Omega(1)$** ; 「函数渐近上界」使用大 $O$ 记号表示,代表「最差时间复杂度」。与之对应,「函数渐近下界」用 $\Omega$ 记号(Omega Notation)来表示,代表「最佳时间复杂度」。 @@ -1960,7 +1960,7 @@ $$ } return res; } - + /* 查找数组 nums 中数字 1 所在索引 */ int findOne(int[] nums) { for (int i = 0; i < nums.length; i++) { @@ -1969,7 +1969,7 @@ $$ } return -1; } - + /* Driver Code */ public void main(String[] args) { for (int i = 0; i < 10; i++) { @@ -2029,7 +2029,7 @@ $$ ```python title="worst_best_time_complexity.py" """ 生成一个数组,元素为: 1, 2, ..., n ,顺序被打乱 """ def random_numbers(n): - # 生成数组 nums =: 1, 2, 3, ..., n + # 生成数组 nums =: 1, 2, 3, ..., n nums = [i for i in range(1, n + 1)] # 随机打乱数组元素 random.shuffle(nums) From 2bd24e61a8ddb17da40b46341b3aa2ae2b52f799 Mon Sep 17 00:00:00 2001 From: RiverTwilight Date: Mon, 2 Jan 2023 18:57:26 +0800 Subject: [PATCH 03/31] code: added doc code --- .../time_complexity.js | 16 ------ .../time_complexity.ts | 17 ------ .../time_complexity.md | 52 ++++++++++++++++++- 3 files changed, 50 insertions(+), 35 deletions(-) diff --git a/codes/javascript/chapter_computational_complexity/time_complexity.js b/codes/javascript/chapter_computational_complexity/time_complexity.js index 1ce3ffbf0..9c877602f 100644 --- a/codes/javascript/chapter_computational_complexity/time_complexity.js +++ b/codes/javascript/chapter_computational_complexity/time_complexity.js @@ -3,19 +3,3 @@ * Created Time: 2023-01-02 * Author: RiverTwilight (contact@rene.wang) */ - -function algorithm_A(n) { - console.log(0); -} -// 算法 B 时间复杂度:线性阶 -function algorithm_B(n) { - for (var i = 0; i < n; i++) { - console.log(0); - } -} -// 算法 C 时间复杂度:常数阶 -function algorithm_C(n) { - for (var i = 0; i < 1000000; i++) { - console.log(0); - } -} diff --git a/codes/typescript/chapter_computational_complexity/time_complexity.ts b/codes/typescript/chapter_computational_complexity/time_complexity.ts index cc183265b..6f0c9737c 100644 --- a/codes/typescript/chapter_computational_complexity/time_complexity.ts +++ b/codes/typescript/chapter_computational_complexity/time_complexity.ts @@ -3,20 +3,3 @@ * Created Time: 2023-01-02 * Author: RiverTwilight (contact@rene.wang) */ - -// 算法 A 时间复杂度:常数阶 -function algorithm_A(n: number): void { - console.log(0); -} -// 算法 B 时间复杂度:线性阶 -function algorithm_B(n: number): void { - for (var i = 0; i < n; i++) { - console.log(0); - } -} -// 算法 C 时间复杂度:常数阶 -function algorithm_C(n: number): void { - for (var i = 0; i < 1000000; i++) { - console.log(0); - } -} diff --git a/docs/chapter_computational_complexity/time_complexity.md b/docs/chapter_computational_complexity/time_complexity.md index dd82b6a2a..8c2edb08a 100644 --- a/docs/chapter_computational_complexity/time_complexity.md +++ b/docs/chapter_computational_complexity/time_complexity.md @@ -233,7 +233,7 @@ $$ === "JavaScript" - ```js title="time_complexity.js" + ```js title="" // 算法 A 时间复杂度:常数阶 function algorithm_A(n) { console.log(0); @@ -255,7 +255,7 @@ $$ === "TypeScript" - ```typescript title="time_complexity.ts" + ```typescript title="" // 算法 A 时间复杂度:常数阶 function algorithm_A(n: number): void { console.log(0); @@ -406,12 +406,32 @@ $$ === "JavaScript" ```js title="" + function algorithm(n){ + var a = 1; // +1 + a += 1; // +1 + a *= 2; // +1 + // 循环 n 次 + for(var i = 0; i < n; i++){ // +1 + console.log(0) // +1 + } + + } ``` === "TypeScript" ```typescript title="" + function algorithm(n: number): void{ + var a: number = 1; // +1 + a += 1; // +1 + a *= 2; // +1 + // 循环 n 次 + for(var i = 0; i < n; i++){ // +1 + console.log(0) // +1 + } + + } ``` @@ -575,12 +595,40 @@ $$ === "JavaScript" ```js title="" + function algorithm(n) { + var a = 1; // +0(技巧 1) + a = a + n; // +0(技巧 1) + // +n(技巧 2) + for (var i = 0; i < 5 * n + 1; i++) { + console.log(0); + } + // +n*n(技巧 3) + for (var i = 0; i < 2 * n; i++) { + for (var j = 0; j < n + 1; j++) { + console.log(0); + } + } + } ``` === "TypeScript" ```typescript title="" + function algorithm(n: number): void { + var a: number = 1; // +0(技巧 1) + a = a + n; // +0(技巧 1) + // +n(技巧 2) + for (var i = 0; i < 5 * n + 1; i++) { + console.log(0); + } + // +n*n(技巧 3) + for (var i = 0; i < 2 * n; i++) { + for (var j = 0; j < n + 1; j++) { + console.log(0); + } + } + } ``` From 8c736252abd973e881091facff78872eef85bbb3 Mon Sep 17 00:00:00 2001 From: RiverTwilight Date: Mon, 2 Jan 2023 19:10:48 +0800 Subject: [PATCH 04/31] code: transcribe time_complexity.js --- .../time_complexity.js | 153 ++++++++++++++++++ .../time_complexity.ts | 2 +- 2 files changed, 154 insertions(+), 1 deletion(-) diff --git a/codes/javascript/chapter_computational_complexity/time_complexity.js b/codes/javascript/chapter_computational_complexity/time_complexity.js index 9c877602f..803b15c85 100644 --- a/codes/javascript/chapter_computational_complexity/time_complexity.js +++ b/codes/javascript/chapter_computational_complexity/time_complexity.js @@ -3,3 +3,156 @@ * Created Time: 2023-01-02 * Author: RiverTwilight (contact@rene.wang) */ + +class time_complexity { + /* 常数阶 */ + constant(n) { + var count = 0; + const size = 100000; + for (var i = 0; i < size; i++) count++; + return count; + } + + /* 线性阶 */ + linear(n) { + var count = 0; + for (var i = 0; i < n; i++) count++; + return count; + } + + /* 线性阶(遍历数组) */ + arrayTraversal(nums) { + var count = 0; + // 循环次数与数组长度成正比 + nums.forEach(() => { + count++; + }); + } + + /* 平方阶 */ + quadratic(n) { + var count = 0; + // 循环次数与数组长度成平方关系 + for (var i = 0; i < n; i++) { + for (let j = 0; j < n; j++) { + count++; + } + } + return count; + } + + /* 平方阶(冒泡排序) */ + bubbleSort(nums) { + var count = 0; // 计数器 + // 外循环:待排序元素数量为 n-1, n-2, ..., 1 + for (var i = nums.length - 1; i > 0; i--) { + // 内循环:冒泡操作 + for (let j = 0; j < i; j++) { + if (nums[j] > nums[j + 1]) { + // 交换 nums[j] 与 nums[j + 1] + let tmp = nums[j]; + nums[j] = nums[j + 1]; + nums[j + 1] = tmp; + count += 3; // 元素交换包含 3 个单元操作 + } + } + } + return count; + } + + /* 指数阶(循环实现) */ + exponential(n) { + var count = 0, + base = 1; + // cell 每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1) + for (var i = 0; i < n; i++) { + for (let j = 0; j < base; j++) { + count++; + } + base *= 2; + } + // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1 + return count; + } + + /* 指数阶(递归实现) */ + expRecur(n) { + if (n == 1) return 1; + return this.expRecur(n - 1) + this.expRecur(n - 1) + 1; + } + + /* 对数阶(循环实现) */ + logarithmic(n) { + var count = 0; + while (n > 1) { + n = n / 2; + count++; + } + return count; + } + + /* 对数阶(递归实现) */ + logRecur(n) { + if (n <= 1) return 0; + return this.logRecur(n / 2) + 1; + } + + /* 线性对数阶 */ + linearLogRecur(n) { + if (n <= 1) return 1; + var count = this.linearLogRecur(n / 2) + this.linearLogRecur(n / 2); + for (var i = 0; i < n; i++) { + count++; + } + return count; + } + + /* 阶乘阶(递归实现) */ + factorialRecur(n) { + if (n == 0) return 1; + var count = 0; + // 从 1 个分裂出 n 个 + for (var i = 0; i < n; i++) { + count += this.factorialRecur(n - 1); + } + return count; + } +} + +(function main() { + var test = new time_complexity(); + + var n = 8; + console.log("输入数据大小 n = " + n); + + var count = test.constant(n); + console.log("常数阶的计算操作数量 = " + count); + + count = test.linear(n); + console.log("线性阶的计算操作数量 = " + count); + count = test.arrayTraversal(new Array(n)); + console.log("线性阶(遍历数组)的计算操作数量 = " + count); + + count = test.quadratic(n); + console.log("平方阶的计算操作数量 = " + count); + var nums = new Array(n); + for (var i = 0; i < n; i++) nums[i] = n - i; // [n,n-1,...,2,1] + count = test.bubbleSort(nums); + console.log("平方阶(冒泡排序)的计算操作数量 = " + count); + + count = test.exponential(n); + console.log("指数阶(循环实现)的计算操作数量 = " + count); + count = test.expRecur(n); + console.log("指数阶(递归实现)的计算操作数量 = " + count); + + count = test.logarithmic(n); + console.log("对数阶(循环实现)的计算操作数量 = " + count); + count = test.logRecur(n); + console.log("对数阶(递归实现)的计算操作数量 = " + count); + + count = test.linearLogRecur(n); + console.log("线性对数阶(递归实现)的计算操作数量 = " + count); + + count = test.factorialRecur(n); + console.log("阶乘阶(递归实现)的计算操作数量 = " + count); +})(); diff --git a/codes/typescript/chapter_computational_complexity/time_complexity.ts b/codes/typescript/chapter_computational_complexity/time_complexity.ts index 6f0c9737c..9c877602f 100644 --- a/codes/typescript/chapter_computational_complexity/time_complexity.ts +++ b/codes/typescript/chapter_computational_complexity/time_complexity.ts @@ -1,5 +1,5 @@ /** - * File: time_complexity.ts + * File: time_complexity.js * Created Time: 2023-01-02 * Author: RiverTwilight (contact@rene.wang) */ From f83dcce3467f31823b471e13c7a93b0125efb814 Mon Sep 17 00:00:00 2001 From: RiverTwilight Date: Mon, 2 Jan 2023 19:13:36 +0800 Subject: [PATCH 05/31] fix: doesnt return anything --- .../chapter_computational_complexity/time_complexity.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/codes/javascript/chapter_computational_complexity/time_complexity.js b/codes/javascript/chapter_computational_complexity/time_complexity.js index 803b15c85..9fafb9656 100644 --- a/codes/javascript/chapter_computational_complexity/time_complexity.js +++ b/codes/javascript/chapter_computational_complexity/time_complexity.js @@ -24,9 +24,10 @@ class time_complexity { arrayTraversal(nums) { var count = 0; // 循环次数与数组长度成正比 - nums.forEach(() => { + for (var i = 0; i < nums.length; i++) { count++; - }); + } + return count; } /* 平方阶 */ From 6e2412f89704665a9e365f9222cf07ee8db422f1 Mon Sep 17 00:00:00 2001 From: RiverTwilight Date: Mon, 2 Jan 2023 20:40:01 +0800 Subject: [PATCH 06/31] lint: code lint --- docs/chapter_computational_complexity/time_complexity.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/chapter_computational_complexity/time_complexity.md b/docs/chapter_computational_complexity/time_complexity.md index 8c2edb08a..2ba703457 100644 --- a/docs/chapter_computational_complexity/time_complexity.md +++ b/docs/chapter_computational_complexity/time_complexity.md @@ -412,7 +412,7 @@ $$ a *= 2; // +1 // 循环 n 次 for(var i = 0; i < n; i++){ // +1 - console.log(0) // +1 + console.log(0); // +1 } } @@ -428,7 +428,7 @@ $$ a *= 2; // +1 // 循环 n 次 for(var i = 0; i < n; i++){ // +1 - console.log(0) // +1 + console.log(0); // +1 } } From db2a91bd93f3803ae34f218736414876e36a27f8 Mon Sep 17 00:00:00 2001 From: RiverTwilight Date: Mon, 2 Jan 2023 20:49:35 +0800 Subject: [PATCH 07/31] lint: remove class and main --- .../time_complexity.js | 280 +++++++++--------- 1 file changed, 137 insertions(+), 143 deletions(-) diff --git a/codes/javascript/chapter_computational_complexity/time_complexity.js b/codes/javascript/chapter_computational_complexity/time_complexity.js index 9fafb9656..b1a9eb7c5 100644 --- a/codes/javascript/chapter_computational_complexity/time_complexity.js +++ b/codes/javascript/chapter_computational_complexity/time_complexity.js @@ -4,156 +4,150 @@ * Author: RiverTwilight (contact@rene.wang) */ -class time_complexity { - /* 常数阶 */ - constant(n) { - var count = 0; - const size = 100000; - for (var i = 0; i < size; i++) count++; - return count; - } - - /* 线性阶 */ - linear(n) { - var count = 0; - for (var i = 0; i < n; i++) count++; - return count; - } - - /* 线性阶(遍历数组) */ - arrayTraversal(nums) { - var count = 0; - // 循环次数与数组长度成正比 - for (var i = 0; i < nums.length; i++) { - count++; - } - return count; - } - - /* 平方阶 */ - quadratic(n) { - var count = 0; - // 循环次数与数组长度成平方关系 - for (var i = 0; i < n; i++) { - for (let j = 0; j < n; j++) { - count++; - } - } - return count; - } - - /* 平方阶(冒泡排序) */ - bubbleSort(nums) { - var count = 0; // 计数器 - // 外循环:待排序元素数量为 n-1, n-2, ..., 1 - for (var i = nums.length - 1; i > 0; i--) { - // 内循环:冒泡操作 - for (let j = 0; j < i; j++) { - if (nums[j] > nums[j + 1]) { - // 交换 nums[j] 与 nums[j + 1] - let tmp = nums[j]; - nums[j] = nums[j + 1]; - nums[j + 1] = tmp; - count += 3; // 元素交换包含 3 个单元操作 - } - } - } - return count; - } - - /* 指数阶(循环实现) */ - exponential(n) { - var count = 0, - base = 1; - // cell 每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1) - for (var i = 0; i < n; i++) { - for (let j = 0; j < base; j++) { - count++; - } - base *= 2; - } - // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1 - return count; - } - - /* 指数阶(递归实现) */ - expRecur(n) { - if (n == 1) return 1; - return this.expRecur(n - 1) + this.expRecur(n - 1) + 1; - } - - /* 对数阶(循环实现) */ - logarithmic(n) { - var count = 0; - while (n > 1) { - n = n / 2; - count++; - } - return count; - } - - /* 对数阶(递归实现) */ - logRecur(n) { - if (n <= 1) return 0; - return this.logRecur(n / 2) + 1; - } - - /* 线性对数阶 */ - linearLogRecur(n) { - if (n <= 1) return 1; - var count = this.linearLogRecur(n / 2) + this.linearLogRecur(n / 2); - for (var i = 0; i < n; i++) { - count++; - } - return count; - } - - /* 阶乘阶(递归实现) */ - factorialRecur(n) { - if (n == 0) return 1; - var count = 0; - // 从 1 个分裂出 n 个 - for (var i = 0; i < n; i++) { - count += this.factorialRecur(n - 1); - } - return count; - } +/* 常数阶 */ +function constant(n) { + var count = 0; + const size = 100000; + for (var i = 0; i < size; i++) count++; + return count; } -(function main() { - var test = new time_complexity(); +/* 线性阶 */ +function linear(n) { + var count = 0; + for (var i = 0; i < n; i++) count++; + return count; +} - var n = 8; - console.log("输入数据大小 n = " + n); +/* 线性阶(遍历数组) */ +function arrayTraversal(nums) { + var count = 0; + // 循环次数与数组长度成正比 + for (var i = 0; i < nums.length; i++) { + count++; + } + return count; +} - var count = test.constant(n); - console.log("常数阶的计算操作数量 = " + count); +/* 平方阶 */ +function quadratic(n) { + var count = 0; + // 循环次数与数组长度成平方关系 + for (var i = 0; i < n; i++) { + for (let j = 0; j < n; j++) { + count++; + } + } + return count; +} - count = test.linear(n); - console.log("线性阶的计算操作数量 = " + count); - count = test.arrayTraversal(new Array(n)); - console.log("线性阶(遍历数组)的计算操作数量 = " + count); +/* 平方阶(冒泡排序) */ +function bubbleSort(nums) { + var count = 0; // 计数器 + // 外循环:待排序元素数量为 n-1, n-2, ..., 1 + for (var i = nums.length - 1; i > 0; i--) { + // 内循环:冒泡操作 + for (let j = 0; j < i; j++) { + if (nums[j] > nums[j + 1]) { + // 交换 nums[j] 与 nums[j + 1] + let tmp = nums[j]; + nums[j] = nums[j + 1]; + nums[j + 1] = tmp; + count += 3; // 元素交换包含 3 个单元操作 + } + } + } + return count; +} - count = test.quadratic(n); - console.log("平方阶的计算操作数量 = " + count); - var nums = new Array(n); - for (var i = 0; i < n; i++) nums[i] = n - i; // [n,n-1,...,2,1] - count = test.bubbleSort(nums); - console.log("平方阶(冒泡排序)的计算操作数量 = " + count); +/* 指数阶(循环实现) */ +function exponential(n) { + var count = 0, + base = 1; + // cell 每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1) + for (var i = 0; i < n; i++) { + for (let j = 0; j < base; j++) { + count++; + } + base *= 2; + } + // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1 + return count; +} - count = test.exponential(n); - console.log("指数阶(循环实现)的计算操作数量 = " + count); - count = test.expRecur(n); - console.log("指数阶(递归实现)的计算操作数量 = " + count); +/* 指数阶(递归实现) */ +function expRecur(n) { + if (n == 1) return 1; + return expRecur(n - 1) + expRecur(n - 1) + 1; +} - count = test.logarithmic(n); - console.log("对数阶(循环实现)的计算操作数量 = " + count); - count = test.logRecur(n); - console.log("对数阶(递归实现)的计算操作数量 = " + count); +/* 对数阶(循环实现) */ +function logarithmic(n) { + var count = 0; + while (n > 1) { + n = n / 2; + count++; + } + return count; +} - count = test.linearLogRecur(n); - console.log("线性对数阶(递归实现)的计算操作数量 = " + count); +/* 对数阶(递归实现) */ +function logRecur(n) { + if (n <= 1) return 0; + return logRecur(n / 2) + 1; +} - count = test.factorialRecur(n); - console.log("阶乘阶(递归实现)的计算操作数量 = " + count); -})(); +/* 线性对数阶 */ +function linearLogRecur(n) { + if (n <= 1) return 1; + var count = linearLogRecur(n / 2) + linearLogRecur(n / 2); + for (var i = 0; i < n; i++) { + count++; + } + return count; +} + +/* 阶乘阶(递归实现) */ +function factorialRecur(n) { + if (n == 0) return 1; + var count = 0; + // 从 1 个分裂出 n 个 + for (var i = 0; i < n; i++) { + count += factorialRecur(n - 1); + } + return count; +} + +var n = 8; +console.log("输入数据大小 n = " + n); + +var count = constant(n); +console.log("常数阶的计算操作数量 = " + count); + +count = linear(n); +console.log("线性阶的计算操作数量 = " + count); +count = arrayTraversal(new Array(n)); +console.log("线性阶(遍历数组)的计算操作数量 = " + count); + +count = quadratic(n); +console.log("平方阶的计算操作数量 = " + count); +var nums = new Array(n); +for (var i = 0; i < n; i++) nums[i] = n - i; // [n,n-1,...,2,1] +count = bubbleSort(nums); +console.log("平方阶(冒泡排序)的计算操作数量 = " + count); + +count = exponential(n); +console.log("指数阶(循环实现)的计算操作数量 = " + count); +count = expRecur(n); +console.log("指数阶(递归实现)的计算操作数量 = " + count); + +count = logarithmic(n); +console.log("对数阶(循环实现)的计算操作数量 = " + count); +count = logRecur(n); +console.log("对数阶(递归实现)的计算操作数量 = " + count); + +count = linearLogRecur(n); +console.log("线性对数阶(递归实现)的计算操作数量 = " + count); + +count = factorialRecur(n); +console.log("阶乘阶(递归实现)的计算操作数量 = " + count); From d3e15a88563713444b0a6ef3a5d745bd30165396 Mon Sep 17 00:00:00 2001 From: RiverTwilight Date: Mon, 2 Jan 2023 20:52:15 +0800 Subject: [PATCH 08/31] lint: var to let --- .../time_complexity.js | 42 ++--- .../time_complexity.ts | 154 ++++++++++++++++++ 2 files changed, 175 insertions(+), 21 deletions(-) diff --git a/codes/javascript/chapter_computational_complexity/time_complexity.js b/codes/javascript/chapter_computational_complexity/time_complexity.js index b1a9eb7c5..d044f22f0 100644 --- a/codes/javascript/chapter_computational_complexity/time_complexity.js +++ b/codes/javascript/chapter_computational_complexity/time_complexity.js @@ -6,24 +6,24 @@ /* 常数阶 */ function constant(n) { - var count = 0; + let count = 0; const size = 100000; - for (var i = 0; i < size; i++) count++; + for (let i = 0; i < size; i++) count++; return count; } /* 线性阶 */ function linear(n) { - var count = 0; - for (var i = 0; i < n; i++) count++; + let count = 0; + for (let i = 0; i < n; i++) count++; return count; } /* 线性阶(遍历数组) */ function arrayTraversal(nums) { - var count = 0; + let count = 0; // 循环次数与数组长度成正比 - for (var i = 0; i < nums.length; i++) { + for (let i = 0; i < nums.length; i++) { count++; } return count; @@ -31,9 +31,9 @@ function arrayTraversal(nums) { /* 平方阶 */ function quadratic(n) { - var count = 0; + let count = 0; // 循环次数与数组长度成平方关系 - for (var i = 0; i < n; i++) { + for (let i = 0; i < n; i++) { for (let j = 0; j < n; j++) { count++; } @@ -43,9 +43,9 @@ function quadratic(n) { /* 平方阶(冒泡排序) */ function bubbleSort(nums) { - var count = 0; // 计数器 + let count = 0; // 计数器 // 外循环:待排序元素数量为 n-1, n-2, ..., 1 - for (var i = nums.length - 1; i > 0; i--) { + for (let i = nums.length - 1; i > 0; i--) { // 内循环:冒泡操作 for (let j = 0; j < i; j++) { if (nums[j] > nums[j + 1]) { @@ -62,10 +62,10 @@ function bubbleSort(nums) { /* 指数阶(循环实现) */ function exponential(n) { - var count = 0, + let count = 0, base = 1; // cell 每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1) - for (var i = 0; i < n; i++) { + for (let i = 0; i < n; i++) { for (let j = 0; j < base; j++) { count++; } @@ -83,7 +83,7 @@ function expRecur(n) { /* 对数阶(循环实现) */ function logarithmic(n) { - var count = 0; + let count = 0; while (n > 1) { n = n / 2; count++; @@ -100,8 +100,8 @@ function logRecur(n) { /* 线性对数阶 */ function linearLogRecur(n) { if (n <= 1) return 1; - var count = linearLogRecur(n / 2) + linearLogRecur(n / 2); - for (var i = 0; i < n; i++) { + let count = linearLogRecur(n / 2) + linearLogRecur(n / 2); + for (let i = 0; i < n; i++) { count++; } return count; @@ -110,18 +110,18 @@ function linearLogRecur(n) { /* 阶乘阶(递归实现) */ function factorialRecur(n) { if (n == 0) return 1; - var count = 0; + let count = 0; // 从 1 个分裂出 n 个 - for (var i = 0; i < n; i++) { + for (let i = 0; i < n; i++) { count += factorialRecur(n - 1); } return count; } -var n = 8; +let n = 8; console.log("输入数据大小 n = " + n); -var count = constant(n); +let count = constant(n); console.log("常数阶的计算操作数量 = " + count); count = linear(n); @@ -131,8 +131,8 @@ console.log("线性阶(遍历数组)的计算操作数量 = " + count); count = quadratic(n); console.log("平方阶的计算操作数量 = " + count); -var nums = new Array(n); -for (var i = 0; i < n; i++) nums[i] = n - i; // [n,n-1,...,2,1] +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("平方阶(冒泡排序)的计算操作数量 = " + count); diff --git a/codes/typescript/chapter_computational_complexity/time_complexity.ts b/codes/typescript/chapter_computational_complexity/time_complexity.ts index 9c877602f..f722a1117 100644 --- a/codes/typescript/chapter_computational_complexity/time_complexity.ts +++ b/codes/typescript/chapter_computational_complexity/time_complexity.ts @@ -3,3 +3,157 @@ * Created Time: 2023-01-02 * Author: RiverTwilight (contact@rene.wang) */ + +/** + * File: time_complexity.js + * Created Time: 2023-01-02 + * Author: RiverTwilight (contact@rene.wang) + */ + +/* 常数阶 */ +function constant(n: number): number { + let count = 0; + const size = 100000; + for (let i = 0; i < size; i++) count++; + return count; +} + +/* 线性阶 */ +function linear(n: number): number { + let count = 0; + for (let i = 0; i < n; i++) count++; + return count; +} + +/* 线性阶(遍历数组) */ +function arrayTraversal(nums) { + let count = 0; + // 循环次数与数组长度成正比 + for (let i = 0; i < nums.length; i++) { + count++; + } + return count; +} + +/* 平方阶 */ +function quadratic(n: number): number { + let count = 0; + // 循环次数与数组长度成平方关系 + for (let i = 0; i < n; i++) { + for (let j = 0; j < n; j++) { + count++; + } + } + return count; +} + +/* 平方阶(冒泡排序) */ +function bubbleSort(nums: number[]): number { + let count = 0; // 计数器 + // 外循环:待排序元素数量为 n-1, n-2, ..., 1 + for (let i = nums.length - 1; i > 0; i--) { + // 内循环:冒泡操作 + for (let j = 0; j < i; j++) { + if (nums[j] > nums[j + 1]) { + // 交换 nums[j] 与 nums[j + 1] + let tmp = nums[j]; + nums[j] = nums[j + 1]; + nums[j + 1] = tmp; + count += 3; // 元素交换包含 3 个单元操作 + } + } + } + return count; +} + +/* 指数阶(循环实现) */ +function exponential(n: number): number { + let count = 0, + base = 1; + // cell 每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1) + for (let i = 0; i < n; i++) { + for (let j = 0; j < base; j++) { + count++; + } + base *= 2; + } + // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1 + return count; +} + +/* 指数阶(递归实现) */ +function expRecur(n: number): number { + if (n == 1) return 1; + return expRecur(n - 1) + expRecur(n - 1) + 1; +} + +/* 对数阶(循环实现) */ +function logarithmic(n: number): number { + let count = 0; + while (n > 1) { + n = n / 2; + count++; + } + return count; +} + +/* 对数阶(递归实现) */ +function logRecur(n: number): number { + if (n <= 1) return 0; + return logRecur(n / 2) + 1; +} + +/* 线性对数阶 */ +function linearLogRecur(n: number): number { + if (n <= 1) return 1; + let count = linearLogRecur(n / 2) + linearLogRecur(n / 2); + for (let i = 0; i < n; i++) { + count++; + } + return count; +} + +/* 阶乘阶(递归实现) */ +function factorialRecur(n: number): number { + if (n == 0) return 1; + let count = 0; + // 从 1 个分裂出 n 个 + for (let i = 0; i < n; i++) { + count += factorialRecur(n - 1); + } + return count; +} + +var n: number = 8; +console.log("输入数据大小 n = " + n); + +let count = constant(n); +console.log("常数阶的计算操作数量 = " + count); + +count = linear(n); +console.log("线性阶的计算操作数量 = " + count); +count = arrayTraversal(new Array(n)); +console.log("线性阶(遍历数组)的计算操作数量 = " + count); + +count = quadratic(n); +console.log("平方阶的计算操作数量 = " + count); +var 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("平方阶(冒泡排序)的计算操作数量 = " + count); + +count = exponential(n); +console.log("指数阶(循环实现)的计算操作数量 = " + count); +count = expRecur(n); +console.log("指数阶(递归实现)的计算操作数量 = " + count); + +count = logarithmic(n); +console.log("对数阶(循环实现)的计算操作数量 = " + count); +count = logRecur(n); +console.log("对数阶(递归实现)的计算操作数量 = " + count); + +count = linearLogRecur(n); +console.log("线性对数阶(递归实现)的计算操作数量 = " + count); + +count = factorialRecur(n); +console.log("阶乘阶(递归实现)的计算操作数量 = " + count); From 621fcb731c3bc833f0de095e3dc67b7fe98bd6a3 Mon Sep 17 00:00:00 2001 From: RiverTwilight Date: Mon, 2 Jan 2023 21:01:16 +0800 Subject: [PATCH 09/31] lint: switch indent type --- .../time_complexity.js | 2 +- .../time_complexity.ts | 14 ++++---------- 2 files changed, 5 insertions(+), 11 deletions(-) diff --git a/codes/javascript/chapter_computational_complexity/time_complexity.js b/codes/javascript/chapter_computational_complexity/time_complexity.js index d044f22f0..4cd6705bf 100644 --- a/codes/javascript/chapter_computational_complexity/time_complexity.js +++ b/codes/javascript/chapter_computational_complexity/time_complexity.js @@ -118,7 +118,7 @@ function factorialRecur(n) { return count; } -let n = 8; +const n = 8; console.log("输入数据大小 n = " + n); let count = constant(n); diff --git a/codes/typescript/chapter_computational_complexity/time_complexity.ts b/codes/typescript/chapter_computational_complexity/time_complexity.ts index f722a1117..0b1b5be4c 100644 --- a/codes/typescript/chapter_computational_complexity/time_complexity.ts +++ b/codes/typescript/chapter_computational_complexity/time_complexity.ts @@ -1,11 +1,5 @@ /** - * File: time_complexity.js - * Created Time: 2023-01-02 - * Author: RiverTwilight (contact@rene.wang) - */ - -/** - * File: time_complexity.js + * File: time_complexity.ts * Created Time: 2023-01-02 * Author: RiverTwilight (contact@rene.wang) */ @@ -26,7 +20,7 @@ function linear(n: number): number { } /* 线性阶(遍历数组) */ -function arrayTraversal(nums) { +function arrayTraversal(nums: number[]) { let count = 0; // 循环次数与数组长度成正比 for (let i = 0; i < nums.length; i++) { @@ -124,10 +118,10 @@ function factorialRecur(n: number): number { return count; } -var n: number = 8; +var n = 8; console.log("输入数据大小 n = " + n); -let count = constant(n); +var count = constant(n); console.log("常数阶的计算操作数量 = " + count); count = linear(n); From 63cd3e4f65117235461437df3f02604cf93aadc5 Mon Sep 17 00:00:00 2001 From: RiverTwilight Date: Mon, 2 Jan 2023 21:16:24 +0800 Subject: [PATCH 10/31] lint: added prettier config and switch indent type --- .prettierrc | 4 + .../time_complexity.js | 140 +++++++++--------- .../time_complexity.ts | 140 +++++++++--------- 3 files changed, 144 insertions(+), 140 deletions(-) create mode 100644 .prettierrc diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 000000000..5a938ce18 --- /dev/null +++ b/.prettierrc @@ -0,0 +1,4 @@ +{ + "tabWidth": 4, + "useTabs": false +} diff --git a/codes/javascript/chapter_computational_complexity/time_complexity.js b/codes/javascript/chapter_computational_complexity/time_complexity.js index 4cd6705bf..c8809caa0 100644 --- a/codes/javascript/chapter_computational_complexity/time_complexity.js +++ b/codes/javascript/chapter_computational_complexity/time_complexity.js @@ -6,116 +6,116 @@ /* 常数阶 */ function constant(n) { - let count = 0; - const size = 100000; - for (let i = 0; i < size; i++) count++; - return count; + let count = 0; + const size = 100000; + for (let i = 0; i < size; i++) count++; + return count; } /* 线性阶 */ function linear(n) { - let count = 0; - for (let i = 0; i < n; i++) count++; - return count; + let count = 0; + for (let i = 0; i < n; i++) count++; + return count; } /* 线性阶(遍历数组) */ function arrayTraversal(nums) { - let count = 0; - // 循环次数与数组长度成正比 - for (let i = 0; i < nums.length; i++) { - count++; - } - return count; + let count = 0; + // 循环次数与数组长度成正比 + for (let i = 0; i < nums.length; i++) { + count++; + } + return count; } /* 平方阶 */ function quadratic(n) { - let count = 0; - // 循环次数与数组长度成平方关系 - for (let i = 0; i < n; i++) { - for (let j = 0; j < n; j++) { - count++; - } - } - return count; + let count = 0; + // 循环次数与数组长度成平方关系 + for (let i = 0; i < n; i++) { + for (let j = 0; j < n; j++) { + count++; + } + } + return count; } /* 平方阶(冒泡排序) */ function bubbleSort(nums) { - let count = 0; // 计数器 - // 外循环:待排序元素数量为 n-1, n-2, ..., 1 - for (let i = nums.length - 1; i > 0; i--) { - // 内循环:冒泡操作 - for (let j = 0; j < i; j++) { - if (nums[j] > nums[j + 1]) { - // 交换 nums[j] 与 nums[j + 1] - let tmp = nums[j]; - nums[j] = nums[j + 1]; - nums[j + 1] = tmp; - count += 3; // 元素交换包含 3 个单元操作 - } - } - } - return count; + let count = 0; // 计数器 + // 外循环:待排序元素数量为 n-1, n-2, ..., 1 + for (let i = nums.length - 1; i > 0; i--) { + // 内循环:冒泡操作 + for (let j = 0; j < i; j++) { + if (nums[j] > nums[j + 1]) { + // 交换 nums[j] 与 nums[j + 1] + let tmp = nums[j]; + nums[j] = nums[j + 1]; + nums[j + 1] = tmp; + count += 3; // 元素交换包含 3 个单元操作 + } + } + } + return count; } /* 指数阶(循环实现) */ function exponential(n) { - let count = 0, - base = 1; - // cell 每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1) - for (let i = 0; i < n; i++) { - for (let j = 0; j < base; j++) { - count++; - } - base *= 2; - } - // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1 - return count; + let count = 0, + base = 1; + // cell 每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1) + for (let i = 0; i < n; i++) { + for (let j = 0; j < base; j++) { + count++; + } + base *= 2; + } + // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1 + return count; } /* 指数阶(递归实现) */ function expRecur(n) { - if (n == 1) return 1; - return expRecur(n - 1) + expRecur(n - 1) + 1; + if (n == 1) return 1; + return expRecur(n - 1) + expRecur(n - 1) + 1; } /* 对数阶(循环实现) */ function logarithmic(n) { - let count = 0; - while (n > 1) { - n = n / 2; - count++; - } - return count; + let count = 0; + while (n > 1) { + n = n / 2; + count++; + } + return count; } /* 对数阶(递归实现) */ function logRecur(n) { - if (n <= 1) return 0; - return logRecur(n / 2) + 1; + if (n <= 1) return 0; + return logRecur(n / 2) + 1; } /* 线性对数阶 */ function linearLogRecur(n) { - if (n <= 1) return 1; - let count = linearLogRecur(n / 2) + linearLogRecur(n / 2); - for (let i = 0; i < n; i++) { - count++; - } - return count; + if (n <= 1) return 1; + let count = linearLogRecur(n / 2) + linearLogRecur(n / 2); + for (let i = 0; i < n; i++) { + count++; + } + return count; } /* 阶乘阶(递归实现) */ function factorialRecur(n) { - if (n == 0) return 1; - let count = 0; - // 从 1 个分裂出 n 个 - for (let i = 0; i < n; i++) { - count += factorialRecur(n - 1); - } - return count; + if (n == 0) return 1; + let count = 0; + // 从 1 个分裂出 n 个 + for (let i = 0; i < n; i++) { + count += factorialRecur(n - 1); + } + return count; } const n = 8; diff --git a/codes/typescript/chapter_computational_complexity/time_complexity.ts b/codes/typescript/chapter_computational_complexity/time_complexity.ts index 0b1b5be4c..900585c81 100644 --- a/codes/typescript/chapter_computational_complexity/time_complexity.ts +++ b/codes/typescript/chapter_computational_complexity/time_complexity.ts @@ -6,116 +6,116 @@ /* 常数阶 */ function constant(n: number): number { - let count = 0; - const size = 100000; - for (let i = 0; i < size; i++) count++; - return count; + let count = 0; + const size = 100000; + for (let i = 0; i < size; i++) count++; + return count; } /* 线性阶 */ function linear(n: number): number { - let count = 0; - for (let i = 0; i < n; i++) count++; - return count; + let count = 0; + for (let i = 0; i < n; i++) count++; + return count; } /* 线性阶(遍历数组) */ function arrayTraversal(nums: number[]) { - let count = 0; - // 循环次数与数组长度成正比 - for (let i = 0; i < nums.length; i++) { - count++; - } - return count; + let count = 0; + // 循环次数与数组长度成正比 + for (let i = 0; i < nums.length; i++) { + count++; + } + return count; } /* 平方阶 */ function quadratic(n: number): number { - let count = 0; - // 循环次数与数组长度成平方关系 - for (let i = 0; i < n; i++) { - for (let j = 0; j < n; j++) { - count++; - } - } - return count; + let count = 0; + // 循环次数与数组长度成平方关系 + for (let i = 0; i < n; i++) { + for (let j = 0; j < n; j++) { + count++; + } + } + return count; } /* 平方阶(冒泡排序) */ function bubbleSort(nums: number[]): number { - let count = 0; // 计数器 - // 外循环:待排序元素数量为 n-1, n-2, ..., 1 - for (let i = nums.length - 1; i > 0; i--) { - // 内循环:冒泡操作 - for (let j = 0; j < i; j++) { - if (nums[j] > nums[j + 1]) { - // 交换 nums[j] 与 nums[j + 1] - let tmp = nums[j]; - nums[j] = nums[j + 1]; - nums[j + 1] = tmp; - count += 3; // 元素交换包含 3 个单元操作 - } - } - } - return count; + let count = 0; // 计数器 + // 外循环:待排序元素数量为 n-1, n-2, ..., 1 + for (let i = nums.length - 1; i > 0; i--) { + // 内循环:冒泡操作 + for (let j = 0; j < i; j++) { + if (nums[j] > nums[j + 1]) { + // 交换 nums[j] 与 nums[j + 1] + let tmp = nums[j]; + nums[j] = nums[j + 1]; + nums[j + 1] = tmp; + count += 3; // 元素交换包含 3 个单元操作 + } + } + } + return count; } /* 指数阶(循环实现) */ function exponential(n: number): number { - let count = 0, - base = 1; - // cell 每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1) - for (let i = 0; i < n; i++) { - for (let j = 0; j < base; j++) { - count++; - } - base *= 2; - } - // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1 - return count; + let count = 0, + base = 1; + // cell 每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1) + for (let i = 0; i < n; i++) { + for (let j = 0; j < base; j++) { + count++; + } + base *= 2; + } + // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1 + return count; } /* 指数阶(递归实现) */ function expRecur(n: number): number { - if (n == 1) return 1; - return expRecur(n - 1) + expRecur(n - 1) + 1; + if (n == 1) return 1; + return expRecur(n - 1) + expRecur(n - 1) + 1; } /* 对数阶(循环实现) */ function logarithmic(n: number): number { - let count = 0; - while (n > 1) { - n = n / 2; - count++; - } - return count; + let count = 0; + while (n > 1) { + n = n / 2; + count++; + } + return count; } /* 对数阶(递归实现) */ function logRecur(n: number): number { - if (n <= 1) return 0; - return logRecur(n / 2) + 1; + if (n <= 1) return 0; + return logRecur(n / 2) + 1; } /* 线性对数阶 */ function linearLogRecur(n: number): number { - if (n <= 1) return 1; - let count = linearLogRecur(n / 2) + linearLogRecur(n / 2); - for (let i = 0; i < n; i++) { - count++; - } - return count; + if (n <= 1) return 1; + let count = linearLogRecur(n / 2) + linearLogRecur(n / 2); + for (let i = 0; i < n; i++) { + count++; + } + return count; } /* 阶乘阶(递归实现) */ function factorialRecur(n: number): number { - if (n == 0) return 1; - let count = 0; - // 从 1 个分裂出 n 个 - for (let i = 0; i < n; i++) { - count += factorialRecur(n - 1); - } - return count; + if (n == 0) return 1; + let count = 0; + // 从 1 个分裂出 n 个 + for (let i = 0; i < n; i++) { + count += factorialRecur(n - 1); + } + return count; } var n = 8; From 92c8d34f13ad2b6e1359b41f27d2a5c31ae6a3ff Mon Sep 17 00:00:00 2001 From: RiverTwilight Date: Mon, 2 Jan 2023 21:53:21 +0800 Subject: [PATCH 11/31] lint: remove extra indent --- .../time_complexity.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/docs/chapter_computational_complexity/time_complexity.md b/docs/chapter_computational_complexity/time_complexity.md index 2ba703457..bd7cb119b 100644 --- a/docs/chapter_computational_complexity/time_complexity.md +++ b/docs/chapter_computational_complexity/time_complexity.md @@ -148,9 +148,9 @@ $$ “时间增长趋势”这个概念比较抽象,我们借助一个例子来理解。设输入数据大小为 $n$ ,给定三个算法 `A` , `B` , `C` 。 -- 算法 `A` 只有 $1$ 个打印操作,算法运行时间不随着 $n$ 增大而增长。我们称此算法的时间复杂度为「常数阶」。 -- 算法 `B` 中的打印操作需要循环 $n$ 次,算法运行时间随着 $n$ 增大成线性增长。此算法的时间复杂度被称为「线性阶」。 -- 算法 `C` 中的打印操作需要循环 $1000000$ 次,但运行时间仍与输入数据大小 $n$ 无关。因此 `C` 的时间复杂度和 `A` 相同,仍为「常数阶」。 +- 算法 `A` 只有 $1$ 个打印操作,算法运行时间不随着 $n$ 增大而增长。我们称此算法的时间复杂度为「常数阶」。 +- 算法 `B` 中的打印操作需要循环 $n$ 次,算法运行时间随着 $n$ 增大成线性增长。此算法的时间复杂度被称为「线性阶」。 +- 算法 `C` 中的打印操作需要循环 $1000000$ 次,但运行时间仍与输入数据大小 $n$ 无关。因此 `C` 的时间复杂度和 `A` 相同,仍为「常数阶」。 === "Java" @@ -1983,8 +1983,8 @@ $$ **某些算法的时间复杂度不是恒定的,而是与输入数据的分布有关。** 举一个例子,输入一个长度为 $n$ 数组 `nums` ,其中 `nums` 由从 $1$ 至 $n$ 的数字组成,但元素顺序是随机打乱的;算法的任务是返回元素 $1$ 的索引。我们可以得出以下结论: -- 当 `nums = [?, ?, ..., 1]`,即当末尾元素是 $1$ 时,则需完整遍历数组,此时达到 **最差时间复杂度 $O(n)$** ; -- 当 `nums = [1, ?, ?, ...]` ,即当首个数字为 $1$ 时,无论数组多长都不需要继续遍历,此时达到 **最佳时间复杂度 $\Omega(1)$** ; +- 当 `nums = [?, ?, ..., 1]`,即当末尾元素是 $1$ 时,则需完整遍历数组,此时达到 **最差时间复杂度 $O(n)$** ; +- 当 `nums = [1, ?, ?, ...]` ,即当首个数字为 $1$ 时,无论数组多长都不需要继续遍历,此时达到 **最佳时间复杂度 $\Omega(1)$** ; 「函数渐近上界」使用大 $O$ 记号表示,代表「最差时间复杂度」。与之对应,「函数渐近下界」用 $\Omega$ 记号(Omega Notation)来表示,代表「最佳时间复杂度」。 From 23b4aa118b413d72dfdbeeeaa766ee2795e84717 Mon Sep 17 00:00:00 2001 From: RiverTwilight Date: Mon, 2 Jan 2023 22:07:48 +0800 Subject: [PATCH 12/31] code: added scripts to the doc --- .../time_complexity.md | 229 ++++++++++++++++-- 1 file changed, 205 insertions(+), 24 deletions(-) diff --git a/docs/chapter_computational_complexity/time_complexity.md b/docs/chapter_computational_complexity/time_complexity.md index bd7cb119b..4328dc6b3 100644 --- a/docs/chapter_computational_complexity/time_complexity.md +++ b/docs/chapter_computational_complexity/time_complexity.md @@ -416,7 +416,6 @@ $$ } } - ``` === "TypeScript" @@ -432,7 +431,6 @@ $$ } } - ``` === "C" @@ -609,7 +607,6 @@ $$ } } } - ``` === "TypeScript" @@ -629,7 +626,6 @@ $$ } } } - ``` === "C" @@ -778,13 +774,24 @@ $$ === "JavaScript" ```js title="time_complexity.js" - + /* 常数阶 */ + function constant(n) { + let count = 0; + const size = 100000; + for (let i = 0; i < size; i++) count++; + return count; + } ``` === "TypeScript" ```typescript title="time_complexity.ts" - + function constant(n: number): number { + let count = 0; + const size = 100000; + for (let i = 0; i < size; i++) count++; + return count; + } ``` === "C" @@ -876,13 +883,23 @@ $$ === "JavaScript" ```js title="time_complexity.js" - + /* 线性阶 */ + function linear(n) { + let count = 0; + for (let i = 0; i < n; i++) count++; + return count; + } ``` === "TypeScript" ```typescript title="time_complexity.ts" - + /* 线性阶 */ + function linear(n: number): number { + let count = 0; + for (let i = 0; i < n; i++) count++; + return count; + } ``` === "C" @@ -980,13 +997,29 @@ $$ === "JavaScript" ```js title="time_complexity.js" - + /* 线性阶(遍历数组) */ + function arrayTraversal(nums) { + let count = 0; + // 循环次数与数组长度成正比 + for (let i = 0; i < nums.length; i++) { + count++; + } + return count; + } ``` === "TypeScript" ```typescript title="time_complexity.ts" - + /* 线性阶(遍历数组) */ + function arrayTraversal(nums: number[]) { + let count = 0; + // 循环次数与数组长度成正比 + for (let i = 0; i < nums.length; i++) { + count++; + } + return count; + } ``` === "C" @@ -1093,13 +1126,33 @@ $$ === "JavaScript" ```js title="time_complexity.js" - + /* 平方阶 */ + function quadratic(n) { + let count = 0; + // 循环次数与数组长度成平方关系 + for (let i = 0; i < n; i++) { + for (let j = 0; j < n; j++) { + count++; + } + } + return count; + } ``` === "TypeScript" ```typescript title="time_complexity.ts" - + /* 平方阶 */ + function quadratic(n: number): number { + let count = 0; + // 循环次数与数组长度成平方关系 + for (let i = 0; i < n; i++) { + for (let j = 0; j < n; j++) { + count++; + } + } + return count; + } ``` === "C" @@ -1244,13 +1297,47 @@ $$ === "JavaScript" ```js title="time_complexity.js" - + /* 平方阶(冒泡排序) */ + function bubbleSort(nums) { + let count = 0; // 计数器 + // 外循环:待排序元素数量为 n-1, n-2, ..., 1 + for (let i = nums.length - 1; i > 0; i--) { + // 内循环:冒泡操作 + for (let j = 0; j < i; j++) { + if (nums[j] > nums[j + 1]) { + // 交换 nums[j] 与 nums[j + 1] + let tmp = nums[j]; + nums[j] = nums[j + 1]; + nums[j + 1] = tmp; + count += 3; // 元素交换包含 3 个单元操作 + } + } + } + return count; + } ``` === "TypeScript" ```typescript title="time_complexity.ts" - + /* 平方阶(冒泡排序) */ + function bubbleSort(nums: number[]): number { + let count = 0; // 计数器 + // 外循环:待排序元素数量为 n-1, n-2, ..., 1 + for (let i = nums.length - 1; i > 0; i--) { + // 内循环:冒泡操作 + for (let j = 0; j < i; j++) { + if (nums[j] > nums[j + 1]) { + // 交换 nums[j] 与 nums[j + 1] + let tmp = nums[j]; + nums[j] = nums[j + 1]; + nums[j + 1] = tmp; + count += 3; // 元素交换包含 3 个单元操作 + } + } + } + return count; + } ``` === "C" @@ -1390,13 +1477,40 @@ $$ === "JavaScript" ```js title="time_complexity.js" + /* 指数阶(循环实现) */ + function exponential(n) { + let count = 0, + base = 1; + // cell 每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1) + for (let i = 0; i < n; i++) { + for (let j = 0; j < base; j++) { + count++; + } + base *= 2; + } + // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1 + return count; + } ``` === "TypeScript" ```typescript title="time_complexity.ts" - + /* 指数阶(循环实现) */ + function exponential(n: number): number { + let count = 0, + base = 1; + // cell 每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1) + for (let i = 0; i < n; i++) { + for (let j = 0; j < base; j++) { + count++; + } + base *= 2; + } + // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1 + return count; + } ``` === "C" @@ -1495,12 +1609,21 @@ $$ === "JavaScript" ```js title="time_complexity.js" - + /* 指数阶(递归实现) */ + function expRecur(n) { + if (n == 1) return 1; + return expRecur(n - 1) + expRecur(n - 1) + 1; + } ``` === "TypeScript" ```typescript title="time_complexity.ts" + /* 指数阶(递归实现) */ + function expRecur(n: number): number { + if (n == 1) return 1; + return expRecur(n - 1) + expRecur(n - 1) + 1; + } ``` @@ -1598,13 +1721,29 @@ $$ === "JavaScript" ```js title="time_complexity.js" - + /* 对数阶(循环实现) */ + function logarithmic(n) { + let count = 0; + while (n > 1) { + n = n / 2; + count++; + } + return count; + } ``` === "TypeScript" ```typescript title="time_complexity.ts" - + /* 对数阶(循环实现) */ + function logarithmic(n: number): number { + let count = 0; + while (n > 1) { + n = n / 2; + count++; + } + return count; + } ``` === "C" @@ -1694,13 +1833,21 @@ $$ === "JavaScript" ```js title="time_complexity.js" - + /* 对数阶(递归实现) */ + function logRecur(n) { + if (n <= 1) return 0; + return logRecur(n / 2) + 1; + } ``` === "TypeScript" ```typescript title="time_complexity.ts" - + /* 对数阶(递归实现) */ + function logRecur(n: number): number { + if (n <= 1) return 0; + return logRecur(n / 2) + 1; + } ``` === "C" @@ -1801,13 +1948,29 @@ $$ === "JavaScript" ```js title="time_complexity.js" - + /* 线性对数阶 */ + function linearLogRecur(n) { + if (n <= 1) return 1; + let count = linearLogRecur(n / 2) + linearLogRecur(n / 2); + for (let i = 0; i < n; i++) { + count++; + } + return count; + } ``` === "TypeScript" ```typescript title="time_complexity.ts" - + /* 线性对数阶 */ + function linearLogRecur(n: number): number { + if (n <= 1) return 1; + let count = linearLogRecur(n / 2) + linearLogRecur(n / 2); + for (let i = 0; i < n; i++) { + count++; + } + return count; + } ``` === "C" @@ -1926,13 +2089,31 @@ $$ === "JavaScript" ```js title="time_complexity.js" - + /* 阶乘阶(递归实现) */ + function factorialRecur(n) { + if (n == 0) return 1; + let count = 0; + // 从 1 个分裂出 n 个 + for (let i = 0; i < n; i++) { + count += factorialRecur(n - 1); + } + return count; + } ``` === "TypeScript" ```typescript title="time_complexity.ts" - + /* 阶乘阶(递归实现) */ + function factorialRecur(n: number): number { + if (n == 0) return 1; + let count = 0; + // 从 1 个分裂出 n 个 + for (let i = 0; i < n; i++) { + count += factorialRecur(n - 1); + } + return count; + } ``` === "C" From 36e83352b866a84be53d1e4a53eed6466ffd102c Mon Sep 17 00:00:00 2001 From: RiverTwilight Date: Tue, 3 Jan 2023 11:17:11 +0800 Subject: [PATCH 13/31] lint: added missing comment --- docs/chapter_computational_complexity/time_complexity.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/chapter_computational_complexity/time_complexity.md b/docs/chapter_computational_complexity/time_complexity.md index 4328dc6b3..22dc3472a 100644 --- a/docs/chapter_computational_complexity/time_complexity.md +++ b/docs/chapter_computational_complexity/time_complexity.md @@ -786,6 +786,7 @@ $$ === "TypeScript" ```typescript title="time_complexity.ts" + /* 常数阶 */ function constant(n: number): number { let count = 0; const size = 100000; From b5c9db935e8e0904ced4e0e09369a8b3fda13c64 Mon Sep 17 00:00:00 2001 From: Yudong Jin Date: Thu, 5 Jan 2023 01:00:56 +0800 Subject: [PATCH 14/31] Update docs/chapter_computational_complexity/time_complexity.md Co-authored-by: Justin Tse --- docs/chapter_computational_complexity/time_complexity.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/chapter_computational_complexity/time_complexity.md b/docs/chapter_computational_complexity/time_complexity.md index 22dc3472a..8ef61d061 100644 --- a/docs/chapter_computational_complexity/time_complexity.md +++ b/docs/chapter_computational_complexity/time_complexity.md @@ -684,7 +684,7 @@ $$
-| 操作数量 $T(n)$ | 时间复杂度 $O(f(n))$ | +| 操作数量 $T(n)$ | 时间复杂度 $O(f(n))$ | | ---------------------- | -------------------- | | $100000$ | $O(1)$ | | $3n + 2$ | $O(n)$ | From abdf1f3117fded4a4721462b37ffb960da869841 Mon Sep 17 00:00:00 2001 From: Yudong Jin Date: Thu, 5 Jan 2023 01:01:03 +0800 Subject: [PATCH 15/31] Update codes/javascript/chapter_computational_complexity/time_complexity.js Co-authored-by: Justin Tse --- .../chapter_computational_complexity/time_complexity.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/codes/javascript/chapter_computational_complexity/time_complexity.js b/codes/javascript/chapter_computational_complexity/time_complexity.js index c8809caa0..2e4688dd2 100644 --- a/codes/javascript/chapter_computational_complexity/time_complexity.js +++ b/codes/javascript/chapter_computational_complexity/time_complexity.js @@ -118,6 +118,8 @@ function factorialRecur(n) { return count; } +/* Driver Code */ +// 可以修改 n 运行,体会一下各种复杂度的操作数量变化趋势 const n = 8; console.log("输入数据大小 n = " + n); From c5a9eea0a94b1b5d6c184262bc49236b6489c0de Mon Sep 17 00:00:00 2001 From: Yudong Jin Date: Thu, 5 Jan 2023 01:01:17 +0800 Subject: [PATCH 16/31] Update codes/typescript/chapter_computational_complexity/time_complexity.ts Co-authored-by: Justin Tse --- .../chapter_computational_complexity/time_complexity.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/codes/typescript/chapter_computational_complexity/time_complexity.ts b/codes/typescript/chapter_computational_complexity/time_complexity.ts index 900585c81..fbfb470e7 100644 --- a/codes/typescript/chapter_computational_complexity/time_complexity.ts +++ b/codes/typescript/chapter_computational_complexity/time_complexity.ts @@ -20,7 +20,7 @@ function linear(n: number): number { } /* 线性阶(遍历数组) */ -function arrayTraversal(nums: number[]) { +function arrayTraversal(nums: number[]): number { let count = 0; // 循环次数与数组长度成正比 for (let i = 0; i < nums.length; i++) { From a29a584b6f7b6ee3d94dbcdfa8132fdeb6178123 Mon Sep 17 00:00:00 2001 From: Yudong Jin Date: Thu, 5 Jan 2023 01:01:28 +0800 Subject: [PATCH 17/31] Update codes/typescript/chapter_computational_complexity/time_complexity.ts Co-authored-by: Justin Tse --- .../chapter_computational_complexity/time_complexity.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/codes/typescript/chapter_computational_complexity/time_complexity.ts b/codes/typescript/chapter_computational_complexity/time_complexity.ts index fbfb470e7..136e5e25f 100644 --- a/codes/typescript/chapter_computational_complexity/time_complexity.ts +++ b/codes/typescript/chapter_computational_complexity/time_complexity.ts @@ -121,7 +121,7 @@ function factorialRecur(n: number): number { var n = 8; console.log("输入数据大小 n = " + n); -var count = constant(n); +let count = constant(n); console.log("常数阶的计算操作数量 = " + count); count = linear(n); From d5969e407057a1a940aff1c7e0786aa6b66406fd Mon Sep 17 00:00:00 2001 From: Yudong Jin Date: Thu, 5 Jan 2023 01:01:42 +0800 Subject: [PATCH 18/31] Update docs/chapter_computational_complexity/time_complexity.md Co-authored-by: Justin Tse --- docs/chapter_computational_complexity/time_complexity.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/chapter_computational_complexity/time_complexity.md b/docs/chapter_computational_complexity/time_complexity.md index 8ef61d061..5a44a17f1 100644 --- a/docs/chapter_computational_complexity/time_complexity.md +++ b/docs/chapter_computational_complexity/time_complexity.md @@ -1013,7 +1013,7 @@ $$ ```typescript title="time_complexity.ts" /* 线性阶(遍历数组) */ - function arrayTraversal(nums: number[]) { + function arrayTraversal(nums: number[]): number { let count = 0; // 循环次数与数组长度成正比 for (let i = 0; i < nums.length; i++) { From 3906c3df06e4bd33e38dcfcd9131a8f2413e25db Mon Sep 17 00:00:00 2001 From: Yudong Jin Date: Thu, 5 Jan 2023 01:01:53 +0800 Subject: [PATCH 19/31] Update docs/chapter_computational_complexity/time_complexity.md Co-authored-by: Justin Tse --- docs/chapter_computational_complexity/time_complexity.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/chapter_computational_complexity/time_complexity.md b/docs/chapter_computational_complexity/time_complexity.md index 5a44a17f1..e567718a5 100644 --- a/docs/chapter_computational_complexity/time_complexity.md +++ b/docs/chapter_computational_complexity/time_complexity.md @@ -601,8 +601,8 @@ $$ console.log(0); } // +n*n(技巧 3) - for (var i = 0; i < 2 * n; i++) { - for (var j = 0; j < n + 1; j++) { + for (let i = 0; i < 2 * n; i++) { + for (let j = 0; j < n + 1; j++) { console.log(0); } } From 9f7a9fde20a3cbca9cf5a647c23f46e9f9a04c5d Mon Sep 17 00:00:00 2001 From: Yudong Jin Date: Thu, 5 Jan 2023 01:02:00 +0800 Subject: [PATCH 20/31] Update codes/typescript/chapter_computational_complexity/time_complexity.ts Co-authored-by: Justin Tse --- .../chapter_computational_complexity/time_complexity.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/codes/typescript/chapter_computational_complexity/time_complexity.ts b/codes/typescript/chapter_computational_complexity/time_complexity.ts index 136e5e25f..e5dde67c6 100644 --- a/codes/typescript/chapter_computational_complexity/time_complexity.ts +++ b/codes/typescript/chapter_computational_complexity/time_complexity.ts @@ -118,7 +118,9 @@ function factorialRecur(n: number): number { return count; } -var n = 8; +/* Driver Code */ +// 可以修改 n 运行,体会一下各种复杂度的操作数量变化趋势 +const n = 8; console.log("输入数据大小 n = " + n); let count = constant(n); From 2acf85a6260a2c134aea77b29d7243f0854c2961 Mon Sep 17 00:00:00 2001 From: Yudong Jin Date: Thu, 5 Jan 2023 01:02:09 +0800 Subject: [PATCH 21/31] Update docs/chapter_computational_complexity/time_complexity.md Co-authored-by: Justin Tse --- docs/chapter_computational_complexity/time_complexity.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/chapter_computational_complexity/time_complexity.md b/docs/chapter_computational_complexity/time_complexity.md index e567718a5..b3dc3c35d 100644 --- a/docs/chapter_computational_complexity/time_complexity.md +++ b/docs/chapter_computational_complexity/time_complexity.md @@ -79,6 +79,7 @@ $$ === "JavaScript" ```js title="" + // 在某运行平台下 function algorithm(n) { var a = 2; // 1 ns a = a + 1; // 1 ns From 3fa04aeb4a2e7140292efbd2b24d9d3536ffbcd4 Mon Sep 17 00:00:00 2001 From: Yudong Jin Date: Thu, 5 Jan 2023 01:02:19 +0800 Subject: [PATCH 22/31] Update docs/chapter_computational_complexity/time_complexity.md Co-authored-by: Justin Tse --- docs/chapter_computational_complexity/time_complexity.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/chapter_computational_complexity/time_complexity.md b/docs/chapter_computational_complexity/time_complexity.md index b3dc3c35d..b26fdf2bf 100644 --- a/docs/chapter_computational_complexity/time_complexity.md +++ b/docs/chapter_computational_complexity/time_complexity.md @@ -617,7 +617,7 @@ $$ var a: number = 1; // +0(技巧 1) a = a + n; // +0(技巧 1) // +n(技巧 2) - for (var i = 0; i < 5 * n + 1; i++) { + for (let i = 0; i < 5 * n + 1; i++) { console.log(0); } // +n*n(技巧 3) From 03aeda84e22d2237e17078213e23680f93fb9332 Mon Sep 17 00:00:00 2001 From: Yudong Jin Date: Thu, 5 Jan 2023 01:02:51 +0800 Subject: [PATCH 23/31] Update docs/chapter_computational_complexity/time_complexity.md Co-authored-by: Justin Tse --- docs/chapter_computational_complexity/time_complexity.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/chapter_computational_complexity/time_complexity.md b/docs/chapter_computational_complexity/time_complexity.md index b26fdf2bf..ee1f6f0ce 100644 --- a/docs/chapter_computational_complexity/time_complexity.md +++ b/docs/chapter_computational_complexity/time_complexity.md @@ -621,8 +621,8 @@ $$ console.log(0); } // +n*n(技巧 3) - for (var i = 0; i < 2 * n; i++) { - for (var j = 0; j < n + 1; j++) { + for (let i = 0; i < 2 * n; i++) { + for (let j = 0; j < n + 1; j++) { console.log(0); } } From 51004b8a85d2a9d2efa35237c58e57994c2a32f0 Mon Sep 17 00:00:00 2001 From: Yudong Jin Date: Thu, 5 Jan 2023 01:05:59 +0800 Subject: [PATCH 24/31] Apply suggestions from code review Co-authored-by: Justin Tse --- .../time_complexity.md | 25 +++++++++++-------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/docs/chapter_computational_complexity/time_complexity.md b/docs/chapter_computational_complexity/time_complexity.md index ee1f6f0ce..9dcb5d84b 100644 --- a/docs/chapter_computational_complexity/time_complexity.md +++ b/docs/chapter_computational_complexity/time_complexity.md @@ -84,7 +84,8 @@ $$ var a = 2; // 1 ns a = a + 1; // 1 ns a = a * 2; // 10 ns - for(var i = 0; i < n; i++) { // 1 ns ,每轮都要执行 i++ + // 循环 n 次 + for(let i = 0; i < n; i++) { // 1 ns ,每轮都要执行 i++ console.log(0); // 5 ns } } @@ -93,11 +94,13 @@ $$ === "TypeScript" ```typescript title="" + // 在某运行平台下 function algorithm(n: number): void { var a: number = 2; // 1 ns a = a + 1; // 1 ns a = a * 2; // 10 ns - for(var i = 0; i < n; i++) { // 1 ns ,每轮都要执行 i++ + // 循环 n 次 + for(let i = 0; i < n; i++) { // 1 ns ,每轮都要执行 i++ console.log(0); // 5 ns } } @@ -241,13 +244,13 @@ $$ } // 算法 B 时间复杂度:线性阶 function algorithm_B(n) { - for (var i = 0; i < n; i++) { + for (let i = 0; i < n; i++) { console.log(0); } } // 算法 C 时间复杂度:常数阶 function algorithm_C(n) { - for (var i = 0; i < 1000000; i++) { + for (let i = 0; i < 1000000; i++) { console.log(0); } } @@ -263,13 +266,13 @@ $$ } // 算法 B 时间复杂度:线性阶 function algorithm_B(n: number): void { - for (var i = 0; i < n; i++) { + for (let i = 0; i < n; i++) { console.log(0); } } // 算法 C 时间复杂度:常数阶 function algorithm_C(n: number): void { - for (var i = 0; i < 1000000; i++) { + for (let i = 0; i < 1000000; i++) { console.log(0); } } @@ -412,7 +415,7 @@ $$ a += 1; // +1 a *= 2; // +1 // 循环 n 次 - for(var i = 0; i < n; i++){ // +1 + for(let i = 0; i < n; i++){ // +1(每轮都执行 i ++) console.log(0); // +1 } @@ -427,7 +430,7 @@ $$ a += 1; // +1 a *= 2; // +1 // 循环 n 次 - for(var i = 0; i < n; i++){ // +1 + for(let i = 0; i < n; i++){ // +1(每轮都执行 i ++) console.log(0); // +1 } @@ -595,10 +598,10 @@ $$ ```js title="" function algorithm(n) { - var a = 1; // +0(技巧 1) + let a = 1; // +0(技巧 1) a = a + n; // +0(技巧 1) // +n(技巧 2) - for (var i = 0; i < 5 * n + 1; i++) { + for (let i = 0; i < 5 * n + 1; i++) { console.log(0); } // +n*n(技巧 3) @@ -614,7 +617,7 @@ $$ ```typescript title="" function algorithm(n: number): void { - var a: number = 1; // +0(技巧 1) + let a = 1; // +0(技巧 1) a = a + n; // +0(技巧 1) // +n(技巧 2) for (let i = 0; i < 5 * n + 1; i++) { From 7cd1347b4453b207c28f12e7d353398cbb4a4b20 Mon Sep 17 00:00:00 2001 From: RiverTwilight Date: Thu, 5 Jan 2023 10:16:05 +0800 Subject: [PATCH 25/31] code(js): worst best time complexity --- .../worst_best_time_complexity.js | 41 +++++++++++++++++++ .../time_complexity.md | 34 +++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 codes/javascript/chapter_computational_complexity/worst_best_time_complexity.js diff --git a/codes/javascript/chapter_computational_complexity/worst_best_time_complexity.js b/codes/javascript/chapter_computational_complexity/worst_best_time_complexity.js new file mode 100644 index 000000000..db966b6fd --- /dev/null +++ b/codes/javascript/chapter_computational_complexity/worst_best_time_complexity.js @@ -0,0 +1,41 @@ +/* + * File: worst_best_time_complexity.js + * Created Time: 2023-01-05 + * Author: RiverTwilight (contact@rene.wang) + */ + +function randomNumbers(n) { + nums = Array(n); + for (let i = 0; i < n; i++) { + nums[i] = i + 1; + } + // 随机打乱数组元素 + for (let i = 0; i < n; i++) { + let r = Math.floor(Math.random() * n); + let temp = nums[i]; + nums[i] = nums[r]; + nums[r] = temp; + } + return nums; +} + +function findOne(nums) { + for (let i = 0; i < nums.length; i++) { + if (nums[i] == 1) { + return i; + } + } + return -1; +} + +function main() { + for (let i = 0; i < 10; i++) { + let n = 100; + let nums = randomNumbers(n); + let index = findOne(nums); + console.log( + "\n数组 [ 1, 2, ..., n ] 被打乱后 = [" + nums.join(", ") + "]" + ); + console.log("数字 1 的索引为 " + index); + } +} diff --git a/docs/chapter_computational_complexity/time_complexity.md b/docs/chapter_computational_complexity/time_complexity.md index 9dcb5d84b..9f9e36c96 100644 --- a/docs/chapter_computational_complexity/time_complexity.md +++ b/docs/chapter_computational_complexity/time_complexity.md @@ -2328,7 +2328,41 @@ $$ === "JavaScript" ```js title="worst_best_time_complexity.js" + function randomNumbers(n) { + nums = Array(n); + for (let i = 0; i < n; i++) { + nums[i] = i + 1; + } + // 随机打乱数组元素 + for (let i = 0; i < n; i++) { + let r = Math.floor(Math.random() * n); + let temp = nums[i]; + nums[i] = nums[r]; + nums[r] = temp; + } + return nums; + } + function findOne(nums) { + for (let i = 0; i < nums.length; i++) { + if (nums[i] == 1) { + return i; + } + } + return -1; + } + + function main() { + for (let i = 0; i < 10; i++) { + let n = 100; + let nums = randomNumbers(n); + let index = findOne(nums); + console.log( + "\n数组 [ 1, 2, ..., n ] 被打乱后 = [" + nums.join(", ") + "]" + ); + console.log("数字 1 的索引为 " + index); + } + } ``` === "TypeScript" From 8031e0e2c541846d90240b72431fca00b04b5c0e Mon Sep 17 00:00:00 2001 From: RiverTwilight Date: Thu, 5 Jan 2023 10:20:19 +0800 Subject: [PATCH 26/31] lint: added missing keyword --- .../worst_best_time_complexity.js | 2 +- .../worst_best_time_complexity.ts | 41 +++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 codes/typescript/chapter_computational_complexity/worst_best_time_complexity.ts diff --git a/codes/javascript/chapter_computational_complexity/worst_best_time_complexity.js b/codes/javascript/chapter_computational_complexity/worst_best_time_complexity.js index db966b6fd..f887fe28d 100644 --- a/codes/javascript/chapter_computational_complexity/worst_best_time_complexity.js +++ b/codes/javascript/chapter_computational_complexity/worst_best_time_complexity.js @@ -5,7 +5,7 @@ */ function randomNumbers(n) { - nums = Array(n); + let nums = Array(n); for (let i = 0; i < n; i++) { nums[i] = i + 1; } diff --git a/codes/typescript/chapter_computational_complexity/worst_best_time_complexity.ts b/codes/typescript/chapter_computational_complexity/worst_best_time_complexity.ts new file mode 100644 index 000000000..b263f994d --- /dev/null +++ b/codes/typescript/chapter_computational_complexity/worst_best_time_complexity.ts @@ -0,0 +1,41 @@ +/* + * File: worst_best_time_complexity.ts + * Created Time: 2023-01-05 + * Author: RiverTwilight (contact@rene.wang) + */ + +function randomNumbers(n: number): number[] { + nums = Array(n); + for (let i = 0; i < n; i++) { + nums[i] = i + 1; + } + // 随机打乱数组元素 + for (let i = 0; i < n; i++) { + let r = Math.floor(Math.random() * n); + let temp = nums[i]; + nums[i] = nums[r]; + nums[r] = temp; + } + return nums; +} + +function findOne(nums) { + for (let i = 0; i < nums.length; i++) { + if (nums[i] == 1) { + return i; + } + } + return -1; +} + +function main() { + for (let i = 0; i < 10; i++) { + let n = 100; + let nums = randomNumbers(n); + let index = findOne(nums); + console.log( + "\n数组 [ 1, 2, ..., n ] 被打乱后 = [" + nums.join(", ") + "]" + ); + console.log("数字 1 的索引为 " + index); + } +} From c53f64d56ba3d29b04cfc0657032fd76bd195143 Mon Sep 17 00:00:00 2001 From: RiverTwilight Date: Thu, 5 Jan 2023 10:23:12 +0800 Subject: [PATCH 27/31] code(ts): worst best time complexity --- .../worst_best_time_complexity.ts | 6 ++-- .../time_complexity.md | 36 ++++++++++++++++++- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/codes/typescript/chapter_computational_complexity/worst_best_time_complexity.ts b/codes/typescript/chapter_computational_complexity/worst_best_time_complexity.ts index b263f994d..97ca003a1 100644 --- a/codes/typescript/chapter_computational_complexity/worst_best_time_complexity.ts +++ b/codes/typescript/chapter_computational_complexity/worst_best_time_complexity.ts @@ -5,7 +5,7 @@ */ function randomNumbers(n: number): number[] { - nums = Array(n); + let nums = Array(n); for (let i = 0; i < n; i++) { nums[i] = i + 1; } @@ -19,7 +19,7 @@ function randomNumbers(n: number): number[] { return nums; } -function findOne(nums) { +function findOne(nums: number[]): number { for (let i = 0; i < nums.length; i++) { if (nums[i] == 1) { return i; @@ -28,7 +28,7 @@ function findOne(nums) { return -1; } -function main() { +function main(): void { for (let i = 0; i < 10; i++) { let n = 100; let nums = randomNumbers(n); diff --git a/docs/chapter_computational_complexity/time_complexity.md b/docs/chapter_computational_complexity/time_complexity.md index 9f9e36c96..815424dd3 100644 --- a/docs/chapter_computational_complexity/time_complexity.md +++ b/docs/chapter_computational_complexity/time_complexity.md @@ -2329,7 +2329,7 @@ $$ ```js title="worst_best_time_complexity.js" function randomNumbers(n) { - nums = Array(n); + let nums = Array(n); for (let i = 0; i < n; i++) { nums[i] = i + 1; } @@ -2368,7 +2368,41 @@ $$ === "TypeScript" ```typescript title="worst_best_time_complexity.ts" + function randomNumbers(n: number): number[] { + let nums = Array(n); + for (let i = 0; i < n; i++) { + nums[i] = i + 1; + } + // 随机打乱数组元素 + for (let i = 0; i < n; i++) { + let r = Math.floor(Math.random() * n); + let temp = nums[i]; + nums[i] = nums[r]; + nums[r] = temp; + } + return nums; + } + function findOne(nums: number[]): number { + for (let i = 0; i < nums.length; i++) { + if (nums[i] == 1) { + return i; + } + } + return -1; + } + + function main(): void { + for (let i = 0; i < 10; i++) { + let n = 100; + let nums = randomNumbers(n); + let index = findOne(nums); + console.log( + "\n数组 [ 1, 2, ..., n ] 被打乱后 = [" + nums.join(", ") + "]" + ); + console.log("数字 1 的索引为 " + index); + } + } ``` === "C" From 21096c8d0aba4cf68a00b2ce9e3159f0ee27bce4 Mon Sep 17 00:00:00 2001 From: RiverTwilight Date: Thu, 5 Jan 2023 10:27:48 +0800 Subject: [PATCH 28/31] lint: added comments --- .../worst_best_time_complexity.js | 4 ++++ .../worst_best_time_complexity.ts | 4 ++++ docs/chapter_computational_complexity/time_complexity.md | 8 ++++++++ 3 files changed, 16 insertions(+) diff --git a/codes/javascript/chapter_computational_complexity/worst_best_time_complexity.js b/codes/javascript/chapter_computational_complexity/worst_best_time_complexity.js index f887fe28d..791cfe7a2 100644 --- a/codes/javascript/chapter_computational_complexity/worst_best_time_complexity.js +++ b/codes/javascript/chapter_computational_complexity/worst_best_time_complexity.js @@ -4,8 +4,10 @@ * Author: RiverTwilight (contact@rene.wang) */ +/* 生成一个数组,元素为 { 1, 2, ..., n },顺序被打乱 */ function randomNumbers(n) { let nums = Array(n); + // 生成数组 nums = { 1, 2, 3, ..., n } for (let i = 0; i < n; i++) { nums[i] = i + 1; } @@ -19,6 +21,7 @@ function randomNumbers(n) { return nums; } +/* 查找数组 nums 中数字 1 所在索引 */ function findOne(nums) { for (let i = 0; i < nums.length; i++) { if (nums[i] == 1) { @@ -28,6 +31,7 @@ function findOne(nums) { return -1; } +/* Driver Code */ function main() { for (let i = 0; i < 10; i++) { let n = 100; diff --git a/codes/typescript/chapter_computational_complexity/worst_best_time_complexity.ts b/codes/typescript/chapter_computational_complexity/worst_best_time_complexity.ts index 97ca003a1..68ed9bc35 100644 --- a/codes/typescript/chapter_computational_complexity/worst_best_time_complexity.ts +++ b/codes/typescript/chapter_computational_complexity/worst_best_time_complexity.ts @@ -4,8 +4,10 @@ * Author: RiverTwilight (contact@rene.wang) */ +/* 生成一个数组,元素为 { 1, 2, ..., n },顺序被打乱 */ function randomNumbers(n: number): number[] { let nums = Array(n); + // 生成数组 nums = { 1, 2, 3, ..., n } for (let i = 0; i < n; i++) { nums[i] = i + 1; } @@ -19,6 +21,7 @@ function randomNumbers(n: number): number[] { return nums; } +/* 查找数组 nums 中数字 1 所在索引 */ function findOne(nums: number[]): number { for (let i = 0; i < nums.length; i++) { if (nums[i] == 1) { @@ -28,6 +31,7 @@ function findOne(nums: number[]): number { return -1; } +/* Driver Code */ function main(): void { for (let i = 0; i < 10; i++) { let n = 100; diff --git a/docs/chapter_computational_complexity/time_complexity.md b/docs/chapter_computational_complexity/time_complexity.md index 815424dd3..db2c5614d 100644 --- a/docs/chapter_computational_complexity/time_complexity.md +++ b/docs/chapter_computational_complexity/time_complexity.md @@ -2328,8 +2328,10 @@ $$ === "JavaScript" ```js title="worst_best_time_complexity.js" + /* 生成一个数组,元素为 { 1, 2, ..., n },顺序被打乱 */ function randomNumbers(n) { let nums = Array(n); + // 生成数组 nums = { 1, 2, 3, ..., n } for (let i = 0; i < n; i++) { nums[i] = i + 1; } @@ -2343,6 +2345,7 @@ $$ return nums; } + /* 查找数组 nums 中数字 1 所在索引 */ function findOne(nums) { for (let i = 0; i < nums.length; i++) { if (nums[i] == 1) { @@ -2352,6 +2355,7 @@ $$ return -1; } + /* Driver Code */ function main() { for (let i = 0; i < 10; i++) { let n = 100; @@ -2368,8 +2372,10 @@ $$ === "TypeScript" ```typescript title="worst_best_time_complexity.ts" + /* 生成一个数组,元素为 { 1, 2, ..., n },顺序被打乱 */ function randomNumbers(n: number): number[] { let nums = Array(n); + // 生成数组 nums = { 1, 2, 3, ..., n } for (let i = 0; i < n; i++) { nums[i] = i + 1; } @@ -2383,6 +2389,7 @@ $$ return nums; } + /* 查找数组 nums 中数字 1 所在索引 */ function findOne(nums: number[]): number { for (let i = 0; i < nums.length; i++) { if (nums[i] == 1) { @@ -2392,6 +2399,7 @@ $$ return -1; } + /* Driver Code */ function main(): void { for (let i = 0; i < 10; i++) { let n = 100; From e96272a06fcec961e401daa942cb89c61fd75d8a Mon Sep 17 00:00:00 2001 From: Yudong Jin Date: Fri, 6 Jan 2023 03:14:52 +0800 Subject: [PATCH 29/31] Apply suggestions from code review Co-authored-by: Justin Tse --- .../worst_best_time_complexity.js | 4 ++-- .../worst_best_time_complexity.ts | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/codes/javascript/chapter_computational_complexity/worst_best_time_complexity.js b/codes/javascript/chapter_computational_complexity/worst_best_time_complexity.js index 791cfe7a2..949e50b04 100644 --- a/codes/javascript/chapter_computational_complexity/worst_best_time_complexity.js +++ b/codes/javascript/chapter_computational_complexity/worst_best_time_complexity.js @@ -13,7 +13,7 @@ function randomNumbers(n) { } // 随机打乱数组元素 for (let i = 0; i < n; i++) { - let r = Math.floor(Math.random() * n); + let r = Math.floor(Math.random() * (i + 1)); let temp = nums[i]; nums[i] = nums[r]; nums[r] = temp; @@ -24,7 +24,7 @@ function randomNumbers(n) { /* 查找数组 nums 中数字 1 所在索引 */ function findOne(nums) { for (let i = 0; i < nums.length; i++) { - if (nums[i] == 1) { + if (nums[i] === 1) { return i; } } diff --git a/codes/typescript/chapter_computational_complexity/worst_best_time_complexity.ts b/codes/typescript/chapter_computational_complexity/worst_best_time_complexity.ts index 68ed9bc35..a7318634f 100644 --- a/codes/typescript/chapter_computational_complexity/worst_best_time_complexity.ts +++ b/codes/typescript/chapter_computational_complexity/worst_best_time_complexity.ts @@ -13,7 +13,7 @@ function randomNumbers(n: number): number[] { } // 随机打乱数组元素 for (let i = 0; i < n; i++) { - let r = Math.floor(Math.random() * n); + let r = Math.floor(Math.random() * (i + 1)); let temp = nums[i]; nums[i] = nums[r]; nums[r] = temp; @@ -24,7 +24,7 @@ function randomNumbers(n: number): number[] { /* 查找数组 nums 中数字 1 所在索引 */ function findOne(nums: number[]): number { for (let i = 0; i < nums.length; i++) { - if (nums[i] == 1) { + if (nums[i] === 1) { return i; } } From 52927cadad684a0d9e1d9583892566cf1ed185be Mon Sep 17 00:00:00 2001 From: RiverTwilight Date: Fri, 6 Jan 2023 08:28:59 +0800 Subject: [PATCH 30/31] fix: change shuffle algo in the doc --- docs/chapter_computational_complexity/time_complexity.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/chapter_computational_complexity/time_complexity.md b/docs/chapter_computational_complexity/time_complexity.md index db2c5614d..fa07bd542 100644 --- a/docs/chapter_computational_complexity/time_complexity.md +++ b/docs/chapter_computational_complexity/time_complexity.md @@ -2337,7 +2337,7 @@ $$ } // 随机打乱数组元素 for (let i = 0; i < n; i++) { - let r = Math.floor(Math.random() * n); + let r = Math.floor(Math.random() * (i + 1)); let temp = nums[i]; nums[i] = nums[r]; nums[r] = temp; @@ -2381,7 +2381,7 @@ $$ } // 随机打乱数组元素 for (let i = 0; i < n; i++) { - let r = Math.floor(Math.random() * n); + let r = Math.floor(Math.random() * (i + 1)); let temp = nums[i]; nums[i] = nums[r]; nums[r] = temp; From 5a24254f6b95d19bae6a366e0695df6ee91d026a Mon Sep 17 00:00:00 2001 From: RiverTwilight Date: Fri, 6 Jan 2023 08:32:10 +0800 Subject: [PATCH 31/31] lint --- docs/chapter_computational_complexity/time_complexity.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/chapter_computational_complexity/time_complexity.md b/docs/chapter_computational_complexity/time_complexity.md index fa07bd542..b6b68c443 100644 --- a/docs/chapter_computational_complexity/time_complexity.md +++ b/docs/chapter_computational_complexity/time_complexity.md @@ -2348,7 +2348,7 @@ $$ /* 查找数组 nums 中数字 1 所在索引 */ function findOne(nums) { for (let i = 0; i < nums.length; i++) { - if (nums[i] == 1) { + if (nums[i] === 1) { return i; } } @@ -2392,7 +2392,7 @@ $$ /* 查找数组 nums 中数字 1 所在索引 */ function findOne(nums: number[]): number { for (let i = 0; i < nums.length; i++) { - if (nums[i] == 1) { + if (nums[i] === 1) { return i; } }