Learn Simpli

Free Online Tutorial For Programmers, Contains a Solution For Question in Programming. Quizzes and Practice / Company / Test interview Questions.

Linear Search

How linear search works

Introduction:

  1. The linear search loops an entire array sequentially and finds the matching element value,
  2. Stops looping if the key element is found or the whole list is finished,

Performance:

  1. The maximum time to find a matching element is 0(n),
  2. Best case: The linear search algorithm suits best when a key element matches the first element in the array,
  3. Worst-case: And the worst when a key element matches the last element in the array

Source code:

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 &lt;stdio.h&gt; 
  
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 &lt;bits/stdc++.h&gt; 
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?