Friendly Algorithm for Beginners

Vmanzanilla
2 min readOct 15, 2020

The picture above is what comes to mind when I think about algorithms. A sea of letters, numbers, and symbols. But I will demonstrate a friendly intro algorithm that is easy to understand. Linear Search is a simple algorithm, but not efficient in which I will explain later. Linear Search in basic writing is: Start from the leftmost element of the array and one by one compare with your target with each element of the array. Then if the target matches with an element in the array, return the index. If the target doesn’t math with any element in the array, return null.

Here is the linear search function in JavaScript:

function linearSearch(arr, target) {for (let i = 0; i < arr.length; i++) {if (arr[i] === target) {return i;}}return null}

The reason why linear search is not efficient is that imagine given a task to find a certain employee in a big database of 1,000,000 entries and that employee is at the end or near the end. Well, if you use this algorithm then is going to compare each employee name or id with your employee name or id. This way is going to take a long time. This algorithm is an example of Brute-Force Algorithm because it is a straightforward method to get a result by comparing one by one instead of being creative on its approach.

--

--