Created
July 25, 2017 16:15
-
-
Save tannaurus/6fbea8b8fce5f7b2f4c4f2a3720cb0e3 to your computer and use it in GitHub Desktop.
Identify the Big 0 of basic algorithms
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Drill #1: | |
function isEven(value){ | |
if (value % 2 == 0){ | |
return true; | |
} | |
else | |
return false; | |
} | |
// O(1) Constant, because it was arithmetic | |
Drill #2: | |
function areYouHere(arr1, arr2) { | |
for (let i=0; i<arr1.length; i++) { | |
const el1 = arr1[i]; | |
for (let j=0; j<arr2.length; j++) { | |
const el2 = arr2[j]; | |
if (el1 === el2) return true; | |
} | |
} | |
return false; | |
} | |
// O(n^2) Polynomial, nested loops (two of them, indicated by the number 2) | |
Drill #3: | |
function doubleArrayValues(array) { | |
for (let i=0; i<array.length; i++) { | |
array[i] *= 2; | |
} | |
return array; | |
} | |
// O(n) Linear, looping through a single array | |
Drill #4: | |
function naiveSearch(array, item) { | |
for (let i=0; i<array.length; i++) { | |
if (array[i] === item) { | |
return i; | |
} | |
} | |
} | |
// 0(n) Linear, similar to the last one. Looping through a single array. | |
Drill #5: | |
function createPairs(arr) { | |
for (let i = 0; i < arr.length; i++) { | |
for(let j = i+1; j < arr.length; j++) { | |
console.log(arr[i] + ", " + arr[j] ); | |
} | |
} | |
} | |
// O(n^2) Polynomial, nested loops (two of them, indicated by the number 2) | |
Drill #6: | |
function generateFib(num) { | |
let result = []; | |
for (let i = 1; i <= num; i++) { | |
// we're adding the first item | |
// to the result list, append the | |
// number 0 to results | |
if (i === 1) { | |
result.push(0); | |
} | |
// ...and if it's the second item | |
// append 1 | |
else if (i == 2) { | |
result.push(1); | |
} | |
// otherwise, sum the two previous result items, and append that value to results. | |
else { | |
result.push(result[i - 2] + result[i - 3]); | |
} | |
} | |
// once the for loop finishes | |
// we return `result`. | |
return result; | |
} | |
//0(n) Linear, the loop gets run a single time per each number passed into the function | |
Drill #7: | |
function efficientSearch(array, item) { | |
let minIndex = 0; | |
let maxIndex = array.length - 1; | |
let currentIndex; | |
let currentElement; | |
while (minIndex <= maxIndex) { | |
currentIndex = Math.floor((minIndex + maxIndex) / 2); | |
currentElement = array[currentIndex]; | |
if (currentElement < item) { | |
minIndex = currentIndex + 1; | |
} | |
else if (currentElement > item) { | |
maxIndex = currentIndex - 1; | |
} | |
else { | |
return currentIndex; | |
} | |
} | |
return -1; | |
} | |
//0(Log(n)) Due to the way it splits things in half, the time it takes to handle larger numbers isn't much | |
Drill #8: | |
function findRandomElement(arr) { | |
return arr[Math.floor(Math.random() * arr.length)]; | |
} | |
//0(1) Due it's simplicity | |
Drill #9: | |
function isPrime(n) { | |
// if n is less than 2 or a decimal, it's not prime | |
if (n < 2 || n % 1 != 0) { | |
return false; | |
} | |
// otherwise, check if `n` is divisible by any integer | |
// between 2 and n. | |
for (let i = 2; i < n; ++i) { | |
if (n % i == 0) return false; | |
} | |
return true; | |
} | |
//0(n) Due to it, in the worst case, always having to count to the number itself. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment