-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathbinary_search.java
More file actions
43 lines (40 loc) · 1.57 KB
/
Copy pathbinary_search.java
File metadata and controls
43 lines (40 loc) · 1.57 KB
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
import java.util.*;
class BinarySearch {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//Example array...
int arr[] = {5, 10, 30, 37, 99, 239, 274, 590, 874, 1023};
//asking the user to provide the number of queries to do in the above array.
System.out.print("Enter number of queries: ");
int q = sc.nextInt();
for(int i = 0; i < q; ++i) {
//element input by user
System.out.print("Enter number to search: ");
int x = sc.nextInt();
//checking wheter it is present in the array-arr
if(binarySearch(arr, x)) {
System.out.println(x + " is present in the array.");
}else {
System.out.println(x + " is not present in the array.");
}
}
}
//funtion to search any element present in the given array.
public static boolean binarySearch(int arr[], int x) {
//two pointers left and right which would come closer to the element to find.
int left = 0, right = arr.length - 1, middle;
while(left <= right) {
//to check only the middle indexed element in the given array.
middle = (left + right) / 2;
if(arr[middle] == x) {
return true;
}else if(arr[middle] > x) {
right = middle - 1;
}else {
left = middle + 1;
}
}
//return false finally when there is no element x present in the given array.
return false;
}
}