Sequential Search / Linear Search Program
Example:1
Python program to assume a list of elements. Further, search for an element from the list. (Without Using Functions)
n=[10,20,50,32,156,57,243,122,615,129] print("List is ") print(n) #item=156 item=int(input("Enter the element to search ")) pos = 0 found=False while pos < len(n) and found==False: if(n[pos] == item): found = True else: pos = pos + 1 if(found==True): print("element found at position ",(pos+1)) else: print("element is not found")
Output:
Case:1
List is [10, 20, 50, 32, 156, 57, 243, 122, 615, 129] Enter the element to search 32 element found at position 4 >>>
Case:2
List is [10, 20, 50, 32, 156, 57, 243, 122, 615, 129] Enter the element to search 125 element is not found >>>
Example:2
Write a python program to assume a list of elements Further, search an element from the list. (Using Functions)
n=[10,20,50,32,156,57,243,122,615,129] print("List is ") print(n) #item=156 item=int(input("Enter the element to search ")) pos = 0 found=False while pos < len(n) and found==False: if(n[pos] == item): found = True else: pos = pos + 1 if(found==True): print("element found at position ",(pos+1)) else: print("element is not found")
Output:
Case:1
List is [10, 20, 50, 32, 156, 57, 243, 122, 615, 129] Enter the element to search 32 element found at position 4 >>>
Case:2
List is [10, 20, 50, 32, 156, 57, 243, 122, 615, 129] Enter the element to search 125 element is not found >>>