Java Linear Search : search one element in an array

Java Program to do linear search for an element in an array :

In this tutorial, we will learn how to do a linear search on elements of an array . Linear search means we will search for an element one by one. For example for the array [1,2,3,4,5,6,7], if you want to search for any number , we will search one by one element and compare it with the element to be searched i.e. 1,2,3,4…

Following algorithm we are using in the program :

  1. Ask the user how many numbers he/she is going to add in the array. Suppose it is ‘n’ .

  2. Create one array of ‘n’ elements.

  3. Get the input from the user for each element of the array using a ‘for’ loop.

  4. Ask the user which element need to be searched.

  5. Scan each element in the array one by one to find out the element.

  6. If it is found, print the index of this element in the array.

  7. Otherwise, if the scan is completed, print as ‘not found’.

import java.util.Scanner;

public class Main {

    /**
     * Utility functions
     */
    static void println(String string) {
        System.out.println(string);
    }

    static void print(String string) {
        System.out.print(string);
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        println("********** Linear Search **********");

        //variable to store total elements count
        int total = 0;

        //get the total count
        println("How many numbers you want to add ?");
        total = sc.nextInt();

        //create one array to store the numbers
        int[] arr = new int[total];
        
        //get input of all the numbers and store it in the array
        for (int i = 0; i < total; i++) {
            print("Enter no for index " + i + " : ");
            arr[i] = sc.nextInt();
        }

        //enter the number to be searched
        println("Enter a number you want to search for : ");
        int searchNo = sc.nextInt();

        //do the linear search by iterating through each element one by one
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == searchNo) {
                println(searchNo + " is found on index " + i);
                break;
            } else if (i == arr.length - 1) {
                println(searchNo + " is not found anywhere !!");
            }
        }


    }
}

Sample Output :

********** Linear Search **********
How many numbers you want to add ?
10
Enter no for index 0 : 1
Enter no for index 1 : 2
Enter no for index 2 : 3
Enter no for index 3 : 4
Enter no for index 4 : 5
Enter no for index 5 : 6
Enter no for index 6 : 7
Enter no for index 7 : 8
Enter no for index 8 : 9
Enter no for index 9 : 12
Enter a number you want to search for : 
9
9 is found on index 8

Sample Output if no number is found :

********** Linear Search **********
How many numbers you want to add ?
5
Enter no for index 0 : 1
Enter no for index 1 : 2
Enter no for index 2 : 3
Enter no for index 3 : 4
Enter no for index 4 : 5
Enter a number you want to search for : 
6
6 is not found anywhere !!

Similar tutorials :