-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaskFive.java
More file actions
29 lines (25 loc) · 785 Bytes
/
TaskFive.java
File metadata and controls
29 lines (25 loc) · 785 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
26
27
28
29
import java.util.Scanner;
public class TaskFive {
public static int binarySearch(int[] number, int key){
int start = 0;
int end = number.length - 1;
while (start <= end) {
int mid = (start + end) / 2;
if (key == number[mid]) {
return mid;
}
if (key < number[mid]) {
end = mid - 1;
} else {
start = mid + 1;
}
}
return -1;
}
public static void main(String args[]){
Scanner input= new Scanner(System.in);
int[]number={2,5,8,3,4,6,10,15,20,1,11};
int key = input.nextInt();
System.out.println(key+" is found at index: "+binarySearch(number, key));
}
}