Linear Search
Introduction:
- The linear search loops an entire array sequentially and finds the matching element value,
- Stops looping if the key element is found or the whole list is finished,
Performance:
- The maximum time to find a matching element is 0(n),
- Best case: The linear search algorithm suits best when a key element matches the first element in the array,
- Worst-case: And the worst when a key element matches the last element in the array
Source code:
JavaScript
PHP
C
C++
Python
function linearSearchJavaScript(array, item) {
for (var i=0; i < array.length; i++)
{
if (array[i] == item) {
return "Item "+array[i]+" found at " +i;
}
}
return "Item "+item+ ' Not found...';
}
var array=['Rajani','Salman','Sharukh','Hruthik','Amitabh']
console.log(linearSearchJavaScript(array, "Salman"));
console.log(linearSearchJavaScript(array, "Sharukh"));
console.log(linearSearchJavaScript(array, "Amir"));
// output
// Item Salman found at 1
// Item Sharukh found at 2
// Item Amir Not found...
function linearSearchJavaScript(array, item) {
for (var i=0; i < array.length; i++)
{
if (array[i] == item) {
return "Item "+array[i]+" found at " +i;
}
}
return "Item "+item+ ' Not found...';
}
var array=['Rajani','Salman','Sharukh','Hruthik','Amitabh']
console.log(linearSearchJavaScript(array, "Salman"));
console.log(linearSearchJavaScript(array, "Sharukh"));
console.log(linearSearchJavaScript(array, "Amir"));
// output
// Item Salman found at 1
// Item Sharukh found at 2
// Item Amir Not found...
function linearSearchPHP($array=null,$item=null)
{
if($array===null)
{
return 'No input...';
}
$maxLoop = count($array);
for ($i = 0; $i < $maxLoop; $i++) {
if ($item == $array[$i]) return "Item $item found at $i \n" ;
}
return "Item $item not found" ;
}
$array=array('Rajani','Salman','Sharukh','Hruthik','Amitabh');
echo linearSearchPHP($array,'Salman');
echo linearSearchPHP($array,'Amir');
// output
//Item Salman found at 1
//Item Amir not found
function linearSearchPHP($array=null,$item=null)
{
if($array===null)
{
return 'No input...';
}
$maxLoop = count($array);
for ($i = 0; $i < $maxLoop; $i++) {
if ($item == $array[$i]) return "Item $item found at $i \n" ;
}
return "Item $item not found" ;
}
$array=array('Rajani','Salman','Sharukh','Hruthik','Amitabh');
echo linearSearchPHP($array,'Salman');
echo linearSearchPHP($array,'Amir');
// output
//Item Salman found at 1
//Item Amir not found
#include <stdio.h>
int linerSearchC(int arr[], int n, int item)
{
int i;
for (i = 0; i < n; i++)
if (arr[i] == item)
return i;
return -1;
}
int main(void)
{
int arr[] = { 1, 5, 15, 20, 25 };
int item = 15;
int n = sizeof(arr) / sizeof(arr[0]);
int response = linerSearchC(arr, n, item);
(response== -1)?printf("Item %d not found..."):printf("Item %d found at position %d", item, response);
return 0;
}
// output
// Item 15 found at position 2
#include <stdio.h>
int linerSearchC(int arr[], int n, int item)
{
int i;
for (i = 0; i < n; i++)
if (arr[i] == item)
return i;
return -1;
}
int main(void)
{
int arr[] = { 1, 5, 15, 20, 25 };
int item = 15;
int n = sizeof(arr) / sizeof(arr[0]);
int response = linerSearchC(arr, n, item);
(response== -1)?printf("Item %d not found..."):printf("Item %d found at position %d", item, response);
return 0;
}
// output
// Item 15 found at position 2
#include <bits/stdc++.h>
using namespace std;
int linerSearchC(int arr[], int n, int item)
{
int i;
for (i = 0; i < n; i++)
if (arr[i] == item)
return i;
return -1;
}
int main(void)
{
int arr[] = { 1, 5, 15, 20, 25 };
int item = 15;
int n = sizeof(arr) / sizeof(arr[0]);
int response = linerSearchC(arr, n, item);
(response== -1)?printf("Item %d not found..."):printf("Item %d found at position %d", item, response);
return 0;
}
// output
// Item 15 found at position 2
#include <bits/stdc++.h>
using namespace std;
int linerSearchC(int arr[], int n, int item)
{
int i;
for (i = 0; i < n; i++)
if (arr[i] == item)
return i;
return -1;
}
int main(void)
{
int arr[] = { 1, 5, 15, 20, 25 };
int item = 15;
int n = sizeof(arr) / sizeof(arr[0]);
int response = linerSearchC(arr, n, item);
(response== -1)?printf("Item %d not found..."):printf("Item %d found at position %d", item, response);
return 0;
}
// output
// Item 15 found at position 2
array = [1, 5, 15, 20, 25]
item = 15
i = flag = 0
while i < len(array):
if array[i] == item:
flag = 1
break
i = i + 1
if flag == 1:
print("Item found at position:", i + 1)
else:
print("Item not found")
# Output
# Item found at position: 3
array = [1, 5, 15, 20, 25]
item = 15
i = flag = 0
while i < len(array):
if array[i] == item:
flag = 1
break
i = i + 1
if flag == 1:
print("Item found at position:", i + 1)
else:
print("Item not found")
# Output
# Item found at position: 3
function linearSearchJavaScript(array, item) { for (var i=0; i < array.length; i++) { if (array[i] == item) { return "Item "+array[i]+" found at " +i; } } return "Item "+item+ ' Not found...'; } var array=['Rajani','Salman','Sharukh','Hruthik','Amitabh'] console.log(linearSearchJavaScript(array, "Salman")); console.log(linearSearchJavaScript(array, "Sharukh")); console.log(linearSearchJavaScript(array, "Amir")); // output // Item Salman found at 1 // Item Sharukh found at 2 // Item Amir Not found...
function linearSearchPHP($array=null,$item=null) { if($array===null) { return 'No input...'; } $maxLoop = count($array); for ($i = 0; $i < $maxLoop; $i++) { if ($item == $array[$i]) return "Item $item found at $i \n" ; } return "Item $item not found" ; } $array=array('Rajani','Salman','Sharukh','Hruthik','Amitabh'); echo linearSearchPHP($array,'Salman'); echo linearSearchPHP($array,'Amir'); // output //Item Salman found at 1 //Item Amir not found
#include <stdio.h> int linerSearchC(int arr[], int n, int item) { int i; for (i = 0; i < n; i++) if (arr[i] == item) return i; return -1; } int main(void) { int arr[] = { 1, 5, 15, 20, 25 }; int item = 15; int n = sizeof(arr) / sizeof(arr[0]); int response = linerSearchC(arr, n, item); (response== -1)?printf("Item %d not found..."):printf("Item %d found at position %d", item, response); return 0; } // output // Item 15 found at position 2
#include <bits/stdc++.h> using namespace std; int linerSearchC(int arr[], int n, int item) { int i; for (i = 0; i < n; i++) if (arr[i] == item) return i; return -1; } int main(void) { int arr[] = { 1, 5, 15, 20, 25 }; int item = 15; int n = sizeof(arr) / sizeof(arr[0]); int response = linerSearchC(arr, n, item); (response== -1)?printf("Item %d not found..."):printf("Item %d found at position %d", item, response); return 0; } // output // Item 15 found at position 2
array = [1, 5, 15, 20, 25] item = 15 i = flag = 0 while i < len(array): if array[i] == item: flag = 1 break i = i + 1 if flag == 1: print("Item found at position:", i + 1) else: print("Item not found") # Output # Item found at position: 3
Also, read What is Binary Search?
3 thoughts on “Linear Search”
Comments are closed.