function constant2(n) {
for (let i = 1; i <= 100; i++) {
console.log(i);
}
}
// always loops 100 times regardless of input n.
function log(n) {
if (n <= 1) return;
log(n / 2);
}
// recursively calling log function again but dividing input by 2 every time.
function log(n) {
let i = n;
while (i > 1) {
i /= 2;
}
}
// repeatedly looping in our while and cutting n by 2 until n is not greater than 1;
function linear(n) {
for (let i = 0; i <= n; i++) {
console.log(i);
}
}
// for loop increases based on size of n;
function linear(array) {
for (let i = 0; i < array.length; i++) {
console.log(i);
}
}
// same thing as above except increases with array length;
function linear(n) {
if (n === 1) return;
linear(n - 1);
}
// we use recursion here but as you can see the amt of recursive calls still scale linearly based on n input size. (if we were calling linear(n / 2) then this time complexity would be O(log(n)));
const find = (li, value) => {
for (let i = 0; i < li.length; i++) {
if (li[i] === value) return true;
}
return false;
};
const printAlot = (li) => {
for (let i = 0; i < li.length; i++) {
for (let j = 0; j < 10; j++) {
console.log(li[j]);
}
}
};
// We have a double for loop but our inner is a fixed loop, therefore not making it polynomial time.
function actuallyLin(n) {
if (n <= 1) return;
for (let i = 0; i <= n; i++) {
console.log(i);
}
actuallyLin(n / 2);
}
function logLinear(n) {
if (n <= 1) return;
for (let i = 0; i <= n; i++) {
console.log(i);
}
logLinear(n / 4);
logLinear(n / 4);
logLinear(n / 4);
// logLinear(n / 2);
}
// runtime is log linear
// T(nlog(n)); Merge sort;
const splitButIterate = (li) => {
if (li.length < 2) return li;
const midIdx = li.length / 2;
splitButIterate(li.slice(0, midIdx));
splitButIterate(li.slice(midIdx));
li.forEach((ele) => console.log(ele));
};
function quadratic(n) {
for (let i = 0; i <= n; i++) {
for (let j = 0; j <= n; j++) {
console.log(i, j);
}
}
}
// double for loops, loops n twice! O(n^2);
function cubic(n) {
for (let i = 1; i <= n; i++) {
for (let j = 1; j <= n; j++) {
for (let k = 1; k <= n; k++) {}
}
}
}
// triple for loops, loops n three times! O(n^3);
function expo(n) {
if (n === 1) return;
exponential(n - 1);
exponential(n - 1);
exponential(n - 1);
}