Linear Search in Java

Introduction

 
In this article, we are going to discuss or describe Java linear searches. This is the simplest method of searching. In this method, the element to be searched is sequentially searched in the list. This method can be applied to a sorted or an unsorted list.
 

Linear Search (Sequential Search)

 
A sequential search of a list/array begins at the beginning of the list/array and continues until the item is found or the entire list/array has been searched. A linear search (aka Sequential Search) is the most fundamental and important of all algorithms. It is simple to understand and implement.
 
The following figure specifies how a linear search actually works.
 
Linear Search
 

Linear Search Algorithms

 
Step 1: Take a search element into key elements and start the search in the list.
  1. System.out.print("\nEnter Search Key : ");  
  2. System.out.flush();  
  3. try  
  4. {  
  5.     BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));  
  6.     str = obj.readLine();  
  7.     key = Integer.parseInt(str);  
  8. }  
Step 2: Match this key element to the starting of the array or list and increase one by one.
  1. for (int i = 0; i < size; i++{  
  2.         if (seaArr[i] == key)  
  3.             return (i + 1);  
  4.     }  
Step 3: When the key element matches the array or list index's element then return the index number.
 
Step 4: If the increment reaches the end of the link or array and a match was not found then return a message such as "Not found".
 
Example
  1. import java.io.*;  
  2. class search  
  3. {  
  4.     String str;  
  5.     int key, size, seaArr[];  
  6.     public void getdata()  
  7.     {  
  8.         System.out.print("Enter how many data you want to enter : ");  
  9.         System.out.flush();  
  10.         try  
  11.         {  
  12.             BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));  
  13.             str = obj.readLine();  
  14.             size = Integer.parseInt(str);  
  15.             seaArr = new int[size];  
  16.             for (int i = 0; i < size; i++)  
  17.             {  
  18.                 System.out.print("Enter element at " + (i + 1) + "th position  :  ");  
  19.                 System.out.flush();  
  20.                 str = obj.readLine();  
  21.                 seaArr[i] = Integer.parseInt(str);  
  22.             }  
  23.         } catch (Exception e)  
  24.         {  
  25.             System.out.println(e);  
  26.         }  
  27.     }  
  28.     public int LinSrch()  
  29.     {  
  30.         System.out.println("=====LINEAR SEARCH=====\n");  
  31.         getdata();  
  32.         System.out.print("\nEnter Search Key : ");  
  33.         System.out.flush();  
  34.         try  
  35.         {  
  36.             BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));  
  37.             str = obj.readLine();  
  38.             key = Integer.parseInt(str);  
  39.             for (int i = 0; i < size; i++)  
  40.             {  
  41.                 if (seaArr[i] == key)  
  42.                     return (i + 1);  
  43.             }  
  44.         } catch (Exception e)  
  45.         {  
  46.             System.out.println(e);  
  47.         }  
  48.         return (0);  
  49.     }  
  50. }  
  51. class Linear  
  52. {  
  53.     public static void main(String args[])  
  54.     {  
  55.         search o1 = new search();  
  56.         int result;  
  57.         result = o1.LinSrch();  
  58.         if (result == 0)  
  59.             System.out.println("\nSearch Not Found");  
  60.         else  
  61.             System.out.println("\nSearch is Located at " + result + " Position");  
  62.     }  
  63. }  
Output
 
Linear Search in Java
 
Resources


Similar Articles