Source code for search
"""This module shows two methods for search to find an index and
a third one is shown for comparison of computational complexity.
* Method 1: linear search, O(N) complexity
* Method 2: binary search, O(log N) complexity
* Method 3: set solution (for membership check only), O(1) complexity
We can see the difference in running times by executing this module.
"""
import random
import time
[docs]def lin_search(l, val):
""" Linear search algorithm: O(N) complexity as it checks
each item. Returns the index of the val if it is found in the
list, and None if it is not found.
"""
for i in xrange(len(l)):
if l[i] == val:
return i
return None
[docs]def bin_search(l, val):
""" Binary search algorithm: O(log N) complexity as it checks
about log N of the items. Assumes the input list is sorted.
Returns the index of the val if it is found in the list, and None
if it is not found.
Algorithm:
#. initialize: min:beginning of list, max:end of list, mid:mid point
#. while min != max
#. if mid value is the value being searched, return its index
#. if value searched is greater than mid value, adjust min value
#. else adjust max value
"""
if len(l) == 0:
return None
min = 0
max = len(l)-1
mid = (min+max)/2
while min != max:
if l[mid] < val:
min = mid+1
else:
max = mid
mid = (min+max)/2
if l[mid]==val:
return mid
return None
[docs]def test_search(f, l, testvaluelist):
"""Example function for testing other functions. Note: it takes a
function as an argument and then calls this function.
"""
start = time.time()
for val in testvaluelist:
x = f(l,val)
end = time.time()
print "Function:%s, takes \t%.10f seconds per call" \
%(f.__name__, (end-start)/float(len(testvaluelist)))
if __name__=="__main__":
#Testing three ways to search a long list. Calling with 100
#test values.
N = 100000
l = []
for i in range(N):
l.append(random.randint(1,N*100))
l.sort()
lset = set(l)
testN = 100
testvals = []
for i in range(testN):
testvals.append(random.randint(1,N))
##Linear search is O(N), which is costly. But this is the most
##general algorithm. It works for lists, returns an index and
##works when the list is not sorted.
test_search( lin_search, l, testvals)
##Binary search is O(log N), but only works when the input list
##is sorted.
test_search( bin_search, l, testvals)
#Note the set solution is O(1), or constant time, the fastest of all
##three. But it is not equivalent to the above two functions as
##it does not give an index.
test_search( set.__contains__, lset, testvals)