-
Notifications
You must be signed in to change notification settings - Fork 0
/
BigO.js
83 lines (74 loc) · 1.52 KB
/
BigO.js
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Big O = O(1)
function constantTime(arr) {
console.log(arr[0]);
}
function constantTime2(arr) {
console.log(arr[0]);
console.log(arr[1]);
}
// Big O = O(n)
function linearTime(arr) {
for (let i = 0; i < arr.length; i++) {
console.log(arr[i]);
}
}
function linearTime2(arr) {
for (let i = 0; i < arr.length; i++) {
console.log(arr[i]);
}
for (let i = 0; i < arr.length; i++) {
console.log(arr[i] + "!");
}
}
function linearTime3(arr) {
console.log(arr[0]);
const middleIndex = Math.floor(arr.length/2)
let index = 0
while (index < middleIndex) {
console.log(arr[index]);
index++;
}
for (let i = 0; i < arr.length; i++) {
console.log(100);
}
}
function findNemo() {
const fish = [
"dory",
"bruce",
"nemo",
"marlin",
"gill",
"bloat",
"nigel",
"squirt",
];
for (let i = 0; i < fish.length; i++) {
if (fish[i] === "nemo") {
console.log("Found nemo");
break; // we assume that nemo is at the end of the array
}
}
}
// Big O = O(n + m): Rule #3
function linearTime2(arr1, arr2) {
for (let i = 0; i < arr.length; i++) {
console.log(arr1[i]);
}
for (let i = 0; i < arr.length; i++) {
console.log(arr2[i] + "!");
}
}
// O(n^2) - Quadratic Time
const pairs = [1, 2, 3, 4, 5];
for (let i = 0; i < pairs.length; i++) {
for (let n = 0; n < pairs.length; i++) {
console.log(pairs[i], pairs[n]);
}
}
// Space Complexity - O(1)
function boo(array) {
for (let i = 0; i < array.length; i++) {
console.log(array[i]);
}
}