-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathBinarySearch.java
More file actions
40 lines (36 loc) · 882 Bytes
/
BinarySearch.java
File metadata and controls
40 lines (36 loc) · 882 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
34
35
36
37
38
39
40
import java.util.Scanner;
class BinarySearch{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
System.out.print("Enter how many numbers: ");
int n=sc.nextInt();
int i;
int a[]=new int[n];
//INPUT
System.out.println("Enter numbers in increasing order");
for(i=0;i<n;i++){
System.out.print("Enter element " + (i+1) + ":");
a[i]=sc.nextInt();
}
System.out.print("Enter value to search: ");
int item=sc.nextInt();
//SEARCHING
int low=0, high=n-1, mid=0;
boolean found=false;
while(low<=high){
mid=(low+high)/2;
if(a[mid]==item){
found=true;
break;
}
else if(item>a[mid])
low=mid+1;
else
high=mid-1;
}
if(found==true)
System.out.println("Number is found at pos " + (mid+1));
else
System.out.println("Number not found!!");
}
}