Java Program to implement linear search


April 27, 2022, Learn eTutorial
1115

Here we are explaining how to write a java program to perform a linear search.

What is linear search in java?

Linear search is also known as sequential search. It is a simple searching method. Its performance is very slow compared to other searching methods. In linear search, the searching is performed on entire the list. Its checks with all the elements in the list sequentially. It checks each element of the list with the search element until the element is found or it reaches the end of the list.

How to implement the java program to perform a linear search?

First, we have to declare the class LinearSearch.Declare the integer variables i, limit, key. Create an object of the scanner class as sc. Read the limit of the array elements as a limit. Declare an integer array of size limit. Read the array elements using a for loop into array[i]. Read the search element into the key. Then by using for loop check each element of the array is equal to the key elements using the if condition. If array[i]=key, then display the element is found at the position i+1 and break from the loop. Otherwise, increment i by one and continue the process until all elements are checked. If i reach to limit then display the element is not found.

 

ALGORITHM

STEP 1: Declare the class LinearSearch with a public modifier.

STEP 2: Open the main() to start the program, Java program execution starts with the main()

STEP 3: Declare the integer variables i, limit, key.

STEP 4: Read the limit of the array into the variable limit.

STEP 5: Declare an array of size limit.

STEP 6: Read the elements into the array by using for loop.

STEP 7: Read the element which is to be searched into the variable key.

STEP 8: By using a for loop with the condition i

STEP 9: Check if array[i]==key, if true then display the element is found at the position i+1 and go to step 11.

STEP 10:Incremnet by one and repeat step 9.

STEP 11:Check if i=limit then display the element is not found.

 

Java Source Code

                                          import java.util.Scanner;  
   
public class LinearSearch   
{  
  public static void main(String args[])  
  {  
    int i, limit, key;  
   
    Scanner sc = new Scanner(System.in);  
    System.out.println("Enter the limit of numbers:");  
    limit = sc.nextInt();   
     int array[] = new int[limit];
   
    System.out.println("Enter " +limit + " numbers:");  
   
    for (i = 0; i < limit; i++)  
      array[i] = sc.nextInt();  
   
    System.out.println("Enter the search element:");  
    key = sc.nextInt();  
   
    for (i = 0; i < limit; i++)  
    {  
      if (array[i] == key)      
      {  
         System.out.println("The search element "+ key +" is found at location "+ (i+1));
          break;  
      }  
   }  
   if (i == limit)  
      System.out.println("The search element "+ key +" is not found");
  }  
}  
                                      

OUTPUT

Enter the limit of numbers:5
Enter 5 numbers:
2 
6
88
10
120
Enter the search element:88
The search element 88 is found at location 3