Nishant Kumar
Cosmic Techie

Cosmic Techie

Linear Search

Nishant Kumar's photo
Nishant Kumar
·Jan 16, 2022·

1 min read

Linear Search

Subscribe to my newsletter and never miss my upcoming articles

Linear Search is a method of searching an element in a list. It is the simplest searching algorithm in which the element is searched in a sequential manner. Each element of the data structure is compared with the key(element to be searched) in a sequential order until the element is found or the last index is visited.



    for each_item in list
            return item_index
    return -1

Linear Search Time Complexity:

  • Best Case : O(1)
  • Average Case : O(n)
  • Worst Case : O(n)

Space complexity: O(1)

def search(li, n, x): 

    for i in range(0, n): 
        if (li[i] == x): 
            return i 
    return -1

inp = input("Enter the list:") 
for i in inp:

x = int(input("Enter the element to be searched: "))
n = len(li) 

# Function call 
result = search(li, n, x) 
if(result == -1): 
    print("Element is not present in array") 
    print("Element is present at index", result)
Share this