Python Linear Search Algorithm Implementation

Python Linear Search Algorithm Implementation

Instructor-svgAl-Mamun Sarkar
Mar 28 , 2020

Python Linear Search Implementation. The following code shows how to implement a linear search algorithm in the Python programming language. 

Code:

def linear_search(items, val):
    length = len(items)
    for i in range(0, length):
        if items[i] == val:
            return i

    return None


# Initialize the sorted list
items = [2, 7, 11, 19, 34, 53, 3, 45, 72, 75]

# Print the search result
print(linear_search(items, 34))
print(linear_search(items, 45))
print(linear_search(items, 12))

 

Output:

4
7
None

 

  • Share On:
  • fb
  • twitter
  • pinterest
  • instagram