-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
45 lines (36 loc) · 725 Bytes
/
index.js
File metadata and controls
45 lines (36 loc) · 725 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// algorithm 算法 algorithm
// 复杂度分为: O(1) O(n) O(n^2) O(logN) O(nlogN)
// 复杂度为 O(n) 线性阶
function main(n) {
while (--n > 0) {
console.log(n);
}
}
// O(n^2) 平方阶
function N2(n) {
for (let index = 0; index < n; index++) {
for (let j = 0; j < index; j++) {
console.log('j is', j);
}
}
}
// main(5)
// O(logN) 对数阶
function logN(n) {
let i = 1;
while (i < n) {
console.log(i);
i = i * 2;
}
console.log('end');
}
// O(nlogn) 线性对数阶
function nlogN(n) {
for (let i = 0; i < n; i++) {
let j = i;
while(j < n) {
console.log(i);
j = j * 2;
}
}
}