|
|
@ -4,9 +4,8 @@
|
|
|
|
* Author: RiverTwilight (contact@rene.wang)
|
|
|
|
* Author: RiverTwilight (contact@rene.wang)
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
class time_complexity {
|
|
|
|
|
|
|
|
/* 常数阶 */
|
|
|
|
/* 常数阶 */
|
|
|
|
constant(n) {
|
|
|
|
function constant(n) {
|
|
|
|
var count = 0;
|
|
|
|
var count = 0;
|
|
|
|
const size = 100000;
|
|
|
|
const size = 100000;
|
|
|
|
for (var i = 0; i < size; i++) count++;
|
|
|
|
for (var i = 0; i < size; i++) count++;
|
|
|
@ -14,14 +13,14 @@ class time_complexity {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 线性阶 */
|
|
|
|
/* 线性阶 */
|
|
|
|
linear(n) {
|
|
|
|
function linear(n) {
|
|
|
|
var count = 0;
|
|
|
|
var count = 0;
|
|
|
|
for (var i = 0; i < n; i++) count++;
|
|
|
|
for (var i = 0; i < n; i++) count++;
|
|
|
|
return count;
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 线性阶(遍历数组) */
|
|
|
|
/* 线性阶(遍历数组) */
|
|
|
|
arrayTraversal(nums) {
|
|
|
|
function arrayTraversal(nums) {
|
|
|
|
var count = 0;
|
|
|
|
var count = 0;
|
|
|
|
// 循环次数与数组长度成正比
|
|
|
|
// 循环次数与数组长度成正比
|
|
|
|
for (var i = 0; i < nums.length; i++) {
|
|
|
|
for (var i = 0; i < nums.length; i++) {
|
|
|
@ -31,7 +30,7 @@ class time_complexity {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 平方阶 */
|
|
|
|
/* 平方阶 */
|
|
|
|
quadratic(n) {
|
|
|
|
function quadratic(n) {
|
|
|
|
var count = 0;
|
|
|
|
var count = 0;
|
|
|
|
// 循环次数与数组长度成平方关系
|
|
|
|
// 循环次数与数组长度成平方关系
|
|
|
|
for (var i = 0; i < n; i++) {
|
|
|
|
for (var i = 0; i < n; i++) {
|
|
|
@ -43,7 +42,7 @@ class time_complexity {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 平方阶(冒泡排序) */
|
|
|
|
/* 平方阶(冒泡排序) */
|
|
|
|
bubbleSort(nums) {
|
|
|
|
function bubbleSort(nums) {
|
|
|
|
var count = 0; // 计数器
|
|
|
|
var count = 0; // 计数器
|
|
|
|
// 外循环:待排序元素数量为 n-1, n-2, ..., 1
|
|
|
|
// 外循环:待排序元素数量为 n-1, n-2, ..., 1
|
|
|
|
for (var i = nums.length - 1; i > 0; i--) {
|
|
|
|
for (var i = nums.length - 1; i > 0; i--) {
|
|
|
@ -62,7 +61,7 @@ class time_complexity {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 指数阶(循环实现) */
|
|
|
|
/* 指数阶(循环实现) */
|
|
|
|
exponential(n) {
|
|
|
|
function exponential(n) {
|
|
|
|
var count = 0,
|
|
|
|
var count = 0,
|
|
|
|
base = 1;
|
|
|
|
base = 1;
|
|
|
|
// cell 每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1)
|
|
|
|
// cell 每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1)
|
|
|
@ -77,13 +76,13 @@ class time_complexity {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 指数阶(递归实现) */
|
|
|
|
/* 指数阶(递归实现) */
|
|
|
|
expRecur(n) {
|
|
|
|
function expRecur(n) {
|
|
|
|
if (n == 1) return 1;
|
|
|
|
if (n == 1) return 1;
|
|
|
|
return this.expRecur(n - 1) + this.expRecur(n - 1) + 1;
|
|
|
|
return expRecur(n - 1) + expRecur(n - 1) + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 对数阶(循环实现) */
|
|
|
|
/* 对数阶(循环实现) */
|
|
|
|
logarithmic(n) {
|
|
|
|
function logarithmic(n) {
|
|
|
|
var count = 0;
|
|
|
|
var count = 0;
|
|
|
|
while (n > 1) {
|
|
|
|
while (n > 1) {
|
|
|
|
n = n / 2;
|
|
|
|
n = n / 2;
|
|
|
@ -93,15 +92,15 @@ class time_complexity {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 对数阶(递归实现) */
|
|
|
|
/* 对数阶(递归实现) */
|
|
|
|
logRecur(n) {
|
|
|
|
function logRecur(n) {
|
|
|
|
if (n <= 1) return 0;
|
|
|
|
if (n <= 1) return 0;
|
|
|
|
return this.logRecur(n / 2) + 1;
|
|
|
|
return logRecur(n / 2) + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 线性对数阶 */
|
|
|
|
/* 线性对数阶 */
|
|
|
|
linearLogRecur(n) {
|
|
|
|
function linearLogRecur(n) {
|
|
|
|
if (n <= 1) return 1;
|
|
|
|
if (n <= 1) return 1;
|
|
|
|
var count = this.linearLogRecur(n / 2) + this.linearLogRecur(n / 2);
|
|
|
|
var count = linearLogRecur(n / 2) + linearLogRecur(n / 2);
|
|
|
|
for (var i = 0; i < n; i++) {
|
|
|
|
for (var i = 0; i < n; i++) {
|
|
|
|
count++;
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -109,51 +108,46 @@ class time_complexity {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 阶乘阶(递归实现) */
|
|
|
|
/* 阶乘阶(递归实现) */
|
|
|
|
factorialRecur(n) {
|
|
|
|
function factorialRecur(n) {
|
|
|
|
if (n == 0) return 1;
|
|
|
|
if (n == 0) return 1;
|
|
|
|
var count = 0;
|
|
|
|
var count = 0;
|
|
|
|
// 从 1 个分裂出 n 个
|
|
|
|
// 从 1 个分裂出 n 个
|
|
|
|
for (var i = 0; i < n; i++) {
|
|
|
|
for (var i = 0; i < n; i++) {
|
|
|
|
count += this.factorialRecur(n - 1);
|
|
|
|
count += factorialRecur(n - 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(function main() {
|
|
|
|
|
|
|
|
var test = new time_complexity();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var n = 8;
|
|
|
|
var n = 8;
|
|
|
|
console.log("输入数据大小 n = " + n);
|
|
|
|
console.log("输入数据大小 n = " + n);
|
|
|
|
|
|
|
|
|
|
|
|
var count = test.constant(n);
|
|
|
|
var count = constant(n);
|
|
|
|
console.log("常数阶的计算操作数量 = " + count);
|
|
|
|
console.log("常数阶的计算操作数量 = " + count);
|
|
|
|
|
|
|
|
|
|
|
|
count = test.linear(n);
|
|
|
|
count = linear(n);
|
|
|
|
console.log("线性阶的计算操作数量 = " + count);
|
|
|
|
console.log("线性阶的计算操作数量 = " + count);
|
|
|
|
count = test.arrayTraversal(new Array(n));
|
|
|
|
count = arrayTraversal(new Array(n));
|
|
|
|
console.log("线性阶(遍历数组)的计算操作数量 = " + count);
|
|
|
|
console.log("线性阶(遍历数组)的计算操作数量 = " + count);
|
|
|
|
|
|
|
|
|
|
|
|
count = test.quadratic(n);
|
|
|
|
count = quadratic(n);
|
|
|
|
console.log("平方阶的计算操作数量 = " + count);
|
|
|
|
console.log("平方阶的计算操作数量 = " + count);
|
|
|
|
var nums = new Array(n);
|
|
|
|
var nums = new Array(n);
|
|
|
|
for (var i = 0; i < n; i++) nums[i] = n - i; // [n,n-1,...,2,1]
|
|
|
|
for (var i = 0; i < n; i++) nums[i] = n - i; // [n,n-1,...,2,1]
|
|
|
|
count = test.bubbleSort(nums);
|
|
|
|
count = bubbleSort(nums);
|
|
|
|
console.log("平方阶(冒泡排序)的计算操作数量 = " + count);
|
|
|
|
console.log("平方阶(冒泡排序)的计算操作数量 = " + count);
|
|
|
|
|
|
|
|
|
|
|
|
count = test.exponential(n);
|
|
|
|
count = exponential(n);
|
|
|
|
console.log("指数阶(循环实现)的计算操作数量 = " + count);
|
|
|
|
console.log("指数阶(循环实现)的计算操作数量 = " + count);
|
|
|
|
count = test.expRecur(n);
|
|
|
|
count = expRecur(n);
|
|
|
|
console.log("指数阶(递归实现)的计算操作数量 = " + count);
|
|
|
|
console.log("指数阶(递归实现)的计算操作数量 = " + count);
|
|
|
|
|
|
|
|
|
|
|
|
count = test.logarithmic(n);
|
|
|
|
count = logarithmic(n);
|
|
|
|
console.log("对数阶(循环实现)的计算操作数量 = " + count);
|
|
|
|
console.log("对数阶(循环实现)的计算操作数量 = " + count);
|
|
|
|
count = test.logRecur(n);
|
|
|
|
count = logRecur(n);
|
|
|
|
console.log("对数阶(递归实现)的计算操作数量 = " + count);
|
|
|
|
console.log("对数阶(递归实现)的计算操作数量 = " + count);
|
|
|
|
|
|
|
|
|
|
|
|
count = test.linearLogRecur(n);
|
|
|
|
count = linearLogRecur(n);
|
|
|
|
console.log("线性对数阶(递归实现)的计算操作数量 = " + count);
|
|
|
|
console.log("线性对数阶(递归实现)的计算操作数量 = " + count);
|
|
|
|
|
|
|
|
|
|
|
|
count = test.factorialRecur(n);
|
|
|
|
count = factorialRecur(n);
|
|
|
|
console.log("阶乘阶(递归实现)的计算操作数量 = " + count);
|
|
|
|
console.log("阶乘阶(递归实现)的计算操作数量 = " + count);
|
|
|
|
})();
|
|
|
|
|
|
|
|