Linear search is used for searching an item in a list of items,such as in Array.It is not efficient when compared to other ways of searching item in an array.
public static int LinearSearch(int[] A,int val)
{
for(int i=0;i<A.Length;i++)
{
if(A[i]==val)
return i;
}
return-1;
}
We can sort items in the array before calling linear search function to improve performance
Array.Sort(vals);
Leave a Reply