Dremendo Tag Line

Linear Search in Python Programming

Searching in Python

In this lesson, we will understand what is Linear Search in Python Programming along with some examples.

What is Linear Search in Python?

A Linear Search also known as Sequential Search is a searching technique used in python to search an element from sequences like lists and arrays in a linear fashion.

In this searching technique, an element is searched in sequential order one by one in a list or array from start to end. If the element is found, then the search is successful and, we print the position of the element. If not found, then the element does not exist in the list or array.

To clearly understand the linear search technique, let's go through its algorithm step by step with an example.

video-poster

Linear Search Visualization

Suppose we have seven numbers stored in a list as shown below. We want to search the number 17 in the list.

python linear search numbers python linear search step 1

Take the 1st number 12 and compare it with the number 17. It's not matching so, check 17 with the next number.

python linear search step 2

Now take the 2nd number 9 and compare it with the number 17. It's not matching so, check 17 with the next number.

python linear search step 3

Now take the 3rd number 37 and compare it with the number 17. It's not matching so, check 17 with the next number.

python linear search step 4

Now take the 4th number 86 and compare it with the number 17. It's not matching so, check 17 with the next number.

python linear search step 5

Now take the 5th number 2 and compare it with the number 17. It's not matching so, check 17 with the next number.

python linear search step 6

Now take the 6th number 17 and compare it with the number 17. It's matching so we, have found the number 17 at index 5 in the list.

Algorithm for Linear Search

An algorithm is the steps used to implement the linear search in a python program.

Assume we have N numbers stored in a list. To implement the linear search on N numbers, the steps are as follows.

  • Define a list to store N numbers for linear search. Suppose we have defined a list with the name num.
  • Store the number we want to search in a variable say x.
  • Declare a variable f and set its value 0. For example f=0.
  • Run a loop i from 0 to N-1 to read each number from the list.
  • Check if the value of x is equal to the value of num[i].
  • If it is equal, then print the message Number found at index and the value of i on the screen and set the value of f to 1 and break the loop.
  • Outside the body of the loop i check if value of f is 0 then print the message Number not found.

Python Program for Linear Search

A Python program to search a number in a list using linear search technique.

Linear Search

# define a list
num=[12,9,37,86,2,17,5]

print('List: ',end='')
for n in num:
    print(n,end=' ')
print('\n')

# store the number in variable x to search in the list
x=int(input('Enter number to search '))

# set the value of variable f=0
f=0

# run loop i from 0 to len(num)-1 to read each number from the list
for i in range(0,len(num)):

    # check if value of x is equal to the value of num[i] then print
    # message 'Number found at index' and the value of i and set f=1 and break the loop
    if x==num[i]:
        print('Number found at index',i)
        f=1
        break

# check if value of f is 0 then print number not found
if f==0:
    print('Number not found')

Output

List: 12 9 37 86 2 17 5

Enter number to search 17
Number found at index 5

Linear Search Program Explanation

The explanation of the above program is given below in details.

Explanation

List: 12 9 37 86 2 17 5

Number to be searched in the list
x = 17
f = 0
The loop i start running from 0 to 6

When i = 0
Value of x=17 and num[0]=12
Check if 17==12  No

When i = 1
Value of x=17 and num[1]=9
Check if 17==9  No

When i = 2
Value of x=17 and num[2]=37
Check if 17==37  No

When i = 3
Value of x=17 and num[3]=86
Check if 17==86  No

When i = 4
Value of x=17 and num[4]=2
Check if 17==2  No

When i = 5
Value of x=17 and num[5]=17
Check if 17==17  Yes
Number found at index 5

f = 1
Break the loop
Loop i ends

Check if f==0   No