Linear Search Algorithm — Simplified Explanation

Tanjim Hasan
2 min readFeb 25, 2023

--

Hello everyone !! This article is the first one of my algorithm blog series. In this article, we try to figure out the basic concept of linear search algorithms. And we also implement this algorithm in JavaScript.

In computer science, searching plays a vital role. people need to search for many things like text, images, videos, files, and so on. So it is important to know how search work on the computer. In computer science, we know that every solution to a problem depends on an algorithm. There are different types of searching algorithms like linear search, binary search, exponential search, ternary search, and many more. Let’s start our short journey with the linear search.

Short brief of linear search

Linear search is called sequential search, this is the most basic type of searching algorithm. The main concept is to start checking each item from a collection ( or data structure ) and stop checking when the targeted item is found.

Think we have to search for a product from a product array. So in the linear search approach, we need to check the product array from the first index until we find our targeted product.

Figure: Linear Search Algorithm

Algorithm

  1. Write a function that accepts an array & a target value as an argument.
  2. Start the iteration from left most element of the array and check our targeted value is equal to the current element of the array.
  3. if there is a match, return the current index of the array.
  4. If the targeted value is not found then the end of the array iteration returns a console log with “Sorry!! Not Found” text.

Example:

Here is an example based on a linear search algorithm. There is a function in the below code that accept two parameters: array & targeted value. This function returns the index of the targeted value from the given array.

Figure: Linear code with JavaScript

Wrap up

Linear search is a very basic search algorithm. we need to perform sequential ways for this algorithm. In the worst case, we need to traverse the array from the first index to the last.

Not least of all, thank you so much for reading📖 . Hopefully, you like this. If there is any typo or mistake send me a private note 📝 thanks 😊 .

--

--