2023-01-02 18:47:36 +08:00
|
|
|
/**
|
|
|
|
* File: time_complexity.js
|
|
|
|
* Created Time: 2023-01-02
|
|
|
|
* Author: RiverTwilight (contact@rene.wang)
|
|
|
|
*/
|
2023-01-02 19:10:48 +08:00
|
|
|
|
2023-01-02 20:49:35 +08:00
|
|
|
/* 常数阶 */
|
|
|
|
function constant(n) {
|
2023-01-02 21:16:24 +08:00
|
|
|
let count = 0;
|
|
|
|
const size = 100000;
|
|
|
|
for (let i = 0; i < size; i++) count++;
|
|
|
|
return count;
|
2023-01-02 20:49:35 +08:00
|
|
|
}
|
2023-01-02 19:10:48 +08:00
|
|
|
|
2023-01-02 20:49:35 +08:00
|
|
|
/* 线性阶 */
|
|
|
|
function linear(n) {
|
2023-01-02 21:16:24 +08:00
|
|
|
let count = 0;
|
|
|
|
for (let i = 0; i < n; i++) count++;
|
|
|
|
return count;
|
2023-01-02 20:49:35 +08:00
|
|
|
}
|
2023-01-02 19:10:48 +08:00
|
|
|
|
2023-01-02 20:49:35 +08:00
|
|
|
/* 线性阶(遍历数组) */
|
|
|
|
function arrayTraversal(nums) {
|
2023-01-02 21:16:24 +08:00
|
|
|
let count = 0;
|
|
|
|
// 循环次数与数组长度成正比
|
|
|
|
for (let i = 0; i < nums.length; i++) {
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
return count;
|
2023-01-02 20:49:35 +08:00
|
|
|
}
|
2023-01-02 19:10:48 +08:00
|
|
|
|
2023-01-02 20:49:35 +08:00
|
|
|
/* 平方阶 */
|
|
|
|
function quadratic(n) {
|
2023-01-02 21:16:24 +08:00
|
|
|
let count = 0;
|
|
|
|
// 循环次数与数组长度成平方关系
|
|
|
|
for (let i = 0; i < n; i++) {
|
|
|
|
for (let j = 0; j < n; j++) {
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return count;
|
2023-01-02 20:49:35 +08:00
|
|
|
}
|
2023-01-02 19:10:48 +08:00
|
|
|
|
2023-01-02 20:49:35 +08:00
|
|
|
/* 平方阶(冒泡排序) */
|
|
|
|
function bubbleSort(nums) {
|
2023-01-02 21:16:24 +08:00
|
|
|
let count = 0; // 计数器
|
2023-05-22 23:05:37 +08:00
|
|
|
// 外循环:未排序区间为 [0, i]
|
2023-01-02 21:16:24 +08:00
|
|
|
for (let i = nums.length - 1; i > 0; i--) {
|
2023-05-22 23:05:37 +08:00
|
|
|
// 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端
|
2023-01-02 21:16:24 +08:00
|
|
|
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;
|
2023-01-02 20:49:35 +08:00
|
|
|
}
|
2023-01-02 19:10:48 +08:00
|
|
|
|
2023-01-02 20:49:35 +08:00
|
|
|
/* 指数阶(循环实现) */
|
|
|
|
function exponential(n) {
|
2023-01-02 21:16:24 +08:00
|
|
|
let count = 0,
|
|
|
|
base = 1;
|
2023-08-08 23:16:33 +08:00
|
|
|
// 细胞每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1)
|
2023-01-02 21:16:24 +08:00
|
|
|
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;
|
2023-01-02 20:49:35 +08:00
|
|
|
}
|
2023-01-02 19:10:48 +08:00
|
|
|
|
2023-01-02 20:49:35 +08:00
|
|
|
/* 指数阶(递归实现) */
|
|
|
|
function expRecur(n) {
|
2023-08-03 14:44:49 +08:00
|
|
|
if (n === 1) return 1;
|
2023-01-02 21:16:24 +08:00
|
|
|
return expRecur(n - 1) + expRecur(n - 1) + 1;
|
2023-01-02 20:49:35 +08:00
|
|
|
}
|
2023-01-02 19:10:48 +08:00
|
|
|
|
2023-01-02 20:49:35 +08:00
|
|
|
/* 对数阶(循环实现) */
|
|
|
|
function logarithmic(n) {
|
2023-01-02 21:16:24 +08:00
|
|
|
let count = 0;
|
|
|
|
while (n > 1) {
|
|
|
|
n = n / 2;
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
return count;
|
2023-01-02 20:49:35 +08:00
|
|
|
}
|
2023-01-02 19:10:48 +08:00
|
|
|
|
2023-01-02 20:49:35 +08:00
|
|
|
/* 对数阶(递归实现) */
|
|
|
|
function logRecur(n) {
|
2023-01-02 21:16:24 +08:00
|
|
|
if (n <= 1) return 0;
|
|
|
|
return logRecur(n / 2) + 1;
|
2023-01-02 20:49:35 +08:00
|
|
|
}
|
2023-01-02 19:10:48 +08:00
|
|
|
|
2023-01-02 20:49:35 +08:00
|
|
|
/* 线性对数阶 */
|
|
|
|
function linearLogRecur(n) {
|
2023-01-02 21:16:24 +08:00
|
|
|
if (n <= 1) return 1;
|
|
|
|
let count = linearLogRecur(n / 2) + linearLogRecur(n / 2);
|
|
|
|
for (let i = 0; i < n; i++) {
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
return count;
|
2023-01-02 20:49:35 +08:00
|
|
|
}
|
2023-01-02 19:10:48 +08:00
|
|
|
|
2023-01-02 20:49:35 +08:00
|
|
|
/* 阶乘阶(递归实现) */
|
|
|
|
function factorialRecur(n) {
|
2023-08-03 14:44:49 +08:00
|
|
|
if (n === 0) return 1;
|
2023-01-02 21:16:24 +08:00
|
|
|
let count = 0;
|
|
|
|
// 从 1 个分裂出 n 个
|
|
|
|
for (let i = 0; i < n; i++) {
|
|
|
|
count += factorialRecur(n - 1);
|
|
|
|
}
|
|
|
|
return count;
|
2023-01-02 19:10:48 +08:00
|
|
|
}
|
|
|
|
|
2023-01-05 01:01:03 +08:00
|
|
|
/* Driver Code */
|
|
|
|
// 可以修改 n 运行,体会一下各种复杂度的操作数量变化趋势
|
2023-01-02 21:01:16 +08:00
|
|
|
const n = 8;
|
2023-04-17 21:58:11 +08:00
|
|
|
console.log('输入数据大小 n = ' + n);
|
2023-01-02 19:10:48 +08:00
|
|
|
|
2023-01-02 20:52:15 +08:00
|
|
|
let count = constant(n);
|
2023-08-20 14:51:39 +08:00
|
|
|
console.log('常数阶的操作数量 = ' + count);
|
2023-01-02 19:10:48 +08:00
|
|
|
|
2023-01-02 20:49:35 +08:00
|
|
|
count = linear(n);
|
2023-08-20 14:51:39 +08:00
|
|
|
console.log('线性阶的操作数量 = ' + count);
|
2023-01-02 20:49:35 +08:00
|
|
|
count = arrayTraversal(new Array(n));
|
2023-08-20 14:51:39 +08:00
|
|
|
console.log('线性阶(遍历数组)的操作数量 = ' + count);
|
2023-01-02 19:10:48 +08:00
|
|
|
|
2023-01-02 20:49:35 +08:00
|
|
|
count = quadratic(n);
|
2023-08-20 14:51:39 +08:00
|
|
|
console.log('平方阶的操作数量 = ' + count);
|
2023-01-02 20:52:15 +08:00
|
|
|
let nums = new Array(n);
|
|
|
|
for (let i = 0; i < n; i++) nums[i] = n - i; // [n,n-1,...,2,1]
|
2023-01-02 20:49:35 +08:00
|
|
|
count = bubbleSort(nums);
|
2023-08-20 14:51:39 +08:00
|
|
|
console.log('平方阶(冒泡排序)的操作数量 = ' + count);
|
2023-01-02 19:10:48 +08:00
|
|
|
|
2023-01-02 20:49:35 +08:00
|
|
|
count = exponential(n);
|
2023-08-20 14:51:39 +08:00
|
|
|
console.log('指数阶(循环实现)的操作数量 = ' + count);
|
2023-01-02 20:49:35 +08:00
|
|
|
count = expRecur(n);
|
2023-08-20 14:51:39 +08:00
|
|
|
console.log('指数阶(递归实现)的操作数量 = ' + count);
|
2023-01-02 19:10:48 +08:00
|
|
|
|
2023-01-02 20:49:35 +08:00
|
|
|
count = logarithmic(n);
|
2023-08-20 14:51:39 +08:00
|
|
|
console.log('对数阶(循环实现)的操作数量 = ' + count);
|
2023-01-02 20:49:35 +08:00
|
|
|
count = logRecur(n);
|
2023-08-20 14:51:39 +08:00
|
|
|
console.log('对数阶(递归实现)的操作数量 = ' + count);
|
2023-01-02 19:10:48 +08:00
|
|
|
|
2023-01-02 20:49:35 +08:00
|
|
|
count = linearLogRecur(n);
|
2023-08-20 14:51:39 +08:00
|
|
|
console.log('线性对数阶(递归实现)的操作数量 = ' + count);
|
2023-01-02 19:10:48 +08:00
|
|
|
|
2023-01-02 20:49:35 +08:00
|
|
|
count = factorialRecur(n);
|
2023-08-20 14:51:39 +08:00
|
|
|
console.log('阶乘阶(递归实现)的操作数量 = ' + count);
|