Source code for searching.linear_search

""""""
"""
==============
Linear Search
==============

TIme Complexity
    Worst Case : O(n)
    Average Case : O(n)
    Best Case : O(1)
Space Complexity : O(1)

------------
This is a searching algorithm that iterates over each element to find the key element
If key is found it will return the position
else -1
------------

"""

import sys


[docs]class LinearSearch:
if __name__ == "__main__": print( """ Input format first line contains space separated elements eg., 9 6 3 4 5 2 7 1 Second line contains the key to be searched eg., 7 """ ) input = sys.stdin.read() data = list(map(int, input.split())) array = data[:-1] key = data[-1] found = LinearSearch().linear_search(array, key) if found == -1: print("Key not found") else: print("Key is found at position : {}".format(found))