What is linear search?

  • Linear Search is a searching algorithm which is used to find an item in an array or list.

How linear search works?

  • Linear search scans the list or an array from the starting one element at a time and compare it with the item that need to be found.
  • Linear search repeat those process until the item is found.
  • Linear search will stop the process when the end of the list or an array is reached.

Example:

  • Consider yourself searching for a card in a deck of shuffled cards.
  • you go through each card in the deck until the card you are looking for is found.
  • This process is same us linear search.
def linear_search(item,lst):
    i = 0
    item_found = False
    while i < len(lst):
        if(item == lst[i]):
            item_found = True
            break
        else:
            i += 1
    return item_found

Check the below link for source code:

https://github.com/NightFury5535/Python/blob/master/linear_search.ipynb