-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSearchBinary.java
42 lines (40 loc) · 1.01 KB
/
SearchBinary.java
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
/*
* Alt + Shift + F to indent lines.
*/
package DataStructure;
import java.util.*;
/**
*
* @author Vinayak
*/
public class SearchBinary {
public static void main(String[] args) {
Scanner s1=new Scanner(System.in);
System.out.println("Enter size");
int n=s1.nextInt();
System.out.println("Enter array");
int arr[]=new int[n];
for(int i=0;i<n;i++)
arr[i]=s1.nextInt();
Arrays.sort(arr);
System.out.println("Enter no to search");
int d=s1.nextInt();
if(search(arr,d)<0)
System.out.println("Not found");
else
System.out.println("Found at "+search(arr,d));
}
static int search(int arr[],int d){
int l=0,h=arr.length-1,mid=0;
while(l<=h){ //l<arr.length-1 && h>0
mid=(l+h)/2;
if(arr[mid]==d)
return mid;
if(arr[mid]<d)
l=mid+1;
else
h=mid-1;
}
return -1;
}
}