What is Linear Search?
Linear search, also known as sequential search, is a simple search algorithm that works by iterating through a list of values, one at a time, until it finds the target value it is searching for or the entire list has been searched. It is one of the simplest and most basic algorithms in computer science.
How it looks?
Simple Understandings
- The linearSearch function takes two arguments: an array of values to search through and a target value that we’re looking for.
- The function then loops through each element in the array, checking if the current element is equal to the target value. If a match is found, the function returns the index of the element in the array.
- If the entire array is searched and no match is found, the function returns -1 to indicate that the target value is not present in the array.
Psudocode
function linearSearch(array, target):
for each element in array:
if element equals target:
return the index of the element
return -1 # if target is not found in array
It’s nice to learn more