I. Sequential lookup---O (n)
Unordered List Lookup
def sequentialsearch (alist,item):p os = 0found = Falsewhile pos < len (alist) and not found:if alist[pos] = = Item:found = Trueelse:pos = pos + 1return foundlist = [2,3,1,4,5,6,0]print (Sequentialsearch (list,5)) print (Sequentialsearch (list,7) )
List lookup with sequence
def ordersequentialsearch (alist,item):p os = 0found = Falsestop = Falsewhile pos < len (alist) and not found and not stop : if alist[pos] = = Item:found = Trueelse:if Alist[pos] > item:stop = Trueelse:pos = pos + 1return foundlist = [1,2,3,4,5 , 6,7]print (Ordersequentialsearch (list,3)) print (Ordersequentialsearch (list,9))
  
two. Two points find--- O (log^n)
def binarysearch (alist,item): first = 0 last = len (list)-1found = Falsewhile First <= last and not found:midpoint = (f Irst + last)//2if alist[midpoint] = = Item:found = Trueelse:if Item < Alist[midpoint]:last = Midpoint-1else:first = Midpoint + 1return Found list = [0,1,2,3,4,5,6,7,8]print (BinarySearch (list,3)) print (BinarySearch (list,10))
Recursive implementation of binary search
def binarysearchcur (Alist,item): If Len (alist) = = 0:return Falseelse:midpoint = Len (alist)//2if alist[midpoint] = = Item:r Eturn trueelse:if Item < Alist[midpoint]:return binarysearchcur (Alist[:midpoint],item) else:print (alist[midpoint+ 1:]) return Binarysearchcur (Alist[midpoint+1:],item)
  
def sequentialsearch (alist,item):p os = 0found = False
While Pos < Len (alist) and not found:if alist[pos] = = Item:found = Trueelse:pos = pos + 1return foundlist = [2,3,1,4,5, 6,0]print (Sequentialsearch (list,5)) print (Sequentialsearch (list,7))
Python Implementation Lookup algorithm