|
|
|
@ -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"
|
|
|
|
|