QUESTION:-BINARY SEARCH
import java.util.*;
class binary
{
void main()
{int k=1;
Scanner sc=new Scanner(System.in);
System.out.println("ente the number to be search for");
int x=sc.nextInt();
int a[]=new int[10];boolean found=false;
for(int i=0;i<10;i++)
{
System.out.println("enter the "+k+" number ");
a[i]=sc.nextInt();
k=k+1;
}
int low=0;
int u=l-1;
int p=0;
while(low<=u)
{
int mid=(low+u)/2;
if(a[mid]==x)
{found=true;
p=mid+1;
break;
}
else
{
if(a[mid]<x)
{low=mid+1;
}
else
u=mid-1;
}
}
System.out.println("no to be found="+p);
if(found==true)
System.out.println("search sucessfull");
else
System.out.println("search unsecessfull");
}
}
import java.util.*;
class binary
{
void main()
{int k=1;
Scanner sc=new Scanner(System.in);
System.out.println("ente the number to be search for");
int x=sc.nextInt();
int a[]=new int[10];boolean found=false;
for(int i=0;i<10;i++)
{
System.out.println("enter the "+k+" number ");
a[i]=sc.nextInt();
k=k+1;
}
int low=0;
int u=l-1;
int p=0;
while(low<=u)
{
int mid=(low+u)/2;
if(a[mid]==x)
{found=true;
p=mid+1;
break;
}
else
{
if(a[mid]<x)
{low=mid+1;
}
else
u=mid-1;
}
}
System.out.println("no to be found="+p);
if(found==true)
System.out.println("search sucessfull");
else
System.out.println("search unsecessfull");
}
}
No comments:
Post a Comment
HAY THAKYOU FOE VISIT