Sitemap

Linear Search in Python — Python Tutorial

3 min readJan 16, 2023

--

Python is one of the most popular and powerful languages. It takes a few lines to execute the code, which makes it a much user-friendly language. In this tutorial, we will learn linear search in Python. Searching is a technique to find whether a particular element is present or not in the given list.

There are two types of searching -

  • Linear Search
  • Binary Search

Both techniques are widely used to search for an element in the given list.

What is a Linear Search?

Linear search is a method of finding elements within a list. It is also called a sequential search. It is the most straightforward search algorithm because it searches the desired element sequentially.

It compares every element with the value that we are searching for. If both are matched, the element is found, and the algorithm returns the key’s index position.

Concept of Linear Search

Let’s understand the following steps to find the element key = 7 in the given list.

Step — 1: Start the search from the first element and Check key = 7 with each element of list x.

Step — 2: If the element is found, return the index position of the key.

Step — 3: If the element is not found, the return element is not present.

Linear Search Algorithm

There is a list of n elements and key values to be searched.

Get Rajguleria’s stories in your inbox

Join Medium for free to get updates from this writer.

Below is the linear search algorithm.

LinearSearch(list, key)  
for each item in the list
if item == value
return its index position
return -1

Python Program

Let’s understand the following Python implementation of the linear search algorithm:

def linear_Search (list1, n, key): 

# Searching list1 sequentially
for i in range(0, n):
if (list1[i] == key):
return i
return -1


list1 = [1 ,3, 5, 4, 7, 9]
key = 7

n = len(list1)
res = linear_Search(list1, n, key)
if(res == -1):
print("Element not found")
else:
print("Element found at index: ", res)

Output:

Element found at index:  4

Explanation:

In the above code, we have defined the function linear_Search(), which takes three arguments: list1, the length of the list, and the number to search. We defined for loop and iterate each element and compared it to the key value. If the element is found, return the index else return -1 which means the element is not present in the list.

Linear Search Complexity

The time complexity of linear search algorithm -

  • Base Case — O(1)
  • Average Case — O(n)
  • Worst Case -O(n)

The linear search algorithm is suitable for smaller lists (100) because it checks every element to get the desired number. Suppose there are 10,000 elements in the list and the desired element is available at the last position; this will consume much time by comparing each element of the list.

To get a fast result, we can use the binary search algorithm.

We have discussed the basic concept of a linear search. In the next tutorial, we will learn the second and most popular search algorithm named Binary search.

--

--

Rajguleria
Rajguleria

Written by Rajguleria

help 9-5 workers start a one-person business without quitting their jobs. Start building your 1-person biz here -> https://michaellim.ck.page/crashcourse 🚀