Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

code: added Typescript and Javascript examples #196

Merged
merged 31 commits into from
Jan 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
c94101a
docs: added Typescript and Javascript examples
RiverTwilight Jan 1, 2023
3f00aa3
code: added code for time complexity chapter
RiverTwilight Jan 2, 2023
2bd24e6
code: added doc code
RiverTwilight Jan 2, 2023
8c73625
code: transcribe time_complexity.js
RiverTwilight Jan 2, 2023
f83dcce
fix: doesnt return anything
RiverTwilight Jan 2, 2023
6e2412f
lint: code lint
RiverTwilight Jan 2, 2023
db2a91b
lint: remove class and main
RiverTwilight Jan 2, 2023
d3e15a8
lint: var to let
RiverTwilight Jan 2, 2023
621fcb7
lint: switch indent type
RiverTwilight Jan 2, 2023
63cd3e4
lint: added prettier config and switch indent type
RiverTwilight Jan 2, 2023
92c8d34
lint: remove extra indent
RiverTwilight Jan 2, 2023
23b4aa1
code: added scripts to the doc
RiverTwilight Jan 2, 2023
36e8335
lint: added missing comment
RiverTwilight Jan 3, 2023
b5c9db9
Update docs/chapter_computational_complexity/time_complexity.md
krahets Jan 4, 2023
abdf1f3
Update codes/javascript/chapter_computational_complexity/time_complex…
krahets Jan 4, 2023
c5a9eea
Update codes/typescript/chapter_computational_complexity/time_complex…
krahets Jan 4, 2023
a29a584
Update codes/typescript/chapter_computational_complexity/time_complex…
krahets Jan 4, 2023
d5969e4
Update docs/chapter_computational_complexity/time_complexity.md
krahets Jan 4, 2023
3906c3d
Update docs/chapter_computational_complexity/time_complexity.md
krahets Jan 4, 2023
9f7a9fd
Update codes/typescript/chapter_computational_complexity/time_complex…
krahets Jan 4, 2023
2acf85a
Update docs/chapter_computational_complexity/time_complexity.md
krahets Jan 4, 2023
3fa04ae
Update docs/chapter_computational_complexity/time_complexity.md
krahets Jan 4, 2023
03aeda8
Update docs/chapter_computational_complexity/time_complexity.md
krahets Jan 4, 2023
51004b8
Apply suggestions from code review
krahets Jan 4, 2023
7cd1347
code(js): worst best time complexity
RiverTwilight Jan 5, 2023
8031e0e
lint: added missing keyword
RiverTwilight Jan 5, 2023
c53f64d
code(ts): worst best time complexity
RiverTwilight Jan 5, 2023
21096c8
lint: added comments
RiverTwilight Jan 5, 2023
e96272a
Apply suggestions from code review
krahets Jan 5, 2023
52927ca
fix: change shuffle algo in the doc
RiverTwilight Jan 6, 2023
5a24254
lint
RiverTwilight Jan 6, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"tabWidth": 4,
"useTabs": false
}
155 changes: 155 additions & 0 deletions codes/javascript/chapter_computational_complexity/time_complexity.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
/**
* File: time_complexity.js
* Created Time: 2023-01-02
* Author: RiverTwilight (contact@rene.wang)
*/

/* 常数阶 */
function constant(n) {
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;
}

/* 线性阶(遍历数组) */
function arrayTraversal(nums) {
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;
}

/* 平方阶(冒泡排序) */
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;
}

/* 指数阶(循环实现) */
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;
}

/* 指数阶(递归实现) */
function expRecur(n) {
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;
}

/* 对数阶(递归实现) */
function logRecur(n) {
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;
}

/* 阶乘阶(递归实现) */
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;
}

/* Driver Code */
// 可以修改 n 运行,体会一下各种复杂度的操作数量变化趋势
const n = 8;
krahets marked this conversation as resolved.
Show resolved Hide resolved
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);
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);

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);
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* File: worst_best_time_complexity.js
* Created Time: 2023-01-05
* 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;
}
// 随机打乱数组元素
for (let i = 0; i < n; i++) {
let r = Math.floor(Math.random() * (i + 1));
let temp = nums[i];
nums[i] = nums[r];
nums[r] = temp;
}
return nums;
}

/* 查找数组 nums 中数字 1 所在索引 */
function findOne(nums) {
for (let i = 0; i < nums.length; i++) {
if (nums[i] === 1) {
return i;
}
}
return -1;
}

/* Driver Code */
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);
}
}
155 changes: 155 additions & 0 deletions codes/typescript/chapter_computational_complexity/time_complexity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
/**
* File: time_complexity.ts
* Created Time: 2023-01-02
* Author: RiverTwilight (contact@rene.wang)
*/
krahets marked this conversation as resolved.
Show resolved Hide resolved

/* 常数阶 */
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: number[]): number {
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;
}

/* Driver Code */
// 可以修改 n 运行,体会一下各种复杂度的操作数量变化趋势
const n = 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);
Loading