Linear Search Algorithm
The linear search algorithm is defined as a sequential search algorithm
that starts at one end and goes through each element of a list until the
desired element is found; otherwise, the search continues till the end of
the dataset. In this article, we will learn about the basics of the linear
search algorithm, its applications, advantages, disadvantages, and more
to provide a deep understanding of linear search.
What is Linear Search Algorithm?
Linear search is a method for searching for an element in a collection of
elements. In linear search, each element of the collection is visited one by
one in a sequential fashion to find the desired element. Linear search is
also known as sequential search.
Algorithm for Linear Search Algorithm:
The algorithm for linear search can be broken down into the following
steps:
Start: Begin at the first element of the collection of elements.
Compare: Compare the current element with the desired element.
Found: If the current element is equal to the desired element,
return true or index to the current element.
Move: Otherwise, move to the next element in the collection.
Repeat: Repeat steps 2-4 until we have reached the end of
collection.
Not found: If the end of the collection is reached without finding
the desired element, return that the desired element is not in the
array.
How Does Linear Search Algorithm Work?
In Linear Search Algorithm,
Every element is considered as a potential match for the key and
checked for the same.
If any element is found equal to the key, the search is successful
and the index of that element is returned.
If no element is found equal to the key, the search yields “No match
found”.
For example: Consider the array arr[] = {10, 50, 30, 70, 80, 20, 90,
40} and key = 30
Step 1: Start from the first element (index 0) and compare key with each
element (arr[i]).
Comparing key with first element arr[0]. SInce not equal, the
iterator moves to the next element as a potential match.
Figure : Compare key with arr[0]
Comparing key with next element arr[1]. SInce not equal, the
iterator moves to the next element as a potential match.
Figure : Compare key with arr[1]
Step 2: Now when comparing arr[2] with key, the value matches. So the
Linear Search Algorithm will yield a successful message and return the
index of the element when key is found (here 2).
Figure :Compare key with arr[2]
Implementation of Linear Search Algorithm:
In Linear Search, we iterate over all the elements of the array and check if
it the current element is equal to the target element. If we find any
element to be equal to the target element, then return the index of the
current element. Otherwise, if no element is equal to the target element,
then return -1 as the element is not found.
Below is the implementation of the linear search algorithm:
C++CJavaPythonC#JavaScriptPHP
// Java code for linearly searching x in arr[].
import java.io.*;
class GFG {
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;
// Driver code
public static void main(String args[])
int arr[] = { 2, 3, 4, 10, 40 };
int x = 10;
// Function call
int result = search(arr, arr.length, x);
if (result == -1)
System.out.print(
"Element is not present in array");
else
System.out.print("Element is present at index "
+ result);
Output
Element is present at index 3
Applications of Linear Search Algorithm:
Unsorted Lists: When we have an unsorted array or list, linear
search is most commonly used to find any element in the collection.
Small Data Sets: Linear Search is preferred over binary search
when we have small data sets with
Searching Linked Lists: In linked list implementations, linear
search is commonly used to find elements within the list. Each node
is checked sequentially until the desired element is found.
Simple Implementation: Linear Search is much easier to
understand and implement as compared to Binary Search or Ternary
Search.
Advantages of Linear Search Algorithm:
Linear search can be used irrespective of whether the array is sorted
or not. It can be used on arrays of any data type.
Does not require any additional memory.
It is a well-suited algorithm for small datasets.
Disadvantages of Linear Search Algorithm:
Linear search has a time complexity of O(N), which in turn makes it
slow for large datasets.
Not suitable for large arrays.
When to use Linear Search Algorithm?
When we are dealing with a small dataset.
When you are searching for a dataset stored in contiguous memory.