//Log all pairs of array
const boxes = ['a', 'b', 'c', 'd', 'e'];
function logAllPairsOfArray(array){
for(let i = 0; i < array.length; i++){
for(let j = 0; j < array.length; j++){
console.log(array[i], array[j])
}
}
}
logAllPairsOfArray(boxes);
// Loops that are nested use multiplication
// O(n * n) => O(n^2) = Quadratic Time
const nemo = ['nemo']; // O(1)
const everyone = ['dory', 'bruce', 'marlin', 'nemo', 'gill', 'bloat', 'nigel', 'squirt', 'darla', 'hank']; // O(10)
const large = new Array(1000).fill('nemo'); // O(1000)
// loops thru everything in the array
function findNemo(array){
for(let i = 0; i < array.length; i++){
console.log('running')
if(array[i] === 'nemo'){
console.log('Found Nemo!');
}
}
}
findNemo(large); // O(n) --> Linear Time
// n mean number of inputs (size of the input) and compared to the number of operations that increase thats what scalability means
// O(1) - Constant Time
// only grabbing the first item in the array, no matter how big the array is it doesnt matter
//function compresFirstBox(boxes){
//console.log(boxes[0]);
//}
// What is the number of operations here in total?
const boxes = [0, 1, 3, 4, 5];
function logFirstTwoBoxes(boxes) {
console.log(boxes[0]); // 0(1)
console.log(boxes[1]); // 0(1)
}
logFirstTwoBoxes(boxes); // O(2)
const {performance} = require('perf_hooks');
const nemo = ['nemo'];
const everyone = ['dory', 'bruce', 'marlin', 'nemo', 'gill', 'bloat', 'nigel', 'squirt', 'darla', 'hank'];
const large = new Array(1000).fill('nemo');
// loops thru everything in the array
function findNemo(array){
let t0 = performance.now();
for(let i = 0; i < array.length; i++){
if(array[i] === 'nemo'){
console.log('Found Nemo!');
}
}
let t1 = performance.now();
// two timers one at the beginning, one at the end after the function goes through the loop to find nemo
console.log('Call to find Nemo took ' + (t1 - t0) + ' milliseconds')
}
findNemo(large);
const nemo = ['nemo']; // O(1)
const everyone = ['dory', 'bruce', 'marlin', 'nemo', 'gill', 'bloat', 'nigel', 'squirt', 'darla', 'hank']; // O(10)
const large = new Array(1000).fill('nemo'); // O(1000)
// loops thru everything in the array
function findNemo(array){
// worst case means going thru 10 loops
for(let i = 0; i < array.length; i++){
console.log('running')
if(array[i] === 'nemo'){
console.log('Found Nemo!');
break; // exits out of loop if nemo is found, instead of looping thru all the items it will stop at nemo
// makes code more efficient
}
}
}
findNemo(everyone);