public class Search
{
/**
The method searches the key in the array passed to it.If the key is
found the position of the key is returned otherwise -1 is returned.
*/
public static int linearSearch(double[] data,double key)
{
int position=-1;
for(int i=0;i<data.length;i++)
{
if(data[i]==key)
{
position=i;
break;
}
}
return position;
}
public static void main(String args[])
{
//calling linerSearch method
double unsortedData[]={5,13,18,4,1,27,50,63.35,100,59};
key=100;
position=Search.linearSearch(unsortedData,key);
if(position>=0)
{
System.out.println(key+" is found at position "+position);
}
else
{
System.out.println(key+" is not found in the array");
}
}
}
You can download this code here.
0 comments:
Post a Comment