Here you will get program for linear search in python.
Linear search is one of the simplest searching algorithm in which targeted item in sequentially matched with each item in a list. It is worst searching algorithm with worst case time complexity O (n).
Below is its implementation.
Also Read: Python Binary Search
Program 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 break i = i + 1 if flag == 1: print("item found at position:", i + 1) else: print("item not found")
Output
list of items is [5, 7, 10, 12, 15]
enter item to search:12
item found at position: 4
Comment below if you have any queries regarding python linear search program.
Nice example
yeah ofcourse
How can we do this by getting the list values from user input and searching for a number from the list which the user entered and displaying it
Hey aasif u can use a for loop to get the inputs
Items=[]
a=int(input(‘enter no of elements’)
for i in range(0,a):
c=int(input(‘enter the elements’))
items.append(c)
hoe to make quality or the best phyton? Any step to get my phyton more better ?
i love your work keep up the good code Neeraj Mishra
Very good solution and explanation
sprb example