Repository: TheAlgorithms/Java
Pull request #4469 (2023-10-01 05:12:06)

Added [FEATURE REQUEST] <Recursive Binary Search> #4457
<!-- For completed items, change [ ] to [x] -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized it.
- [x] All filenames are in PascalCase.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.


debnath003: I have given the recursive binary search code in Java and created a PR, could you please merge it? @debasishbsws @BamaCharanChhandogi 
debnath003: I have resolved all the resolved conversations, could you please review it? @debasishbsws 
debnath003: @debasishbsws I have done the required changes and updated the codes, could you please merge my PR?
debnath003: I have updated the test code of java but the checks are not getting completed, why is this so???
debnath003: @debasishbsws could you please review the changes?
debnath003: After editing that part, will you merge this PR? Coz everything is okay in that code
debnath003: I have done what you have told me as follows: my class RecursiveBinarySearch should extend the SearchAlogrithm class and implement the find method, can you please check it @debasishbsws 

Patch:
diff --git a/src/main/java/com/thealgorithms/searches/RecursiveBinarySearch.java b/src/main/java/com/thealgorithms/searches/RecursiveBinarySearch.java
new file mode 100644
index 00000000..8a02b30a
--- /dev/null
+++ b/src/main/java/com/thealgorithms/searches/RecursiveBinarySearch.java
@@ -0,0 +1,74 @@
+// Code by Pronay Debnath
+// Created:- 1/10/2023
+// File Name should be RecursiveBinarySearch.java
+// Explanation:- https://www.tutorialspoint.com/java-program-for-binary-search-recursive
+
+import java.util.*;
+
+// Create a SearchAlgorithm class with a generic type
+abstract class SearchAlgorithm<T extends Comparable<T>> {
+    // Abstract find method to be implemented by subclasses
+    public abstract int find(T[] arr, T target);
+}
+
+public class RecursiveBinarySearch<T extends Comparable<T>> extends SearchAlgorithm<T> {
+
+    // Override the find method as required
+    @Override
+    public int find(T[] arr, T target) {
+        // Call the recursive binary search function
+        return binsear(arr, 0, arr.length - 1, target);
+    }
+
+    // Recursive binary search function
+    public int binsear(T[] arr, int left, int right, T target) {
+        if (right >= left) {
+            int mid = left + (right - left) / 2;
+
+            // Compare the element at the middle with the target
+            int comparison = arr[mid].compareTo(target);
+
+            // If the element is equal to the target, return its index
+            if (comparison == 0) {
+                return mid;
+            }
+
+            // If the element is greater than the target, search in the left subarray
+            if (comparison > 0) {
+                return binsear(arr, left, mid - 1, target);
+            }
+
+            // Otherwise, search in the right subarray
+            return binsear(arr, mid + 1, right, target);
+        }
+
+        // Element is not present in the array
+        return -1;
+    }
+
+    public static void main(String[] args) {
+        Scanner sc = new Scanner(System.in);
+        // User inputs
+        System.out.print("Enter the number of elements in the array: ");
+        int n = sc.nextInt();
+
+        Integer[] a = new Integer[n]; // You can change the array type as needed
+
+        System.out.println("Enter the elements in sorted order:");
+
+        for (int i = 0; i < n; i++) {
+            a[i] = sc.nextInt();
+        }
+
+        System.out.print("Enter the target element to search for: ");
+        int t = sc.nextInt();
+
+        RecursiveBinarySearch<Integer> searcher = new RecursiveBinarySearch<>();
+        int res = searcher.find(a, t);
+
+        if (res == -1)
+            System.out.println("Element not found in the array.");
+        else
+            System.out.println("Element found at index " + res);
+    }
+}


Test patch:
diff --git a/src/test/java/com/thealgorithms/searches/RecursiveBinarySearchTest.java b/src/test/java/com/thealgorithms/searches/RecursiveBinarySearchTest.java
new file mode 100644
index 00000000..3b2cdead
--- /dev/null
+++ b/src/test/java/com/thealgorithms/searches/RecursiveBinarySearchTest.java
@@ -0,0 +1,40 @@
+// Created by Pronay Debnath
+// Date:- 1/10/2023
+// Test file updated with JUnit tests
+
+import static org.junit.jupiter.api.Assertions.*;
+
+import org.junit.jupiter.api.Test; // Import the JUnit 5 Test annotation
+
+public class RecursiveBinarySearchTest {
+
+    @Test
+    public void testBinarySearch() {
+        // Create an instance of GenericBinarySearch
+        RecursiveBinarySearch<Integer> searcher = new RecursiveBinarySearch<>();
+
+        // Test case 1: Element found in the array
+        Integer[] arr1 = {1, 2, 3, 4, 5};
+        int target1 = 3;
+        int result1 = searcher.binsear(arr1, 0, arr1.length - 1, target1);
+        assertEquals(2, result1);
+
+        // Test case 2: Element not found in the array
+        Integer[] arr2 = {1, 2, 3, 4, 5};
+        int target2 = 6;
+        int result2 = searcher.binsear(arr2, 0, arr2.length - 1, target2);
+        assertEquals(-1, result2);
+
+        // Test case 3: Element found at the beginning of the array
+        Integer[] arr3 = {10, 20, 30, 40, 50};
+        int target3 = 10;
+        int result3 = searcher.binsear(arr3, 0, arr3.length - 1, target3);
+        assertEquals(0, result3);
+
+        // Test case 4: Element found at the end of the array
+        Integer[] arr4 = {10, 20, 30, 40, 50};
+        int target4 = 50;
+        int result4 = searcher.binsear(arr4, 0, arr4.length - 1, target4);
+        assertEquals(4, result4);
+    }
+}