How to implement Linear Search in Java? Example tutorial

In the last article about searching and sorting, we have learned binary search algorithm and today I'll teach you another fundamental searching algorithm called Linear search. Linear search is nothing but iterating over the array and comparing each element with target element to see if they are equal since we search the array sequential from start to end, this is also known as sequential search or linear search. It is very slow as compared to binary search because you have to compare each element with every other element and definitely not suitable for a large array. It's practically useful only in case of the small array up to 10 to 15 numbers. In the worst case, you need to check all elements to confirm if target element exists in an array or not.

The time complexity of linear search algorithm is O(n) where n is the number of elements in the target array, which shows its slower than binary search algorithm, whose time complexity was O(logN) because it was dividing the array into two part in every iteration.

Actually the learning order is to first learn linear search and then the binary search but and we all learned that way but I found that when you first code binary search, then linear search becomes extremely easy and it also easier to reason about its time and space complexity and performance, hence I presented this algorithm after binary search.

Btw, if you enjoy learning algorithms and want to see the application of algorithms in real world, I would suggest to start reading Grokking Algorithms by Aditya Bhargava, one of the interesting book on algorithms with lots of easy to understand diagrams, visuals and real world explanation. I am reading this book now and it's truly interesting.

Java Program to implement Linear Search

Here is our program to implement linear search in Java.

import java.util.Scanner;

 * Java Program to implement binary search algorithm
 * using recursion

public class LinearSearchAlgorithm {

  public static void main(String[] args) {

    Scanner commandReader = new Scanner(;
        .println("Welcome to Java Program to perform linear search on int array");
    System.out.println("Enter length of array : ");
    int length = commandReader.nextInt();
    int[] input = new int[length];

    System.out.printf("Enter %d numbers %n", length);
    for (int i = 0; i < length; i++) {
      input[i] = commandReader.nextInt();

    System.out.println("Please enter target number");
    int target = commandReader.nextInt();

    int index = linearSearch(input, target);

    if (index == -1) {
      System.out.printf("Sorry, %d is not found in array %n", target);
    } else {
      System.out.printf("%d is found in array at index %d %n", target, index);



   * Java method to liner search an element in array
   * @param input
   * @param target
   * @return index of target element or -1 if not found
  public static int linearSearch(int[] input, int target) {

    for (int i = 0; i < input.length; i++) {
      if (input[i] == target) {
        return i;
    return -1;


Welcome to Java Program to perform linear search on int array
Enter length of array : 
Enter 7 numbers 
Please enter target number
4 is found in array at index 4 
Welcome to Java Program to perform linear search on int array
Enter length of array : 
Enter 3 numbers 
Please enter target number
22 is found in array at index

That's all about how to implement linear search in Java.

Other Searching and Sorting Algorithms tutorials you may like to explore
  • How to do quicksort algorithm in Java? (solution)
  • Write a program to implement bubble sort in Java? (solution)
  • How to implement insertion sort algorithm in Java? (answer)
  • How to sort an array in descending order in Java? (example)

Searching and Sorting Algorithms 

No comments:

Post a Comment