-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearch.java
More file actions
27 lines (26 loc) · 852 Bytes
/
BinarySearch.java
File metadata and controls
27 lines (26 loc) · 852 Bytes
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
// 13. Write a program to perform binary search on a sorted array.
public class BinarySearch {
public static void main(String[] args) {
int[] arr = { 2, 4, 6, 8, 10, 12, 14, 16 };
int target = 10;
int left = 0;
int right = arr.length - 1;
int result = -1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == target) {
result = mid;
break;
} else if (arr[mid] < target) {
left = mid + 1;
} else {
right = mid - 1;
}
}
if (result != -1) {
System.out.println("Element found at index " + result);
} else {
System.out.println("Element not found in the array.");
}
}
}