-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinarySearch.java
44 lines (38 loc) · 1.2 KB
/
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
39
40
41
42
43
44
package array;
class BinarySearch {
/**
* Main method of the class for the following question:
* Find a target of elements of a given array using binary search.
* */
public static void main(String[] args) {
/* Example:
* int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
* int target = 5;
*/
java.util.Scanner scanner = new java.util.Scanner(System.in);
System.out.print("Enter length of array: ");
int length = scanner.nextInt();
int[] arr = new int[length];
for (int i = 0; i < length; i++) {
arr[i] = scanner.nextInt();
}
System.out.print("Enter target: ");
int target = scanner.nextInt();
System.out.println(binarySearch(arr, target));
}
public static int binarySearch(int[] arr, int target) {
int start = 0;
int end = arr.length - 1;
while (start <= end) {
int mid = start + (end - start) / 2;
if (arr[mid] == target) {
return mid;
} else if (arr[mid] < target) {
start = mid + 1;
} else {
end = mid - 1;
}
}
return -1;
}
}