Pages

Thursday, 25 February 2016

Linear search in Python

# Write A Program For Linear Search In Linear List….
# Change your Font Type as Courier

def linear_search(L,z) :
    for i in range(len(L)) :
        if L[i]==z :
            print "Element found at index :",i,"Position :",i+1
    else :
        print "Element not in the List"
n=int(raw_input("Enter desired linear-list size (max. 50)..."))
print "Enter elements for Linear List"
L=[0]*n
for i in range(n) :
    L[i]=raw_input("Enter Element "+str(i)+" : ")
z=raw_input("Enter Element to be searched for . . . ") 
linear_search(L,z)