-
Notifications
You must be signed in to change notification settings - Fork 56
/
binarySearch.java
42 lines (27 loc) · 933 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
39
40
import java.util.Scanner;
public class binarySearch {
public static int binarySearchFunction(int numbers[],int key){
int start = 0, end = numbers.length -1;
while(start <= end){
int mid = (start + end) / 2;
// comparisons
if(numbers[mid] == key){ // element found
return mid;
}
if(numbers[mid] < key){ // element is on right side
start = mid + 1;
} else { // element is on left side
end = mid - 1;
}
}
return -1;
}
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
// prerequisite -> sorted arrays
int numbers[] = {2,4,6,8,10,12,14};
int key = 14;
System.out.println("Index for key is : " + binarySearchFunction(numbers, key));
sc.close();
}
}