How to Implement Linear Search: The Simplest Search Algorithm Explained
Linear search is a simple searching algorithm. It is also known as the sequential search. In linear search, it checks an array for an item by comparing from the first item of the array to the last item of it until the matched item is found.
By the way, if you missed my previous article on Algorithms, you can read it right here.
Alright, back to this one again!
Linear search can be used in both sorted and unsorted lists. However, as it checks every element position, the time assumed is high in linear search.
An algorithm for linear search can be represented using pseudocodes as below.
The above algorithm in a pseudocode can be implemented in Python language using an example as below.
How the Linear Search Works
Example 1 — Search for item 300 in the following array.
Array = [100, 200, 300, 400, 500, 600, 700, 800, 900, 1000]
In the above example, there are ten elements in the array. Therefore, the position index numbers are from 0 to 9.
When the element 300 is to found, the linear search starts to scan the array from index value 0 in iteration 1. As the element is not in index value 0, it increments the index value by 1 and scan the next position in iteration 2. As the element is not there, it again increments the index value by 1 and scan the next position in iteration 3. The item 300 is found at the third position, which has the index value 2. Now the linear search will return that value as the output.
Example 2 — Search for item 500 in the above array.
Array = [100, 200, 300, 400, 500, 600, 700, 800, 900, 1000]
Get Chamod Kavishka’s stories in your inbox
Join Medium for free to get updates from this writer.
Like in the above example, when the element 500 is to found, the linear search starts to scan the array from index value 0 in iteration 1. As the element is not there, it scans the index value 1 in iteration 2. As the element is not there, it then scans the index value 2 in iteration 3. As the element was not found, it scans the index value 3 in iteration 4. As the element was not found, it scans the index value 4 in iteration 5. The item 500 is found and the index value of 4 will be the output.
Example 3 — Search for item 1500 in the above array.
Array = [100, 200, 300, 400, 500, 600, 700, 800, 900, 1000]
Like in the above examples, linear search starts to scan the array from index value 0 until the end of the array, which is index value 9. As there is no matching result, it gives the “False’ output as the result.
These examples were implemented in Python language. The expected outputs were reached.
Linear search is effective for unsorted and smaller data sets. But, when it comes to real world scenarios, that is not the case more often than not. Hence we need another solution. Actually, we have one. I will talk about it later with the upcoming articles.
Keep up and connect with me! 🚀
The article may have come to an end, but there is always more to find, learn and achieve. Are you someone who thinks the same?
Stay updated with the latest trends, insights and exciting opportunities to grow professionally as well as personally. Let’s build a community of innovators and dreamers! Check the links below for more.
💼 LinkedIn: https://www.linkedin.com/in/uxchamod
📚 All Links: https://linktr.ee/uxchamod









