-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearch.java
More file actions
36 lines (32 loc) · 1.11 KB
/
Copy pathBinarySearch.java
File metadata and controls
36 lines (32 loc) · 1.11 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
public class BinarySearch {
// Method to perform binary search on a sorted array
public static int binarySearch(int[] arr, int target) {
int left = 0;
int right = arr.length - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
// Check if the target is present at mid
if (arr[mid] == target) {
return mid; // Target found
}
// If target is greater, ignore left half
if (arr[mid] < target) {
left = mid + 1;
} else {
// If target is smaller, ignore right half
right = mid - 1;
}
}
return -1; // Target not found
}
public static void main(String[] args) {
int[] arr = {2, 3, 4, 10, 40}; // Sorted array
int target = 10; // Target to be searched
int result = binarySearch(arr, target);
if (result == -1) {
System.out.println("Element not found in the array.");
} else {
System.out.println("Element found at index: " + result);
}
}
}