-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearch.java
More file actions
43 lines (35 loc) · 1.48 KB
/
Copy pathBinarySearch.java
File metadata and controls
43 lines (35 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
public class BinarySearch {
// Main binary search function that matches the required signature
public int binarySearch(int[] arr, int k) {
// Call the recursive helper function with initial bounds
return binarySearchHelper(arr, 0, arr.length - 1, k);
}
// Recursive helper function
private int binarySearchHelper(int[] arr, int left, int right, int target) {
// Base case: if bounds cross, element not found
if (left > right) {
return -1;
}
// Calculate middle index
int mid = left + (right - left) / 2;
// If element found at mid, return its index
if (arr[mid] == target) {
return mid;
}
// If target is less than middle element, search left half
if (target < arr[mid]) {
return binarySearchHelper(arr, left, mid - 1, target);
}
// If target is greater than middle element, search right half
return binarySearchHelper(arr, mid + 1, right, target);
}
// Main method for testing
public static void main(String[] args) {
BinarySearch bs = new BinarySearch();
// Test case from assignment
int[] arr = {12, 45, 67, 89};
// Test cases
System.out.println("Searching for 67: " + bs.binarySearch(arr, 67)); // Should print 2
System.out.println("Searching for 31: " + bs.binarySearch(arr, 31)); // Should print -1
}
}