SEARCHING
1.) Sequential Search(Linear Searching)
class seqSearch
{
puclic static void main (string args[]){
int x = Integer.parseInt(args[0]);
int a[x] = { };
int i, pas = -1;
for(i=0; i<a.length; i++)
{
if(a[i] == x)
{
pas = i;
break;
}
}
if(pas == -1)
system.out.println("value not found");
else
system.out.println("value found at index:-" +pas);
}
}
2.) Binary Search :--
class seqSearch
{
puclic static void main (string args[]){
int x = Integer.parseInt(args[0]);
int a[x] = { };
while(first<=last)
{
mid = (first+last)/2;
if( x > a[mid])
first = mid +1;
else if(x<a[a])
last= mid-1;
else
{
pass= mid;
break;
}
}
No comments:
Post a Comment