|
| 1 | +// ! Day 2 of learning DSA in Javascript |
| 2 | + |
| 3 | +// Todo: Topic 1 (Big O Notation) |
| 4 | + |
| 5 | +// Todo: O(n) -> ("signifies that the execution time of the algorithm grows linearly in proportion to the size of the input data(n) ") (Linear Search) (OK Time Complexity) |
| 6 | + |
| 7 | +const groceryList = ["Milk", "Eggs", "Bread", "Butter", "Cheese", "Tomatoes"]; |
| 8 | + |
| 9 | +function findGrocery(item) { |
| 10 | + for (i = 0; i < groceryList.length; i++) { |
| 11 | + if (groceryList[i] === item) { |
| 12 | + return `found ${item}`; |
| 13 | + } |
| 14 | + } |
| 15 | +} |
| 16 | +console.log(findGrocery("Butter")); |
| 17 | + |
| 18 | +// Todo: O(1) -> ("aka constant time signifies that the execution time of an algorithm remains constant regardless of the input size") (Good Time Complexity) |
| 19 | + |
| 20 | +const numberList = [1, 2, 3, 4, 5, 6, 7]; |
| 21 | + |
| 22 | +function findNumber(array, index) { |
| 23 | + return array[index]; |
| 24 | +} |
| 25 | + |
| 26 | +console.log(findNumber(numberList, 6)); |
| 27 | + |
| 28 | +// Todo: O(n^2) -> (" O(n^2) Indicates that the algorithm's execution time grows quadratically with the size of the input data (represent by n") (Quadratic) (Bad Time Complexity) |
| 29 | + |
| 30 | +function findPairs(array) { |
| 31 | + for (i = 0; i < array.length; i++) { |
| 32 | + for (j = i + 1; j < array.length; j++) { |
| 33 | + console.log(`Pair ${array[i]} ${array[j]}`); |
| 34 | + } |
| 35 | + } |
| 36 | +} |
| 37 | +const numbers = [1, 2, 3, 4, 5]; |
| 38 | +console.log(findPairs(numbers)); |
| 39 | + |
| 40 | +// Todo: O(log n) -> ("Time Complexity refers to an algorithm's runtime that grows algorithmically with the size of the input represented by n in simple terms as the input size increase the time it take for the algorithm to run increases slowly") (Binary Search) (Good Time Complexity) |
| 41 | + |
| 42 | +// Todo: O(n log n) -> ("We use in Sorting") (OK Time Complexity) |
| 43 | + |
| 44 | +// Todo: O(2n) -> ("Exponential Time Complexity (use in recursion)") (Bad Time Complexity) |
| 45 | +// Todo: O(3n) -> ("Exponential Time Complexity (use in recursion)") (Bad Time Complexity) |
| 46 | +// Todo: O(4n) -> ("Exponential Time Complexity (use in recursion)") (Bad Time Complexity) |
| 47 | + |
| 48 | +// Todo: O(n!) -> ("Worst Time Complexity") |
0 commit comments