Linear search is also called a sequential search algorithm. It is the simplest search algorithm. In Linear search, we simply traverse the list completely from left and match each element of the array.
Implementation of Linear Search is listed as follows -
Step 1: First, we have to traverse the array of elements using for a loop.
Step 2: In each iteration of for loop, compare the search element with the current array element.
Step 3: If the element matches, then we make check variable true and break the loop and If the element does not match, then move to the next element.
Step 4: If there is no match or the search element is not present in the given array, So Check variable is false and we print " Number Not Found"
PROGRAM :
// Linear Search Algorithm
let array=[1,2,3,10,-12];
// Search Element
let num=-12
let check=false;
let i;
for(i=0;i<array.length;i++){
if(array[i]==num){
check=true;
break;
}
}
if(check==true){
console.log("Number found at index:",i)
}
else{
console.log("Number is not present")
}