diff --git a/Searching algos/Binary_Search.js b/Searching algos/Binary_Search.js new file mode 100644 index 0000000..5343e42 --- /dev/null +++ b/Searching algos/Binary_Search.js @@ -0,0 +1,39 @@ +class BinarySearch { + static iterativeBinarySearch(array, valueToSearch) { + let low = 0; + let high = array.length - 1; + + while (low <= high) { + const middle = Math.floor((low + high) / 2); + + if (array[middle] < valueToSearch) { + low = middle + 1; + } else if (array[middle] > valueToSearch) { + high = middle - 1; + } else { + return middle; // Value found at index `middle` + } + } + return -1; // Value not found + } + + static recursiveBinarySearch(array, valueToSearch, low, high) { + if (high < low) return -1; // Base case: value not found + + const middle = Math.floor((low + high) / 2); + + if (array[middle] === valueToSearch) { + return middle; // Value found at index `middle` + } else if (array[middle] > valueToSearch) { + return this.recursiveBinarySearch(array, valueToSearch, low, middle - 1); // Search left + } else { + return this.recursiveBinarySearch(array, valueToSearch, middle + 1, high); // Search right + } + } + } + + // Example usage: + const arrayToSearch = [1, 4, 5, 6, 7, 10, 14, 15, 21, 33, 34]; + console.log(BinarySearch.iterativeBinarySearch(arrayToSearch, 6)); // Output: 3 + console.log(BinarySearch.recursiveBinarySearch(arrayToSearch, 15, 0, arrayToSearch.length - 1)); // Output: 7 + \ No newline at end of file diff --git a/Searching algos/Linear_Search.js b/Searching algos/Linear_Search.js new file mode 100644 index 0000000..b999e4b --- /dev/null +++ b/Searching algos/Linear_Search.js @@ -0,0 +1,21 @@ +class LinearSearch { + static linear(array, key) { + for (let index = 0; index < array.length; index++) { + if (array[index] === key) { + return index; + } + } + return -1; + } +} + +// Example usage: +const array = [5, 6, 12, 4, 56, 87, 12]; +const key = 56; +const index = LinearSearch.linear(array, key); + +if (index === -1) { + console.log("Element not found."); +} else { + console.log("Element found at index " + index); +} diff --git a/Searching algos/binarySearch.java b/Searching algos/binarySearch.java index 6acb4ac..80ddad8 100644 --- a/Searching algos/binarySearch.java +++ b/Searching algos/binarySearch.java @@ -1,43 +1,43 @@ -// Java implementation of iterative Binary Search -class BinarySearch { - // Returns index of x if it is present in arr[], - // else return -1 - int binarySearch(int arr[], int x) - { - int l = 0, r = arr.length - 1; - while (l <= r) { - int m = l + (r - l) / 2; - - // Check if x is present at mid - if (arr[m] == x) - return m; - - // If x greater, ignore left half - if (arr[m] < x) - l = m + 1; - - // If x is smaller, ignore right half - else - r = m - 1; - } - - // if we reach here, then element was - // not present - return -1; - } - - // Driver method to test above - public static void main(String args[]) - { - BinarySearch ob = new BinarySearch(); - int arr[] = { 2, 3, 4, 10, 40 }; - int n = arr.length; - int x = 10; - int result = ob.binarySearch(arr, x); - if (result == -1) - System.out.println("Element not present"); - else - System.out.println("Element found at " - + "index " + result); - } +// Java implementation of iterative Binary Search +class BinarySearch { + // Returns index of x if it is present in arr[], + // else return -1 + int binarySearch(int arr[], int x) + { + int l = 0, r = arr.length - 1; + while (l <= r) { + int m = l + (r - l) / 2; + + // Check if x is present at mid + if (arr[m] == x) + return m; + + // If x greater, ignore left half + if (arr[m] < x) + l = m + 1; + + // If x is smaller, ignore right half + else + r = m - 1; + } + + // if we reach here, then element was + // not present + return -1; + } + + // Driver method to test above + public static void main(String args[]) + { + BinarySearch ob = new BinarySearch(); + int arr[] = { 2, 3, 4, 10, 40 }; + int n = arr.length; + int x = 10; + int result = ob.binarySearch(arr, x); + if (result == -1) + System.out.println("Element not present"); + else + System.out.println("Element found at " + + "index " + result); + } } \ No newline at end of file