import java.util.Scanner;
class BinarySearch
{
int a[]=new int[10];
Scanner sc=new Scanner(System.in);
int n,mid,key;
void accept()
{
System.out.println("Enter no of elements in array");
n=sc.nextInt();
for(int i=0;i<n;i++)
{
System.out.print("A["+i+"]= ");
a[i]=sc.nextInt();
}
}
void display()
{
System.out.println("\nArray Elements are: ");
for(int i=0;i<n;i++)
{
System.out.println("A["+i+"]= "+a[i]);
}
System.out.println("\nAfter Sorting Array : ");
int temp;
for(int x=0; x<n; x++)
{
for(int i=0;i<n-1;i++)
{
if(a[i]>a[i+1])
{
temp=a[i];
a[i]=a[i+1];
a[i+1]=temp;
}
}
}
for(int i=0;i<n;i++)
{
System.out.println("A["+i+"]= "+a[i]);
}
System.out.print("\nEnter Element to be Search : ");
key=sc.nextInt();
int result=binarySearch(a,key,0,n-1);
System.out.println("Element "+key+" is Found at location a["+result+"]");
}
int binarySearch(int[] a,int key,int l,int h)
{
mid=(l+h)/2;
if(a[mid]==key)
return mid;
if(a[mid]<key)
{
l=mid+1;
binarySearch(a, key, l, h);
}
if(a[mid]>key)
{
h=mid-1;
binarySearch(a, key, l, h);
}
return mid;
}
}
public class Solution {
public static void main(String args[])
{
BinarySearch b=new BinarySearch();
b.accept();
b.display();
}
}