# Introduction to Linear Search Algorithm

** 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 data set. In this article, we will learn about the basics of Linear Search Algorithm, Applications, Advantages, Disadvantages, etc. to provide a deep understanding of Linear Search.

Table of Content

- What is Linear Search Algorithm?
- Algorithm for Linear Search Algorithm
- How Does Linear Search Algorithm Work?
- Implementation of Linear Search Algorithm
- Time and Space Complexity of Linear Search Algorithm
- Applications of Linear Search Algorithm
- Advantages of Linear Search Algorithm
- Disadvantages of Linear Search Algorithm
- When to use Linear Search Algorithm?
- Frequently Asked Questions (FAQs) on Linear Search Algorithm

__What is Linear Search Algorithm?__

** Linear Search **is a method for searching 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:

Begin at the first element of the collection of elements.**Start:**Compare the current element with the desired element.**Compare:**If the current element is equal to the desired element, return true or index to the current element.**Found:**Otherwise, move to the next element in the collection.**Move:**Repeat steps 2-4 until we have reached the end of collection.**Repeat:**If the end of the collection is reached without finding the desired element, return that the desired element is not in the array.**Not found:**

__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

**and**

**arr[] = {10, 50, 30, 70, 80, 20, 90, 40}****= 30**

**key**

Start from the first element (index 0) and compareStep 1:with each element (arr[i]).key

- Comparing key with first element arr[0]. SInce not equal, the iterator moves to the next element as a potential match.

- Comparing key with next element arr[1]. SInce not equal, the iterator moves to the next element as a potential match.

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).Step 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++ code to linearly search x in arr[].
#include <bits/stdc++.h>
using namespace std;
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
int main(void)
{
int arr[] = { 2, 3, 4, 10, 40 };
int x = 10;
int N = sizeof(arr) / sizeof(arr[0]);
// Function call
int result = search(arr, N, x);
(result == -1)
? cout << "Element is not present in array"
: cout << "Element is present at index " << result;
return 0;
}

// C code to linearly search x in arr[].
#include <stdio.h>
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
int main(void)
{
int arr[] = { 2, 3, 4, 10, 40 };
int x = 10;
int N = sizeof(arr) / sizeof(arr[0]);
// Function call
int result = search(arr, N, x);
(result == -1)
? printf("Element is not present in array")
: printf("Element is present at index %d", result);
return 0;
}

// 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);
}
}

# Python3 code to linearly search x in arr[].
def search(arr, N, x):
for i in range(0, N):
if (arr[i] == x):
return i
return -1
# Driver Code
if __name__ == "__main__":
arr = [2, 3, 4, 10, 40]
x = 10
N = len(arr)
# Function call
result = search(arr, N, x)
if(result == -1):
print("Element is not present in array")
else:
print("Element is present at index", result)

// C# code to linearly search x in arr[].
using System;
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's code
public static void Main()
{
int[] arr = { 2, 3, 4, 10, 40 };
int x = 10;
// Function call
int result = search(arr, arr.Length, x);
if (result == -1)
Console.WriteLine(
"Element is not present in array");
else
Console.WriteLine("Element is present at index "
+ result);
}
}
// This code is contributed by DrRoot_

// Javascript code to linearly search x in arr[].
function search(arr, n, x)
{
for (let i = 0; i < n; i++)
if (arr[i] == x)
return i;
return -1;
}
// Driver code
let arr = [ 2, 3, 4, 10, 40 ];
let x = 10;
let n = arr.length;
// Function call
let result = search(arr, n, x);
(result == -1)
? console.log("Element is not present in array")
: console.log("Element is present at index " + result);
// This code is contributed by Manoj

<?php
// PHP code for linearly search x in arr[].
function search($arr, $n, $x)
{
for($i = 0; $i < $n; $i++) {
if($arr[$i] == $x)
return $i;
}
return -1;
}
// Driver Code
$arr = array(2, 3, 4, 10, 40);
$x = 10;
// Function call
$result = search($arr, sizeof($arr), $x);
if($result == -1)
echo "Element is not present in array";
else
echo "Element is present at index " ,
$result;
// This code is contributed
// by jit_t
?>

**Output**

Element is present at index 3

__Time and Space Complexity of Linear Search Algorithm:__

