English

Use the linear search program to search the key with value 8 in the list having duplicate values such as [42, -2, 32, 8, 17, 19, 42, 13, 8, 44]. What is the position returned? What does this mean?

Advertisements
Advertisements

Question

Use the linear search program to search the key with value 8 in the list having duplicate values such as [42, -2, 32, 8, 17, 19, 42, 13, 8, 44]. What is the position returned? What does this mean?

Answer in Brief
Advertisements

Solution

Position returned = 4

This means that linear search always returns the first position where the element is present if it is present multiple times in the list.

The linear search function is written as under:

def linearSearch(list, key):

    #function to perform the search

    for index in range(0,len(list)):

        if list[index] == key: #key is present

            return index+1 #position of key in list

    return None #key is not in list

#end of function
shaalaa.com
Linear Search in Python
  Is there an error in this question or solution?
Chapter 6: Searching - Exercise [Page 94]

APPEARS IN

NCERT Computer Science [English] Class 12
Chapter 6 Searching
Exercise | Q 2. | Page 94
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×