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
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