Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

Linear Search Program in Java

Last Updated on August 21, 2023 by Mayank Dham

Binary search and linear search are two of the most popular search methods. This tutorial will go over the linear search program in Java. The process of finding a specific element in a list is known as searching. If the element is found in the list, the operation is considered successful, and the location of the element is returned; otherwise, the search is considered unsuccessful.

What is Linear Search ?
Another name for a linear search algorithm is a sequential search method. It is the search algorithm that is easiest to understand. A linear search simply involves going over the entire list and matching each element with the item whose position has to be determined. If a match is found, the algorithm returns the item’s location; if not, it returns NULL.

It is typically used to look for a specific element in an unsorted or unordered list of items. The linear search’s worst-case time complexity is O(n).

The processes involved in implementing linear search are as follows:

  • First, we must use a for loop to iterate through the members of the array.
  • Compare the search element with the current array element throughout each iteration of the for loop, and if the elements match, return the index of the matching array element.
  • Move on to the next element if the first one does not match.
  • Return -1 if there is no match or if the search element is missing from the array.

Now, let’s see the algorithm of linear search.

Linear Search Algorithm:

let’s say the array is X and the value is i;

  • Set j to 1
  • If j > n, jump to step 7
  • If X[j] == i, jump to step 6
  • Then, increment j by 1 i.e. j = j+1
  • Go back to step 2
  • Display the element i which is found at particular index i, then jump to step 8
  • Display element not found in the set of input elements.
  • Exit/End

How does the Linear Search Algorithm Work?

To find an element in the list below with the value k = 1, follow these steps.

Array to be searched for

  1. Compare each element x with k starting with the first element.

  2. Return the index if x == k.

  1. Else, return not found.

Java Program for Linear search

import java.util.*;
import java.lang.*;
import java.io.*;
class LinearSearch{    
public static int linearSearch(int[] arr, int key){    
        for(int i=0;i<arr.length;i++){    
            if(arr[i] == key){    
                return i;    
            }    
        }    
        return -1;    
    }    
    public static void main(String a[]){    
        int[] a1= {10,20,30,50,70,90};    
        int key = 70;    
        System.out.println(key+" is found at index: "+linearSearch(a1, key));    
    }    
}
Output: 70 is found at index: 4

Linear search complexities:

Time complexity: O(N) will be the time complexity for performing linear search.

Space complexity: O(1) will be the space complexity for performing linear search.

With this, we come to the end of this blog on ‘Linear Search in Java’. Performing linear search will help in building the logic.

Conclusion
The linear search program in Java is a simple and straightforward method for finding a target element within an array or list. While its time complexity of O(n) makes it inefficient for large datasets, it is still a viable option when dealing with smaller or unsorted collections of data. Because of its simplicity and ease of implementation, linear search is appropriate for scenarios where speed is not a critical factor or when the data lacks a specific order. However, when performance is critical, alternatives such as binary search or data structures such as hash maps and binary search trees should be considered, as they can provide faster search times. Finally, the appropriate search algorithm is determined by the specific characteristics of the data and the requirements of the application at hand.

Frequently Asked Questions (FAQs)

Here are some frequently asked questions about Java program for Linear search

Q1: What is a linear search in Java?
A linear search, also known as a sequential search, is a simple searching algorithm used to find the position of a target element within an array or a list. In Java, it involves iterating through the elements of the array one by one until the target element is found or the entire array has been searched.

Q2: How does linear search work in Java?
In Java, a linear search works by iterating through each element of an array or list, comparing each element with the target element you’re looking for. If a match is found, the index of the matching element is returned. If the entire array is searched without finding a match, the search returns a special value (e.g., -1) to indicate that the target element is not present in the array.

Q3: What is the time complexity of a linear search in Java?
The time complexity of a linear search in Java is O(n), where "n" is the number of elements in the array or list. This is because in the worst-case scenario, you might have to iterate through all elements to find the target element. The linear search is suitable for small datasets or when the elements are not sorted.

Q4: When to use a linear search in Java?
You should consider using a linear search in Java when the array or list is relatively small and not sorted, or when you don’t have prior information about the order of the elements. Linear search is also useful when you need to find multiple occurrences of a target element, as it will continue searching even after finding the first match.

Q5: Are there alternatives to linear search for better performance?
Yes, there are alternatives to linear search in Java that can offer better performance for certain scenarios. For example, if the data is sorted, you can use binary search, which has a time complexity of O(log n). If you frequently need to search for elements in large datasets, you might consider using data structures like hash maps or binary search trees, which can provide faster search times than linear search.

Other Java Programs

Java Program to Add Two Numbers
Java Program to Check Prime Number
Java Program to Check Whether a Number is a Palindrome or Not
Java Program to Find the Factorial of a Number
Java Program to Reverse a Number
Java Program to search an element in a Linked List
Program to convert ArrayList to LinkedList in Java
Java Program to Reverse a linked list
Java Program to search an element in a Linked List
Anagram Program in Java
Inheritance Program in Java
Even Odd Program in Java
Hello World Program in Java
If else Program in Java
Binary Search Program in Java

Leave a Reply

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