Python 3 Linear Search Algorithm Script to Find Number Position in Array

Python 3 Linear Search Algorithm Script to Find Number Position in Array

Python 3 Linear Search Algorithm Script to Find Number Position in Array

# Searching an element in a list/array in python
# can be simply done using 'in' operator
# Example:
# if x in arr:
# print arr.index(x)

# If you want to implement Linear Search in python

# Linearly search x in arr[]
# If x is present then return its location
# else return -1

def search(arr, x):

    for i in range(len(arr)):

        if arr[i] == x:
            return i

    return -1

print(search([45,56,67,78],67))
Share on:

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Comment