def linear_search(L,x): # Scan the list L from left to right and compare it with x. # If we find x, return the index. If not, return -1. for i in range(len(L)): # i = 0, 1, ..., len(L)-1 if x==L[i]: return i # Will only arrive here, if x is not in L return -1 def linear_search_returnAll(L,x): # Scan the list L from left to right and compare it with x. # If we find x, store index in result list. If not, return empty list. result=[] for i in range(len(L)): # i = 0, 1, ..., len(L)-1 if x==L[i]: result.append(i) # Will only arrive here, if x is not in L return result def binary_search(L,x): ''' Input: List L, sorted in ascending order; and element x Output: index of x in L, or -1 otherwise 1. Test whether x is less than the median (if it is equal, we are done). 2. Continue to search the half of the list that x is in. 3. We are done when the correct side of the list is empty. ''' left = 0 # left index of "zoomed-in" portion right = len(L)-1 # right index of "zoomed-in" portion #print "outside: left=", left, ", right=",right while left<=right: ## not finished median = (left+right)/2 # index of median; automatically rounded down #print "left=", left, ", right=",right if xL[median] # throw away left portion, continue in right portion left = median+1 # x not found return -1 def binary_search_rec(L, x, left, right): if left>right: # empty portion of L return -1 median = (left+right)/2 if x