import java.io.*;
class binarysearch
{
public static void main(String arg[]) throws Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("How many elements");
int n=Integer.parseInt(br.readLine());
int a[]=new int[n];
System.out.println("Enter the array elements");
for(int i=0;i<n;i++)
{
a[i]=Integer.parseInt(br.readLine());
}
int mid,top,bott;
System.out.println("Enter the key elements");
int d=Integer.parseInt(br.readLine());
top=0;
bott=n-1;
while(top<=bott)
{
mid=(top+bott)/2;
if(d==a[mid])
{
System.out.println("Element found at"+(mid+1));
top++;
}
else
if(d<a[mid])
bott=mid-1;
else
top=mid+1;
}
}
}
To run this follow steps in previews post:
class binarysearch
{
public static void main(String arg[]) throws Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("How many elements");
int n=Integer.parseInt(br.readLine());
int a[]=new int[n];
System.out.println("Enter the array elements");
for(int i=0;i<n;i++)
{
a[i]=Integer.parseInt(br.readLine());
}
int mid,top,bott;
System.out.println("Enter the key elements");
int d=Integer.parseInt(br.readLine());
top=0;
bott=n-1;
while(top<=bott)
{
mid=(top+bott)/2;
if(d==a[mid])
{
System.out.println("Element found at"+(mid+1));
top++;
}
else
if(d<a[mid])
bott=mid-1;
else
top=mid+1;
}
}
}
To run this follow steps in previews post:
0 comments:
Post a Comment