Linear Search in Java - Linear Search

Linear Search in  Java

Linear Search using Java

  • A linear or sequential search is a simple algorithm. 
  • The time complexity of linear search is O (n). 
  • It is very slow as it searches n-1.

In this search algorithm, an array list will be searched one by one from the beginning until the required element is found. If the required element is found in the array then the search is successful otherwise unsuccessful. 

Linear search in java with Numeric data

import java.util.*;

public class LinearSearch

{

    public static void main(String args[])

    {

        Scanner sc=new Scanner(System.in);

        int n[]=new int[10];

        int s, i, f=0;

        System.out.println("Enter 10 number in Array:");

        for(i=0; i<10; i++)

        {

            n[i]=sc.nextInt();

        }

        System.out.println("Enter number to Search:");

        s=sc.nextInt();

        for(i=0; i<10; i++)

        {

            if(n[i]==s)

            {

                f=1;

                break;

            }

        }

        if(f==1)

        {

            System.out.println("Search Successful");

        }

        else

        {

            System.out.println("Search Unsuccessful");

        }

    }

}

Output

Enter 10 number in Array:

30

9

6

4

3

12

44

55

64

3

Enter number to Search:

12

Search Successful

 Linear search in java with String data

 import java.util.*;

public class LinearSearchWithString

{

    public static void main(String args[])

    {

        Scanner sc=new Scanner(System.in);

        String n[]=new String[10];

        String s; // For Search

        int i, f=0;

        System.out.println("Enter 10 number in Array:");

        for(i=0; i<10; i++)

        {

            n[i]=sc.nextLine();

        }

        System.out.println("Enter number to Search:");

        s=sc.nextLine();

        // search the element

        for(i=0; i<10; i++)

        {

            if(n[i].compareTo(s)==0)

            {

                f=1;

                break;

            }

        }

        // if f is 1 then search successful

        if(f==1)

        {

            System.out.println("Search Successful");

        }

        else

        {

            System.out.println("Search Unsuccessful");

        }

    }

}

Output

Enter 10 Name in Array:

Sachin

Tania

Wasim

Yasmin

Kalim

Ummesh

Salim

Dannial

Lalit

Rehan

Enter Name to Search:

Ummesh

Search Successful

Linear search in java with Recursion technique

 public class LinearSearchWithRecursion

{

    // Recursive Method to search

    int linearSearchRecursive (int array[], int init, int length, int search)

    {

        if (length < init)

            return -1;

        if (array[init] == search)

            return init;

        if (array[length] == search)

            return length;

        return linearSearchRecursive(array, init + 1, length-1, search);

    }

    //

    public static void main(String args[])

    {

        int array[] = {4, 6, 7, 8, 9, 5};

        LinearSearchWithRecursion ob = new LinearSearchWithRecursion();

        int search, p ,i;

        search = 9;

        System.out.println("Original Data");

        for(i=0; i<array.length; i++)

        {

            System.out.print(array[i] + " ");

        }

        p = ob.linearSearchRecursive (array,  0,  array.length-1,  search);

        if (p != -1)

           System.out.println("\nSearch successful and " + search  + " is found at " + p);

        else

            System.out.println("\nSearch is unsuccesful " + search + " is not in the array");

    }

}

Output

Original Data

4 6 7 8 9 5

Search successful and 9 is found at 4


More Tutorial and Programs

SHARE THIS
Previous Post
Next Post