Linear search in java

Let’s learn linear search in java.

Linear search in java

Linear search is very simple sequential search algorithm. It’s used to search key element in the given array. Here search starts from leftmost element of an array and key element is compared with every element in an array.

linear search in java

Search continues until the key element is found. If key element is found,  index position is returned, else, -1 is returned.

Linear search is rarely used because it is practically very slow compared to binary search and hashing. Let’s see program for linear search or linear search program using function.

// linear search program in java
public class LinearSearchExample
{
   // here function returns index of element x in arrLinear
   static int searchNumber(int[] arrLinear, int key)
   {
      int num = arrLinear.length;
      for(int a = 0; a < num; a++)
      {
         // here we are returning the index of the element if found
         if(arrLinear[a] == key)
            return a;
      }
      // here we are returning -1 if element is not found
      return -1;
   }
   public static void main(String[] args)
   {
      int[] arrLinear = {15, 25, 35, 55, 75, 95};
      int key = 55;
      int output = searchNumber(arrLinear, key);
      if(output == -1)
      {
         System.out.println("Sorry!!Element is not present");
      }
      else
      {
         System.out.println("Element is present at index " + output);
      }
   }
}


Output:

Element is present at index 3


What is time complexity of linear search?

In general we can say, if we have “n” elements in an array to search an element in an array, it will take O(1) in best case, average of O(n) and worst case of O(n).


Also read – garbage collection in java