Get Some Free Products at your Door Step. Just feed ur Address in dis Link...!!

Tuesday, October 11, 2011

Linear Search Program in Java

public class LinearSearch
{
public static void main(String[] args)
{
LinearSearch ds = new LinearSearch();
int[] data = {23, 12, 10, 14, 0 }; //getting data in array
System.out.println("Position of 12 is: "+ds.linearSearch(data, 12)); //sending array and no to be searched
}
public int linearSearch(int[] data, int no)
{
int pos=0;
for(;data[pos]!=no;pos++){}
return pos; //returning position of found no
}
}

No comments:

Post a Comment