def linearSearch(arr, key):  #user-defined function
          for i in range(len(arr)): 
              if (arr[i] == key): 
                  return i 
          return -1
      arr = [50, 90, 30, 70, 60]  #array
      key = 70  #search key
      index = linearSearch(arr, key) #calling function
      # display result
      if (index == -1):
          print(key, 'not Found.
          print(key, 'Found at Index', index)
      Python language logo

      Linear Searching


      Python Program for Linear Search

      ALGORITHM: The procedure to find an element in a given array or list through linear search, a) Take an array and the search key. Assume they are:- array and key b) Traverse through the array. c) Compare the key with each element. d) If the match is found then return the position. e) Else repeat the process until the end of the array. f) After traversing the array If the match is not found then return -1.


        Add Comment

        Log in to add a comment

        Codiga - All rights reserved 2022.