Q. Write a program that takes as input the following unsorted list of English words:


[Perfect, Stupendous, Wondrous, Gorgeous, Awesome, Mirthful, Fabulous, Splendid, Incredible, Outstanding, Propitious, Remarkable, Stellar, Unbelievable, Super, Amazing].


• Use linear search to find the position of Amazing, Perfect, Great and Wondrous in the list.  Also note the number of key comparisons required to find these words in the list.


• Use a Python function to sort the list.


• Again, use linear search to determine the position of Amazing, Perfect, Great and Wondrous in the list and note the number of key comparisons required to find these words in the list.


• Use binary search to determine the position of Amazing, Perfect, Great and Wondrous in the sorted list. Record the number of iterations required in each case.


Answer :-

def linearSearch(list, key):
    found = 0
    for index in range(0,len(list)):
        if list[index] == key:
            return key ,"is at index :-", index+1 , "and number of key  comparisons required", index
    if found == 0 :
        return key ,"is not found !!"

def bisearch (ar, key):
    it = 0
    low = 0
    high = len(ar)-1
    while low <= high :
        it += 1
        mid = int ((low + high)/2)
        if key == ar[mid]:
            return key ,"is at index :-", mid, "Number of iterations required :-",it
        elif key < ar [mid]:
            high = mid - 1
        else :
            low = mid +1
    else :
        return key ,"is not found !!"
    
lst = [ "Perfect", "Stupendous", "Wondrous", "Gorgeous", "Awesome", "Mirthful", "Fabulous", "Splendid", "Incredible", "Outstanding", "Propitious", "Remarkable", "Stellar", "Unbelievable", "Super", "Amazing"]

#i
print( linearSearch (lst , "Amazing" ))
print(linearSearch (lst , "Perfect"))
print(linearSearch (lst , "Great" ))
print( linearSearch (lst , "Wondrous" ))

#ii
lst.sort()

#iii
print( linearSearch (lst , "Amazing" ))
print(linearSearch (lst , "Perfect"))
print(linearSearch (lst , "Great" ))
print( linearSearch (lst , "Wondrous" ))

#iv
print( bisearch (lst , "Amazing" ))
print( bisearch (lst , "Perfect"))
print( bisearch (lst , "Great" ))
print( bisearch (lst , "Wondrous" ))

Output:-

('Amazing', 'is at index :-', 16, 'and number of key  comparisons required', 15)
('Perfect', 'is at index :-', 1, 'and number of key  comparisons required', 0)
('Great', 'is not found !!')
('Wondrous', 'is at index :-', 3, 'and number of key  comparisons required', 2)
('Amazing', 'is at index :-', 1, 'and number of key  comparisons required', 0)
('Perfect', 'is at index :-', 8, 'and number of key  comparisons required', 7)
('Great', 'is not found !!')
('Wondrous', 'is at index :-', 16, 'and number of key  comparisons required', 15)
('Amazing', 'is at index :-', 0, 'Number of iterations required :-', 4)
('Perfect', 'is at index :-', 7, 'Number of iterations required :-', 1)
('Great', 'is not found !!')
('Wondrous', 'is at index :-', 15, 'Number of iterations required :-', 5)

>>>

Post a Comment

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Previous Post Next Post