diff --git a/snippets/java/searching/binary-search.md b/snippets/java/searching/binary-search.md new file mode 100644 index 00000000..8e1490be --- /dev/null +++ b/snippets/java/searching/binary-search.md @@ -0,0 +1,39 @@ +--- +title: Binary Search +description: Searches a specific element in an array in optimised manner +author: SarvariHarshitha +tags: searching, binary-search +--- + +```java +public class BinarySearchRecursive { + public static int binarySearch(int[] arr, int low, int high, int key) { + if (low <= high) { + int mid = low + (high - low) / 2; + + if (arr[mid] == key) { + return mid; // Key found + } else if (arr[mid] < key) { + return binarySearch(arr, mid + 1, high, key); // Search in the right half + } else { + return binarySearch(arr, low, mid - 1, key); // Search in the left half + } + } + return -1; // Key not found + } + + public static void main(String[] args) { + int[] numbers = {10, 20, 30, 40, 50}; + int key = 30; + + int result = binarySearch(numbers, 0, numbers.length - 1, key); + + if (result != -1) { + System.out.println("Element found at index: " + result); //Result : Element found at index: 2 + } else { + System.out.println("Element not found in the array."); + } + } +} + +``` \ No newline at end of file diff --git a/snippets/java/searching/linear-search.md b/snippets/java/searching/linear-search.md new file mode 100644 index 00000000..daad843f --- /dev/null +++ b/snippets/java/searching/linear-search.md @@ -0,0 +1,34 @@ +--- +title: Linear Search +description: Searches a specific element in an array +author: SarvariHarshitha +tags: searching, linear-search +--- + +```java +public class LinearSearch { + public static int linearSearch(int[] arr, int key) { + for (int i = 0; i < arr.length; i++) { + if (arr[i] == key) { + return i; // Return the index where the key is found + } + } + return -1; // Return -1 if the key is not found + } + + public static void main(String[] args) { + int[] numbers = {10, 20, 30, 40, 50}; + int key = 30; + + int result = linearSearch(numbers, key); + + if (result != -1) { + System.out.println("Element found at index: " + result); // Result : Element found at 2 + } else { + System.out.println("Element not found in the array."); + } + } +} + + +``` \ No newline at end of file