Searching Algorithms: Linear Search (Javascript)

Edur
1 min readMar 1, 2021
Screen capture from Visual Code displaying a linearSearch algorithm
Linear Search implementation

Linear Search :

Linear search is probably the easiest searching algorithm out there. It’s going to check one by one all the elements of an array in a sequential order.

Time complexity

As “n” grows, the average amount of time it takes will grow. If we have an array with a million elements it is going to search one by one until it finds the passed value.

Best case O(1).
Even if it’s pretty rare, depending on the size of the data. The Best Case would be find the thing we’re looking for right away.

Worst Case is O(n)
In case the element we’re searching is the last one or is not even there our algorithm will have to check sequentially all the elements incrementing the time complexity.

Pseudocode:

1º We need a function that accepts an array and a value.
2º We have to loop through the array and check if the current array element is equal to the value.
3º If it is, the function will return the index at which the element is found.
4ºIf the value is never found, it will return -1.

Code:

--

--