forked from narnolii/java-DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_search.java
More file actions
37 lines (34 loc) · 848 Bytes
/
binary_search.java
File metadata and controls
37 lines (34 loc) · 848 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
import java.util.Scanner;
public class binary_search {
public static void main(String ad[]){
int a[] = {2,3,4,56,6};
int mid ;
int first;
int last;
first=0;
last= (a.length-1);
int k=0;
Scanner b = new Scanner(System.in);
System.out.print("enter a number = ");
int num = b.nextInt();
while(first<=last){
mid = (first+last)/2;
if(num==a[mid]){
k=1;
break;
}
else if(num<a[mid]){
last = mid
}
else if(num>a[mid]){
first = mid+1;
}
}
if(k==1){
System.out.print("number is found");
}
else{
System.out.print("number not found.");
}
}
}