Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Determine the Big-O complexity:
- Even or odd: constant time complexity 0(1)
- function isEven(value){
- if (value % 2 == 0){
- return true;
- }
- else
- return false;
- }
- // No matter the size of your input, the algorithm will take the same amount of time to complete.
- // Examples of constant time complexity are: Adding, dividing, accessing an array item.
- Are you here?: Polynomial 0(n^k)
- 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;
- }
- // The easiest way to understand polynomial time complexity is with nested loops.
- // An algorithm that requires two levels of looping over an input would be O(n^2).
- Doubler: linear run time complexity 0(n)
- function doubleArrayValues(array) {
- for (let i=0; i<array.length; i++) {
- array[i] *= 2;
- }
- return array;
- }
- //
Add Comment
Please, Sign In to add comment