Linear Search in Java with Multiple Values

 

linear search in java with multiple value
linear search in Java with multiple values

Linear search in Java

Linear search in Java: Java program to search name and print name and its corresponding other value by Linear Search

Linear search is a simple searching algorithm used to find a target element within a list or array by sequentially checking each element until a match is found. This is a very simple search algorithm. It can be inefficient for large datasets.

Linear search in Java

Here's an example of linear search in Java:


java

Copy code

class LinearSearch {

    public static int search(int[] arr, int n, int x) {

        for (int i = 0; i < n; i++) {

            if (arr[i] == x)

                return i;

        }

        return -1;

    }


    public static void main(String[] args) {

        int[] arr = { 3, 4, 1, 7, 5 };

        int n = arr.length;

        int x = 4;

        int index = search(arr, n, x);

        if (index == -1)

            System.out.println("Element is not present in the array");

        else

            System.out.println("Element found at position " + index);

    }

}

In the above example, we have a method called a search that takes an array arr, its length n, and the element to be searched x. It will check each element of the array. If the element is equal to the target element then the match is found. It will return the index position of the element otherwise, it will return -1 mean false. This indicates that the element was not found.

In the main method, we create an array and perform a linear search for element 4. If the target element is found it will print the position of the element. If it is not it will print a message the element is not present in the array.

The time complexity of the linear search is O(n). 'n' indicates the number of elements in the array. The best-case time complexity occurs when the target element is found at the first location, resulting in O(1) time complexity. The worst-case time complexity occurs when the target element is not present in the array or found at the last location, resulting in O(n) time complexity. The space complexity of the linear search is O(1) since it does not require any additional space.

In the following program, we have taken three arrays as name[], city[], and run[] with some given values. We need to find out a given value within the name array. And if found then print the name with the corresponding city and run.

import java.util.*;

// Linear Search

public class Array1

{

    public static void main(String args[])

    {

       Scanner sc=new Scanner(System.in);      

       // name array in String

        String name[]={"Rohit", "Kholi", "Dhoni", "Sachin", "Rahul", "Siraj", "Bumrah", "Shami"};

        // City array in String

        String city[]={"Mumbai", "Delhi", "Rachi", "Mumbai", "Bangalore", "Hyderabad", "Mumbai", "Patna"};

       //  run array in int

        int run[]={88, 50, 60, 54, 70, 5, 10, 6};

        String s;        // search variable

        int I;               // for loop

        int f=0;          // flag variable

        int pos=-1;  // Position of the index

        int l=name.length;

        // Printing of name

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

        {

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

        }

         System.out.println("\nEnter a name to search");

        s=sc.nextLine();

        System.out.println("Output");

       // linear searching logic

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

        {

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

            {

                f=1;

                pos=i;

                break;

            }

        }

        if(f == 1)

        {

            System.out.println(s + " made " + run[pos] + " and live in " + city[pos]);

        }

        else

        {

            System.out.println("Not Found");

        }

    }      

}

output

Rohit Kholi Dhoni Sachin Rahul Siraj Bumrah Shami 

Enter a name to search

Dhoni

Output

Dhoni made 60 and lives in Rachi


More programs 



SHARE THIS
Previous Post
Next Post