Monday, 2 February 2015

LINEAR SEARCH


1) write java programs that use non-recursive function for implementing Linear Search  method?

import java.util.Scanner;
class Linear
{
public static void main(String ar[])
{
int i,n,item,a[];
Scanner sc=new Scanner(System.in);
System.out.println("enter the size of the array");
n=sc.nextInt();
a=new int[n];
System.out.println("enter " + n + "  values");
for(i=0;i<n;i++)
a[i]=sc.nextInt();
System.out.println("enter element u want to search");
item=sc.nextInt();
for(i=0;i<n;i++)
{
if(a[i]==item)
{
System.out.println(item + "   found at index    " + i);
break;
}
}
if(i==n)
{
System.out.println("element not found");
}
}
}

No comments:

Post a Comment