मराठी

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

प्रश्न

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

उत्तर

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
  या प्रश्नात किंवा उत्तरात काही त्रुटी आहे का?
पाठ 6: Searching - Exercise [पृष्ठ ९४]

APPEARS IN

एनसीईआरटी Computer Science [English] Class 12
पाठ 6 Searching
Exercise | Q 2. | पृष्ठ ९४
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×