├── README.md └── index.js /README.md: -------------------------------------------------------------------------------- 1 | # Algorithms Course Code "Shadow Coding on youtube" 2 | 3 | ## Hey there.🙋‍♂️ 4 | 5 | ### All code for you. ❤️ 6 | 7 | ### Don't forget star ⭐ 8 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @desc Factorial Number => Recursion 3 | * @param {n} number 4 | * @returns Factorial of number 5 | * @analysis // Time Complexity : O(n) 6 | 7 | */ 8 | function factorilalR(n) { 9 | // Base Case 10 | if (n < 2) return 1; 11 | // Recursive Case 12 | return n * factorilalR(n - 1); 13 | } 14 | factorilalR(5); // output 120 15 | // ---------------------------------------- 16 | //* Search Algorithms 17 | /** 18 | * @desc Search Number => Linear Search Algorithm 19 | * @param {numbers} array 20 | * @param {n} search number 21 | * @returns if number is found return index else return -1 22 | * @analysis // Time Complexity : O(n) 23 | */ 24 | function linearSearch(numbers, n) { 25 | for (let i = 0; i < numbers.length; i++) { 26 | if (numbers[i] === n) return i; 27 | } 28 | return -1; 29 | } 30 | console.log(linearSearch([1, 4, 6, 8, 9, 2], 2)); // output 5 31 | 32 | /** 33 | * @desc Search Number => binary Search Algorithm 34 | * @param {arr} sortedArray 35 | * @param {n} search number 36 | * @returns if number is found return index 37 | * @analysis // Time Complexity : O(log n) 38 | */ 39 | function binarySearch(arr, n) { 40 | let leftIdx = 0; 41 | let rightIdx = arr.length - 1; 42 | while (leftIdx <= rightIdx) { 43 | let midIdx = Math.floor((leftIdx + rightIdx) / 2); 44 | if (arr[midIdx] === n) return midIdx; 45 | if (arr[midIdx] > n) { 46 | rightIdx = midIdx - 1; 47 | } else { 48 | leftIdx = midIdx + 1; 49 | } 50 | } 51 | } 52 | console.log(binarySearch([1, 2, 4, 6, 8], 4)); // output 2 53 | 54 | // ---------------------------------------- 55 | //* Sorting Algorithms 56 | /** 57 | * @desc sorting array by Bubblesort algorithm 58 | * @param {arr} 59 | * @returns sorted array 60 | * @analysis Time Complexity : O(n^2) Space Complexity : O(1) 61 | */ 62 | function bubbleSort(arr) { 63 | for (let i = 0; i < arr.length; i++) { 64 | for (let j = 0; j < arr.length; j++) { 65 | if (arr[j] > arr[j + 1]) { 66 | // Swap numbers 67 | let temp = arr[j]; // 99 68 | arr[j] = arr[j + 1]; // 2 69 | arr[j + 1] = temp; 70 | } 71 | } 72 | } 73 | return arr; 74 | } 75 | console.log(bubbleSort([99, 2, 5, 6, 78, 33])); // output [ 2, 5, 6, 33, 78, 99 ] 76 | /** 77 | * @desc sorting array by Selectionsort algorithm 78 | * @param {arr} 79 | * @returns sorted array 80 | * @analysis Time Complexity : O(n^2) Space Complexity : O(1) 81 | */ 82 | function selectionSort(arr) { 83 | let min; 84 | for (let i = 0; i < arr.length; i++) { 85 | min = i; 86 | for (let j = i + 1; j < arr.length; j++) { 87 | if (arr[j] < arr[min]) min = j; 88 | } 89 | if (min !== i) { 90 | let temp = arr[i]; 91 | arr[i] = arr[min]; 92 | arr[min] = temp; 93 | } 94 | } 95 | return arr; 96 | } 97 | console.log(selectionSort([99, 2, 5, 6, 78, 33])); // output [ 2, 5, 6, 33, 78, 99 ] 98 | /** 99 | * @desc sorting array by Insertionsort algorithm 100 | * @param {arr} 101 | * @returns sorted array 102 | * @analysis Time Complexity : O(n^2) Space Complexity : O(1) 103 | */ 104 | function insertionSort(arr) { 105 | for (let i = 1; i < arr.length; i++) { 106 | let insertNumber = arr[i]; 107 | let j = i - 1; 108 | while (j > -1 && arr[j] > insertNumber) { 109 | arr[j + 1] = arr[j]; 110 | j = j - 1; 111 | } 112 | arr[j + 1] = insertNumber; 113 | } 114 | return arr; 115 | } 116 | console.log(insertionSort([99, 2, 5, 6, 78, 33])); // output [ 2, 5, 6, 33, 78, 99 ] 117 | /** 118 | * @desc sorting array by Mergesort algorithm 119 | * @param {arr} 120 | * @returns sorted array 121 | * @analysis Time Complexity : O(n log(n)) Space Complexity : O(n) 122 | */ 123 | function mergeSort(array) { 124 | // O(log n) 125 | // Base Case 126 | if (array.length < 2) return array; 127 | // Recursive Case 128 | const mid = Math.floor(array.length / 2); 129 | const leftArray = array.slice(0, mid); 130 | const rightArray = array.slice(mid); 131 | return merge(mergeSort(leftArray), mergeSort(rightArray)); 132 | } 133 | function merge(leftArray, rightArray) { 134 | // O(n) 135 | const sortedArray = []; 136 | while (leftArray.length && rightArray.length) { 137 | if (leftArray[0] <= rightArray[0]) { 138 | sortedArray.push(leftArray.shift()); 139 | } else { 140 | sortedArray.push(rightArray.shift()); 141 | } 142 | } 143 | return [...sortedArray, ...leftArray, ...rightArray]; 144 | } 145 | console.log(mergeSort([99, 2, 5, 6, 78, 33])); // output [ 2, 5, 6, 33, 78, 99 ] 146 | /** 147 | * @desc sorting array by Quicksort algorithm 148 | * @param {array} 149 | * @returns sorted array 150 | * @analysis Time Complexity :O(n^2) Space Complexity : O(log(n)) 151 | */ 152 | function quicksort(array) { 153 | // Base Case 154 | if (array.length < 2) return array; 155 | // Recursive Case 156 | let povit = array[array.length - 1]; 157 | let left = []; // less 158 | let right = []; // gutter 159 | for (let i = 0; i < array.length - 1; i++) { 160 | if (array[i] > povit) right.push(array[i]); 161 | else left.push(array[i]); 162 | } 163 | return [...quicksort(left), povit, ...quicksort(right)]; 164 | } 165 | console.log(quicksort([99, 2, 5, 6, 78, 33])); // output [ 2, 5, 6, 33, 78, 99 ] 166 | --------------------------------------------------------------------------------