Linear Search Algorithm : Data Structure

AnKiT KaMbOj
Sep 5, 2018 · 1 min read

LinearSearch Algorithm:

Linear search is a very simple search algorithm. In this type of search, a sequential search is made over all items one by one. Every item is checked and if a match is found then that particular item is returned, otherwise the search continues till the end of the data collection.

Java Program Example for Linear Search:

public class LinearSearchAlgorithm {

public static void main(String[] args) {
int number, sitem, counter, array[];

Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of elements in array");
number = sc.nextInt();
array = new int[number];

System.out.println("Enter "+ number + " array elements");
for(counter=0;counter<number;counter++){
array[counter]=sc.nextInt();
}
System.out.println("length of array is: "+array.length);
System.out.println("Enter element to searched in array");

sitem=sc.nextInt();
for(counter=0;counter<number;counter++){
if(array[counter]==sitem){
System.out.println("Search Element "+sitem+" is found in array at location "+(counter+1));
break;
}
}

if(counter==number){
System.out.println("Search Element "+sitem+ "is not in the list");
}
}

}
Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade