-
Notifications
You must be signed in to change notification settings - Fork 0
/
binarySearch.java
38 lines (32 loc) · 1002 Bytes
/
binarySearch.java
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
public class binarySearch {
public static int binarySearch(int arr[], int x) {
int low = 0, high = arr.length - 1;
while (low <= high) {
int mid = low + (high - low) / 2;
// Check if x is present at mid
if (arr[mid] == x) {
return mid;
}
// If x is greater, ignore the left half
if (arr[mid] < x) {
low = mid + 1;
}
// If x is smaller, ignore the right half
else {
high = mid - 1;
}
}
// If x is not present in array
return -1;
}
public static void main(String args[]) {
int arr[] = {2, 3, 4, 10, 40};
int x = 10;
int result = binarySearch(arr, x);
if (result == -1) {
System.out.println("Element is not present in array");
} else {
System.out.println("Element is present at index " + result);
}
}
}