Program for Linear Search using Python

Introduction

The task is to find the element in array using linear search algorithm.

Program for Linear Search using Python

Program

def search_ele(ip_arr, x):
    for i in range(len(ip_arr)):
        if ip_arr[i] == x:
            return True
    return False
  
ip_arr = [2, 15, 4, 10]
x = 4
if search_ele(ip_arr, x):
    print("Element found!")
else:
    print("Element not found!")

Output:

Program for Linear Search using Python Output

Explanation

To search element using linear search algorithm, below steps are followed:

  1. Compare each element of array one by one with x starting from left.
  2. If x == element in array, return x is found.
  3. If x is not found, return x is not present in the array.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.