-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathARRAY4.java
More file actions
25 lines (22 loc) · 715 Bytes
/
Copy pathARRAY4.java
File metadata and controls
25 lines (22 loc) · 715 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
public class ARRAY4 {
public static int BinarySearch(int arr[], int target) {
int start = 0;
int end = arr.length - 1;
while (start <= end) {
int mid = (start + end) / 2;
if (target > arr[mid]) {
start = mid + 1; // WORKS ONLY FOR SORTED ARRAYS !!!
} else if (target < arr[mid]) {
end = mid - 1;
} else {
return mid;
}
}
return -1;
}
public static void main(String[] args) {
// BINARY SEARCH
int arr[] = {1, 2, 3, 65, 90, 100};
System.out.println("TARGET FOUND AT: " + BinarySearch(arr, 100));
}
}