Linear Search Algorithm Using Python

Understanding the importance of Linear Search Algorithm.

Manan B Shah
2 min readJun 28, 2020
photo by photo by shibajidebnath

Algorithm is an important concept in programming and every programmers must be well versed in it to write efficient codes. According to Wikipedia Algorithm is defined as “a set of rules that precisely defines a sequence of operations”. Algorithm is basically a set of simplified rules or instructions that will help users to perform a task or operation in much efficient manner.

Algorithms are used to find the best possible way of solving a problem and sometimes even used to save millions of dollars by implementing Algorithms to the project.

Examples : Linear Search ,Binary Search , Binary Search , Fibonacci Search, Exponential Search and many other.

Today we shall be discussing about LINEAR SEARCH ALGORITHM and its importance in programming

picture by netlify

In simple terms we can define linear search algorithm as Linear search is a very simple search algorithm. In this type of search, a sequential search is made over all items one by one. Every item is checked and if a match is found then that particular item is returned, otherwise the search continues till the end of the data collection.

Linear Search Advantages

  • It is simplest and conventional searching technique.
  • Linear search can be applied on both sorted or unsorted list of data.
  • It is easy to implement.

Lets get started how Linear Search Algorithm works in python:

consider you have a list of random numbers in a file say 100 you need to find whether the a particular number exist or not. It would not be efficient way to go through all the numbers.

image by simplesnippets

The problem above can be solved using the below python program :

The above program is a example of Linear search algorithm

Explaination:

  1. list = total values present in the list , key = the value to be found from the list

2. if and else loops are used for checking the value needed to be found

3. list.index(key) + 1 is used as in languages number starts from zero so we can understand in human number prediction in which index the number is present.

Output:

--

--

Manan B Shah
Manan B Shah

No responses yet