Table of Contents
Introduction
The task is to find the element in array using linear search algorithm.
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:
Explanation
To search element using linear search algorithm, below steps are followed:
- Compare each element of array one by one with x starting from left.
- If x == element in array, return x is found.
- If x is not found, return x is not present in the array.
0 Comments