**Time Complexity:**

In the best case, the key might be present at the first index. So the best case complexity is O(1)**Best Case:**In the worst case, the key might be present at the last index i.e., opposite to the end from which the search has started in the list. So the worst-case complexity is O(N) where N is the size of the list.**Worst Case:**O(N)**Average Case:**

** Auxiliary Space:** O(1) as except for the variable to iterate through the list, no other variable is used.Â

__Applications of Linear Search Algorithm:__

When we have an unsorted array or list, linear search is most commonly used to find any element in the collection.**Unsorted Lists:**Linear Search is preferred over binary search when we have small data sets with**Small Data Sets:**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.**Searching Linked Lists:**Linear Search is much easier to understand and implement as compared to Binary Search or Ternary Search.**Simple Implementation:**

**Advantages of Linear Search **__Algorithm__**:**

**Advantages of Linear Search**

**:**- 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__**:**

**Disadvantages of Linear Search**

**:**- 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 to use Linear Search**

**?**- When we are dealing with a small dataset.
- When you are searching for a dataset stored in contiguous memory.

__Frequently Asked Questions (FAQs) on Linear Search Algorithm:__

**1. What is linear search **algorithm**?**

**1. What is linear search**

**?**

Linear search algorithm, also known as sequential search algorithm, is a simple searching algorithm that traverses a list or array sequentially to find a target element. In Linear Search, we can get the

**2. How does linear search **algorithm** work?**

**2. How does linear search**

**work?**

Linear search algorithm iterates through each element in the list or array, comparing it with the target element until a match is found or the end of the list is reached. If the end of the list is reached, then it means that the target element is not present in the array.

**3. What is the time complexity of linear search **algorithm**?**

**3. What is the time complexity of linear search**

**?**

The time complexity of linear search algorithm is O(n), where n is the number of elements in the list or array being searched. This means the time taken for searching increases linearly with the size of the input.

**4. When is linear search **algorithm** preferred over other searching algorithms?**

**4. When is linear search**

**preferred over other searching algorithms?**

Linear search algorithm is preferred when the list or array is unsorted, or when the size of the input is relatively small. It’s simple to implement and doesn’t require the data to be in any specific order.

**5. What are the advantages of linear search **algorithm**?**

**5. What are the advantages of linear search**

**?**

Linear search algorithm is easy to implement, and it works efficiently on small-sized arrays or lists. It doesn’t require any pre-processing like sorting, making it suitable for dynamic data structures.

**6. What are the disadvantages of linear search **algorithm**?**

**6. What are the disadvantages of linear search**

**?**

Linear search algorithm becomes inefficient for large-sized arrays or lists, as it needs to scan through each element sequentially. It has a time complexity of O(n), which means the search time grows linearly with the size of the input.

**7. How do you implement linear search **algorithm** in programming languages like Python, Java, or C++?**

**7. How do you implement linear search**

**in programming languages like Python, Java, or C++?**

Linear search algorithm can be implemented using loops to iterate through the elements of the array or list, comparing each element with the target value until a match is found or the end of the list is reached.

**8. Can linear search **algorithm** be applied to other data structures?**

**8. Can linear search**

**be applied to other data structures?**

Yes, linear search algorithm can be applied not only to arrays or lists but also to other linear data structures like linked lists. The principle remains the same: iterating through each element until the target is found or the end is reached.

**9. Is linear search **algorithm** suitable for sorted arrays or lists?**

**9. Is linear search**

**suitable for sorted arrays or lists?**

While linear search algorithm can still be used on sorted arrays or lists, it’s not the most efficient option. Binary search, for example, is more suitable for sorted data as it has a time complexity of O(log n).

**10. What are some real-world applications of linear search **algorithm**?**

**10. What are some real-world applications of linear search**

**?**

Linear search algorithm can be used in scenarios such as searching for a specific value in a phone book, searching for a name in an unsorted list of contacts, or finding an item in a grocery list. It’s often used in scenarios where the data size is small or not expected to grow significantly.

**Related Articles:**

- What is Linear Search?
- Time and Space Complexity of Linear Search Algorithm
- Linear Search vs Binary Search
- What is linear search useful for?
- Improving Linear Search Technique