To search using Binary Search


/*Write a program in java to accept 10 different number in a single Dimensional array .enter a number and using Binary search technique,check whether the number is present or not in the array.If the number is present then display the message “Search Successful”otherwise “The is not Successful”*/

 

import java.io.*;

public class binsearch

{

public static void main(String arg[])

{

InputStreamReader r1=new InputStreamReader(System.in);

BufferedReader in=new BufferedReader(r1);

int i,j,k,l,m,n,p;

n=0;

j=0;

k=9;

l=0;

for(i=0;i<10;i++)

{

System.out.println(“Enter the elements”+(i+1)+”:”);

M[i]=Integer.parseInt(in.readLine());

While( j<=k)

{

l=(j+k)/2

if(m[p]<n)

j=l+1;

if(m[l]>n)

k=l-1;

if(m[l]==n)

{

p=1;

break;

}

}

if(p==1)

System.out.println(“The search is Sucessful”);

Else

System.out.println(“The search is not Sucessful”);

}}

Related Posts

Comments are closed.