-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary.java
More file actions
40 lines (34 loc) · 1.15 KB
/
binary.java
File metadata and controls
40 lines (34 loc) · 1.15 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
public class BinarySearch {
/**
* Standard iterative Binary Search
* @param arr - The sorted array to search through
* @param target - The value to find
* @return The index of target if found, otherwise -1
*/
public static int search(int[] arr, int target) {
int low = 0;
int high = arr.length - 1;
while (low <= high) {
// Using low + (high - low) / 2 to avoid integer overflow
int mid = low + (high - low) / 2;
if (arr[mid] == target) {
return mid; // Target found
} else if (arr[mid] < target) {
low = mid + 1; // Search right half
} else {
high = mid - 1; // Search left half
}
}
return -1; // Target not present
}
public static void main(String[] args) {
int[] data = {1, 3, 5, 7, 9, 11};
int target = 7;
int result = search(data, target);
if (result != -1) {
System.out.println("Element found at index: " + result);
} else {
System.out.println("Element not found.");
}
}
}