Python Linear Search

Here you may get the program for linear search in python.

Linear search is one {in all|one amongst|one in every of} the best-looking algorithms during which targeted item in consecutive matched with every item in a list. it’s the worst looking rule with worst-case time quality O (n).
A simple approach is to try to linear search, i.e

Start from the left component of arr[] and one by one compare x with every element of arr[]

If x matches with a part, come back the index.
If x doesn’t match with any of components, return -1.

Program Code for Python Linear Search

items = [5, 7, 10, 12, 15]
print("list of items is", items)
x = int(input("enter item to search:"))
i = flag = 0
while i < len(items):
	if items[i] == x:
		flag = 1
	i = i + 1
if flag == 1:
	print("item found at position:", i + 1)
	print("item not found")


list of items is [5, 7, 10, 12, 15]
enter item to search:12
item found at position: 4

Comment Down If You Have any questions about Linear Search.

Leave a Comment

error: Alert: Content is protected!!