-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearch.java
More file actions
37 lines (30 loc) · 1.02 KB
/
BinarySearch.java
File metadata and controls
37 lines (30 loc) · 1.02 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
public class BinarySearch {
static int binarySearch (int[] array, int target){
int left = 0, right = array.length -1;
while(left <= right) {
//calculate mid point
int mid = left + (right - left) / 2;
//check if the value we are looking for is at the middle
if (array[mid] == target){
return mid;
}
//check if our target is greater than the mid if it is then ignore the left half
if (array[mid] < target){
left = mid + 1;
} else {
right = mid - 1;
}
}
return -1;//target is not present
}
public static void main (String[] args) {
int[] sortedArray = {2,5,8,23,35,45,60,78,89};
int target = 60;
int result = binarySearch(sortedArray , target);
if (result == -1){
System.out.println("Element not found");
} else {
System.out.println("Element found at index " + result);
}
}
}