Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

Linear Search Program in Java

In this tutorial, we’ll discuss the linear search method. Searching in the list is the process of locating a specific element. If the element is present in the list, the operation is deemed successful and returns the element’s location; otherwise, the search is considered unsuccessful.

Binary search and linear search are two most frequently used search techniques. Consequently, we will discuss the widely used linear search technique in this article..

Another name for a linear search algorithm is a sequential search method. It is the search algorithm that is easiest to understand. A linear search simply involves going over the entire list and matching each element with the item whose position has to be determined. If a match is found, the algorithm returns the item’s location; if not, it returns NULL.

It is typically used to look for a specific element in an unsorted or unordered list of items. The linear search’s worst-case time complexity is O(n).

The processes involved in implementing linear search are as follows:

  • First, we must use a for loop to iterate through the members of the array.
  • Compare the search element with the current array element throughout each iteration of the for loop, and if the elements match, return the index of the matching array element.
  • Move on to the next element if the first one does not match.
  • Return -1 if there is no match or if the search element is missing from the array.

Now, let’s see the algorithm of linear search.

Linear Search Algorithm:

let’s say the array is X and the value is i;

  • Set j to 1
  • If j > n, jump to step 7
  • If X[j] == i, jump to step 6
  • Then, increment j by 1 i.e. j = j+1
  • Go back to step 2
  • Display the element i which is found at particular index i, then jump to step 8
  • Display element not found in the set of input elements.
  • Exit/End

How does the Linear Search Algorithm Work?

To find an element in the list below with the value k = 1, follow these steps.

Array to be searched for

  1. Compare each element x with k starting with the first element.

  2. Return the index if x == k.

  1. Else, return not found.

Java Program for Linear search

import java.util.*;
import java.lang.*;
class LinearSearch{    
public static int linearSearch(int[] arr, int key){    
        for(int i=0;i<arr.length;i++){    
            if(arr[i] == key){    
                return i;    
        return -1;    
    public static void main(String a[]){    
        int[] a1= {10,20,30,50,70,90};    
        int key = 70;    
        System.out.println(key+" is found at index: "+linearSearch(a1, key));    
Output: 70 is found at index: 4

Linear search complexities:

Time complexity: O(N) will be the time complexity for performing linear search.

Space complexity: O(1) will be the space complexity for performing linear search.

With this, we come to the end of this blog on ‘Linear Search in Java’. Performing linear search will help in building the logic.

Other Java Programs

Java Program to Add Two Numbers
Java Program to Check Prime Number
Java Program to Check Whether a Number is a Palindrome or Not
Java Program to Find the Factorial of a Number
Java Program to Reverse a Number
Java Program to search an element in a Linked List
Program to convert ArrayList to LinkedList in Java
Java Program to Reverse a linked list
Java Program to search an element in a Linked List
Anagram Program in Java
Inheritance Program in Java
Even Odd Program in Java
Hello World Program in Java
If else Program in Java
Binary Search Program in Java

Leave a Reply

Your email address will not be published. Required fields are marked *