Linear Search in Python

Linear search is the most fundamental mechanism to search a give item in a small set of values. In this post we will understand working of Linear Search in Python program.

Linear search is simple to understand. A set of values are stored as an array. Term Linear symbolizes that search is done in linear or sequential fashion starting from the first element of the array and going to the last. Moving from one element to the next, the element to be searched is compared with each element of the array. Search process is successful as soon as an array element matches the item being searched. If no element in the array matches the search element, an unsuccessful search is reported. 

The Python solution for Linear Search is created as a menu driven program that asks user to choose one of the options

  • Read and add elements in the array.
  • Display the elements stored in the array.
  • Ask the user to enter an element to be linearly searched in the array.

Functions used in Linear Search in Python Program

For each of these options  a function is created in the Linear Search in Python program.

  • Readarr(arr) function accepts the name of the array and returns the total number of elements inputted by the user.
  • Disparr(arr,n)  accepts the name of the array and the number of elements to display all the elements of the array.
  • Linsearch(arr,n)  function accepts an array name and number of elements as arguments and  asks user  to input the element to be searched. This function returns the index of the array where the element is located when search is successful. If the element is not present in the array this functions returns -1

Linear Search-Algorithm

  1. Read the value to be searched in item in the array arr with number of elements n
  2. Initialize counter i=0
  3. Repeat  steps 4 and 5 while i<n
  4. If arr[i]= item then set loc=i and exit [successful search by returning the location of item as loc]
  5. Increment i
  6. If i=n then set loc =-1 [unsuccessful search] and exit

Code

def linsearch(arr,n):
    i=0
    item=int(input("enter element to search--->"))
    while (i<n):
        if (item==arr[i]):
            return i
        i+=1
    if i==n:
        return -1
        
def readarr(arr):
    i=0
    n=int(input("Number of elements you want to add--->"))
    while i<n:
        arr.append(int(input("input value--->")))
        i=i+1
    return n    
def disparr(arr,n):
    i=0
    print("Array elements are--->",n)
    while (i<n):
        print(arr[i])
        i=i+1
        

arr=[]
n=0
ch=0
while ch<4:
    print("Choose option.. ")
    print("1. Read Array ")
    print("2. Display Array ")
    print("3. Find an element ")
    print("4. Exit Program")
    ch=int(input("enter your choice--->"))
    if ch==1:
        n=readarr(arr)
    elif ch==2:
        disparr(arr,n)
    elif ch==3:
        loc=linsearch(arr,n)
        if loc==-1:
            print("element is not in list")
        else:         
            print("element found at ",loc+1)
    else:
        exit
Linear Search in Python

Be First to Comment

Leave a Reply

Your email address will not be published.