Skip to content

Commit da687c1

Browse files
authored
Added [FEATURE REQUEST] <Recursive Binary Search> #4457 (#4469)
* Create RecursiveBinarySearch.java * Update RecursiveBinarySearch.java * Update RecursiveBinarySearch.java * Update RecursiveBinarySearch.java * Update RecursiveBinarySearch.java * Create ReverseArray.java * Update RecursiveBinarySearch.java * Update RecursiveBinarySearch.java * Create RecursiveBinarySearchTest.java * Update RecursiveBinarySearchTest.java * Update RecursiveBinarySearchTest.java * Delete src/main/java/com/thealgorithms/others/ReverseArray.java * Update RecursiveBinarySearchTest.java * Update RecursiveBinarySearchTest.java * Create ReverseArray.java * Delete src/main/java/com/thealgorithms/others/ReverseArray.java * Update RecursiveBinarySearchTest.java * Update RecursiveBinarySearch.java
1 parent ee2629c commit da687c1

File tree

2 files changed

+114
-0
lines changed

2 files changed

+114
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// Code by Pronay Debnath
2+
// Created:- 1/10/2023
3+
// File Name should be RecursiveBinarySearch.java
4+
// Explanation:- https://www.tutorialspoint.com/java-program-for-binary-search-recursive
5+
6+
import java.util.*;
7+
8+
// Create a SearchAlgorithm class with a generic type
9+
abstract class SearchAlgorithm<T extends Comparable<T>> {
10+
// Abstract find method to be implemented by subclasses
11+
public abstract int find(T[] arr, T target);
12+
}
13+
14+
public class RecursiveBinarySearch<T extends Comparable<T>> extends SearchAlgorithm<T> {
15+
16+
// Override the find method as required
17+
@Override
18+
public int find(T[] arr, T target) {
19+
// Call the recursive binary search function
20+
return binsear(arr, 0, arr.length - 1, target);
21+
}
22+
23+
// Recursive binary search function
24+
public int binsear(T[] arr, int left, int right, T target) {
25+
if (right >= left) {
26+
int mid = left + (right - left) / 2;
27+
28+
// Compare the element at the middle with the target
29+
int comparison = arr[mid].compareTo(target);
30+
31+
// If the element is equal to the target, return its index
32+
if (comparison == 0) {
33+
return mid;
34+
}
35+
36+
// If the element is greater than the target, search in the left subarray
37+
if (comparison > 0) {
38+
return binsear(arr, left, mid - 1, target);
39+
}
40+
41+
// Otherwise, search in the right subarray
42+
return binsear(arr, mid + 1, right, target);
43+
}
44+
45+
// Element is not present in the array
46+
return -1;
47+
}
48+
49+
public static void main(String[] args) {
50+
Scanner sc = new Scanner(System.in);
51+
// User inputs
52+
System.out.print("Enter the number of elements in the array: ");
53+
int n = sc.nextInt();
54+
55+
Integer[] a = new Integer[n]; // You can change the array type as needed
56+
57+
System.out.println("Enter the elements in sorted order:");
58+
59+
for (int i = 0; i < n; i++) {
60+
a[i] = sc.nextInt();
61+
}
62+
63+
System.out.print("Enter the target element to search for: ");
64+
int t = sc.nextInt();
65+
66+
RecursiveBinarySearch<Integer> searcher = new RecursiveBinarySearch<>();
67+
int res = searcher.find(a, t);
68+
69+
if (res == -1)
70+
System.out.println("Element not found in the array.");
71+
else
72+
System.out.println("Element found at index " + res);
73+
}
74+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Created by Pronay Debnath
2+
// Date:- 1/10/2023
3+
// Test file updated with JUnit tests
4+
5+
import static org.junit.jupiter.api.Assertions.*;
6+
7+
import org.junit.jupiter.api.Test; // Import the JUnit 5 Test annotation
8+
9+
public class RecursiveBinarySearchTest {
10+
11+
@Test
12+
public void testBinarySearch() {
13+
// Create an instance of GenericBinarySearch
14+
RecursiveBinarySearch<Integer> searcher = new RecursiveBinarySearch<>();
15+
16+
// Test case 1: Element found in the array
17+
Integer[] arr1 = {1, 2, 3, 4, 5};
18+
int target1 = 3;
19+
int result1 = searcher.binsear(arr1, 0, arr1.length - 1, target1);
20+
assertEquals(2, result1);
21+
22+
// Test case 2: Element not found in the array
23+
Integer[] arr2 = {1, 2, 3, 4, 5};
24+
int target2 = 6;
25+
int result2 = searcher.binsear(arr2, 0, arr2.length - 1, target2);
26+
assertEquals(-1, result2);
27+
28+
// Test case 3: Element found at the beginning of the array
29+
Integer[] arr3 = {10, 20, 30, 40, 50};
30+
int target3 = 10;
31+
int result3 = searcher.binsear(arr3, 0, arr3.length - 1, target3);
32+
assertEquals(0, result3);
33+
34+
// Test case 4: Element found at the end of the array
35+
Integer[] arr4 = {10, 20, 30, 40, 50};
36+
int target4 = 50;
37+
int result4 = searcher.binsear(arr4, 0, arr4.length - 1, target4);
38+
assertEquals(4, result4);
39+
}
40+
}

0 commit comments

Comments
 (0)