Linear search Program in java

import java.util.Scanner;

class LinearSearch 
{
    public static void main(String args[]) 
    {
        int c, n, search, array[];
        Scanner in = new Scanner(System.in);

        System.out.println("Enter number of elements");
        n = in.nextInt();
        array = new int[n];

        System.out.println("Enter " + n + " integers");
        for (c = 0; c < n; c++)
        {
            array[c] = in.nextInt();
        }

        System.out.println("Enter value to find");
        search = in.nextInt();

        for (c = 0; c < n; c++) 
        {
            if (array[c] == search)  // Searching element is present
            {
                System.out.println(search + " is present at location " + (c + 1));
                break;
            }
        }

        if (c == n)  // Searching element is absent
        {
            System.out.println(search + " is not present in array.");
        }
    }
}

Step-by-Step Explanation of the Program:

This program uses Linear Search to find a particular number in an array, which the user will input.

1. Import the Scanner Class:

 import java.util.Scanner;

 Import this class to use the scanner for console input

2. Class Declaration:

class LinearSearch {

The program lies within a class called LinearSearch.

3. Main Method:

public static void main(String args[]) {

The main method represents the entry point where the running of the program starts.

4. Declare Variables:

int c, n, search, array[];

  • c: Variable to count for the loop in the array.
  • n: The number of elements the user will input in the array.
  • search: The number the user is looking for in the array.
  • array[]: The array where the integers input by the user will be stored.

5. Create Scanner Object:

Scanner in = new Scanner(System.in);

A Scanner object is created to take user input from the console.

6. Input Number of Elements for Array:

System.out.println(“Enter number of elements”);

n = in.nextInt();

This program asks the user to input the number of elements n that the array will have.

7. Create the Array:

array = new int[n];

An array array of type int and size n is declared for the user’s integers.

8. Input Array Elements:

System.out.println(“Enter ” + n + ” integers”);

for (c = 0; c < n; c++)

       array[c] = in.nextInt();

The program prompts the user to read n integers, and the integers will be stored into the array[]. Then the for loop, which runs from 0 to n-1, will accept each integer and then assign it to the array.

9. Scan for Search Item:

System.out.println(“Enter value to find”);

search = in.nextInt();

The program prompts the user to read in the value (search) they are trying to locate within the array.

10. Linear Search Function:

for (c = 0; c < n; c++)

if (array[c] == search) 

{

           System.out.println(search + ” is present at location ” + (c + 1));

           break;

   }

  • This program runs a for loop to search for search in array.
  • For each element in the array array[c], it checks if the current element is equal to search.
  • If a match is found, it prints a message stating that the search value is found at position c + 1 (to show the position in a 1-based index) and breaks out of the loop.

11. Handle the Case Where the Element is Not Found:

if (c == n) {

System.out.println(search + ” is not present in array.”);

 }

If the loop ends without finding the search value (i.e., c == n), the program prints a message saying that the search value is not found in the array.

Example Outputs:

Case 1: Element Found

Enter number of elements

5

Enter 5 integers

12 24 56 78 89

Enter number of elements

5

Enter 5 integers

12 24 56 78 89

Enter value to find

100

100 is not present in array.

Explanation of the Output:

1. The user input 5 integers: 12 24 56 78 89.

2. The user is looking for 56.

3. The linear search finds 56 at position 3 (1-based index) and prints: 56 is present at location 3.

Case 2: Element Not Found

Enter number of elements

5

Enter 5 integers

12 24 56 78 89

Enter value to find

100

100 is not present in array.

Explanation of the Output:

1. The user specified 5 integers: 12 24 56 78 89.

2. The user is seeking 100.

3. The linear search failed to locate 100 in the array, so the program prints: 100 is not present in array.

Conclusion:

This program shows a Linear Search, where the program checks each element in the array sequentially to find a match with the search term. If the element is found, the program prints at what index the element is located. If the element is not found, it prints a message saying the element is not found in the array. The program works by letting the user input the size of the array, the elements of the array, and the element to search for.

Leave a Comment

Your email address will not be published. Required fields are marked *