-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathA5BinSearch.java
More file actions
31 lines (31 loc) · 879 Bytes
/
A5BinSearch.java
File metadata and controls
31 lines (31 loc) · 879 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
package com.company;
import java.util.Scanner;
public class A5BinSearch {
public static void main(String[] args){
Scanner s=new Scanner(System.in);
int arr[]={1,2,3,4,5,6,7,8,9,10,11};
System.out.print("Enter any number to search: ");
int x=s.nextInt();
int l=arr.length;
int up=l-1,low=0;
int middle,k=-1;
while(up>=low){
middle=(up+low)/2;
if(x>arr[middle]){
low=middle+1;
}
else if(x<arr[middle]){
up=middle-1;
}
else{
k=middle;
break;
}
}
if(k!=-1)
System.out.println("Yes,"+x+" is present in the array");
else{
System.out.println(x+" is not present in the array");
}
}
}