-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearch.java
More file actions
33 lines (31 loc) · 842 Bytes
/
BinarySearch.java
File metadata and controls
33 lines (31 loc) · 842 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
30
31
32
33
import java.util.Scanner;
public class BinarySearch {
public static boolean bSearch(int[] arr, int n , int k){
int low = 0;
int high = n-1;
while (low<=high) {
int mid = low + ((high-low)/2);
if (arr[mid]==k) {
return true;
}
if (arr[mid]<k) {
low = mid + 1;
}
if (arr[mid]>k) {
high = mid - 1;
}
}
return false;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i]=sc.nextInt();
}
int k = sc.nextInt();
System.out.println(bSearch(arr,n,k));
sc.close();
}
}