Menu



Java program for Linear Search


Linear search is a very simple search algorithm.

It is used to search a target element from multiple elements.

In Linear search the search goes sequentially and check one by one.After chcek all item if a match found then it returned otherwise the search continue till the end.

linear_search.java

public class LinearSearch{
public static int linearSearch(int[] arr, int key){
for(int i=0;i if(arr[i] == key){
return i;
}
}
return -1;
}
public static void main(String a[]){
int[] a1= {10,20,30,40,60,70,80,90};
int key = 40;
System.out.println(key+" is found at index: "+linearSearch(a1, key));
}