Linear Search - javatpoint
Linear Search - javatpoint
Home Data Structure C C++ C# Java SQL HTML CSS JavaScript Ajax
https://www.javatpoint.com/linear-search 1/17
6/5/24, 2:53 PM Linear Search - javatpoint
ADVERTISEMENT
Two popular search methods are Linear Search and Binary Search. So, here we will discuss the
popular searching technique, i.e., Linear Search Algorithm.
Linear search is also called as sequential search algorithm. It is the simplest searching algorithm.
In Linear search, we simply traverse the list completely and match each element of the list with the
item whose location is to be found. If the match is found, then the location of the item is returned;
otherwise, the algorithm returns NULL.
It is widely used to search an element from the unordered list, i.e., the list in which items are not
sorted. The worst-case time complexity of linear search is O(n).
https://www.javatpoint.com/linear-search 2/17
6/5/24, 2:53 PM Linear Search - javatpoint
The steps used in the implementation of Linear Search are listed as follows -
In each iteration of for loop, compare the search element with the current array element,
and -
If the element matches, then return the index of the corresponding array element.
If the element does not match, then move to the next element.
If there is no match or the search element is not present in the given array, return -1.
Algorithm
Linear_Search(a, n, val) // 'a' is the given array, 'n' is the size of given array, 'val' is the value to searc
Step 1: set pos = -1
Step 2: set i = 1
Step 3: repeat step 4 while i <= n
Step 4: if a[i] == val
set pos = i
print pos
go to step 6
[end of if]
set ii = i + 1
[end of loop]
https://www.javatpoint.com/linear-search 3/17
6/5/24, 2:53 PM Linear Search - javatpoint
Step 5: if pos = -1
print "value is not present in the array "
[end of if]
Step 6: exit
To understand the working of linear search algorithm, let's take an unsorted array. It will be easy to
understand the working of linear search with an example.
Now, start from the first element and compare K with each element of the array.
https://www.javatpoint.com/linear-search 4/17
6/5/24, 2:53 PM Linear Search - javatpoint
The value of K, i.e., 41, is not matched with the first element of the array. So, move to the next
element. And follow the same process until the respective element is found.
Now, the element to be searched is found. So algorithm will return the index of the element
matched.
1. Time Complexity
https://www.javatpoint.com/linear-search 5/17
6/5/24, 2:53 PM Linear Search - javatpoint
Best Case Complexity - In Linear search, best case occurs when the element we are finding
is at the first position of the array. The best-case time complexity of linear search is O(1).
Average Case Complexity - The average case time complexity of linear search is O(n).
Worst Case Complexity - In Linear search, the worst case occurs when the element we are
looking is present at the end of the array. The worst-case in linear search could be when the
target element is not present in the given array, and we have to traverse the entire array.
The worst-case time complexity of linear search is O(n).
The time complexity of linear search is O(n) because every element in the array is compared only
once.
2. Space Complexity
https://www.javatpoint.com/linear-search 6/17
6/5/24, 2:53 PM Linear Search - javatpoint
#include <stdio.h>
int linearSearch(int a[], int n, int val) {
// Going through array sequencially
for (int i = 0; i < n; i++)
{
if (a[i] == val)
return i+1;
}
return -1;
}
int main() {
int a[] = {70, 40, 30, 11, 57, 41, 25, 14, 52}; // given array
int val = 41; // value to be searched
int n = sizeof(a) / sizeof(a[0]); // size of array
int res = linearSearch(a, n, val); // Store result
printf("The elements of the array are - ");
for (int i = 0; i < n; i++)
printf("%d ", a[i]);
printf("\nElement to be searched is - %d", val);
if (res == -1)
printf("\nElement is not present in the array");
else
printf("\nElement is present at %d position of array", res);
return 0;
}
Output
#include <iostream>
https://www.javatpoint.com/linear-search 7/17
6/5/24, 2:53 PM Linear Search - javatpoint
Output
https://www.javatpoint.com/linear-search 8/17
6/5/24, 2:53 PM Linear Search - javatpoint
using System;
class LinearSearch {
static int linearSearch(int[] a, int n, int val) {
// Going through array sequencially
for (int i = 0; i < n; i++)
{
if (a[i] == val)
return i+1;
}
return -1;
}
static void Main() {
int[] a = {56, 30, 20, 41, 67, 31, 22, 14, 52}; // given array
int val = 14; // value to be searched
int n = a.Length; // size of array
int res = linearSearch(a, n, val); // Store result
Console.Write("The elements of the array are - ");
for (int i = 0; i < n; i++)
Console.Write(" " + a[i]);
Console.WriteLine();
Console.WriteLine("Element to be searched is - " + val);
if (res == -1)
Console.WriteLine("Element is not present in the array");
else
Console.Write("Element is present at " + res +" position of array");
}
https://www.javatpoint.com/linear-search 9/17
6/5/24, 2:53 PM Linear Search - javatpoint
Output
class LinearSearch {
static int linearSearch(int a[], int n, int val) {
// Going through array sequencially
for (int i = 0; i < n; i++)
{
if (a[i] == val)
return i+1;
}
return -1;
}
public static void main(String args[]) {
int a[] = {55, 29, 10, 40, 57, 41, 20, 24, 45}; // given array
int val = 10; // value to be searched
int n = a.length; // size of array
int res = linearSearch(a, n, val); // Store result
System.out.println();
System.out.print("The elements of the array are - ");
https://www.javatpoint.com/linear-search 10/17
6/5/24, 2:53 PM Linear Search - javatpoint
Output
<html>
<head>
</head>
<body>
<script>
var a = [54, 26, 9, 80, 47, 71, 10, 24, 45]; // given array
var val = 71; // value to be searched
var n = a.length; // size of array
function linearSearch(a, n, val) {
// Going through array sequencially
for (var i = 0; i < n; i++)
{
if (a[i] == val)
return i+1;
https://www.javatpoint.com/linear-search 11/17
6/5/24, 2:53 PM Linear Search - javatpoint
}
return -1
}
var res = linearSearch(a, n, val); // Store result
document.write("The elements of the array are: ");
for (i = 0; i < n; i++)
document.write(" " + a[i]);
document.write("<br>" + "Element to be searched is: " + val);
if (res == -1)
document.write("<br>" + "Element is not present in the array");
else
document.write("<br>" + "Element is present at " + res +" position of array");
</script>
</body>
</html>
Output
<?php
$a = array(45, 24, 8, 80, 62, 71, 10, 23, 43); // given array
$val = 62; // value to be searched
$n = sizeof($a); //size of array
function linearSearch($a, $n, $val) {
// Going through array sequencially
for ($i = 0; $i < $n; $i++)
{
if ($a[$i] == $val)
https://www.javatpoint.com/linear-search 12/17
6/5/24, 2:53 PM Linear Search - javatpoint
return $i+1;
}
return -1;
}
$res = linearSearch($a, $n, $val); // Store result
echo "The elements of the array are: ";
for ($i = 0; $i < $n; $i++)
echo " " , $a[$i];
echo "<br>" , "Element to be searched is: " , $val;
if ($res == -1)
echo "<br>" , "Element is not present in the array";
else
echo "<br>" , "Element is present at " , $res , " position of array";
?>
Output
So, that's all about the article. Hope the article will be helpful and informative to you.
https://www.javatpoint.com/linear-search 13/17
6/5/24, 2:53 PM Linear Search - javatpoint
← Prev Next →
ADVERTISEMENT
Feedback
https://www.javatpoint.com/linear-search 14/17
6/5/24, 2:53 PM Linear Search - javatpoint
Reinforcement
Learning
Preparation
Company
Interview
Questions
Company Questions
Trending Technologies
https://www.javatpoint.com/linear-search 15/17
6/5/24, 2:53 PM Linear Search - javatpoint
B.Tech / MCA
https://www.javatpoint.com/linear-search 16/17
6/5/24, 2:53 PM Linear Search - javatpoint
ADVERTISEMENT
https://www.javatpoint.com/linear-search 17/17