2.3.4 Searching Algorithms

Cards (5)

  • Linear search is the most basic searching algorithm. You can think of Linear search, as going along a bookshelf one by one until you come across the book you’re looking for. Sometimes the algorithm gets lucky and finds the desired element immediately, while in other situations, the algorithm is incredibly inefficient. It’s time complexity is O(n).
  • Unlike binary search, linear search doesn’t require the data to be sorted.
  • Complete this code
    pointer = 0
    LengthOfList = [2, 3, 5, 6, 9, 11]
    searchedFor = 5
    while pointer < len(LengthOfList) and LengthOfList[pointer] != searchedFor:
    pointer += 1if pointer >= len(LengthOfList):
    print("Item is not in the list")
    else:
    print("Item is at the location", pointer)
  • The binary search algorithm can only be applied on sorted data (either alphabetically or numerically). It works by finding the middle element in a list of data before deciding which side of the data should be discarded and the process repeats until the desired element is found.
  • The binary search has the time complexity of O(log n)