Contents

  1. search2.py
  2. search_compare.py
  3. search.py

search2.py 1/3

[
top][prev][next]
# Demonstrate implementations of the linear and binary search
# techniques.
# Sara Sprenkle

# represents that binarySearch did not find the key
NOT_FOUND=-1

def main():
    
    integers = range(1,20,2)
    print "The list: ", integers
    print
    
    print "Search for", 1
    print "Linear: Found?", linearSearch(integers,1)
    # binarySearch returns the position the number was found, or -1 if it was
    # not found.  Translate the result from binarySearch to a True or False
    pos = binarySearch(integers, 1)
    binFound = pos != NOT_FOUND
    print "Binary: Found?", binFound
    print
    
    print "Search for", 4
    print "Linear: Found?", linearSearch(integers,4)
    pos = binarySearch(integers, 4)
    binFound = pos != NOT_FOUND
    print "Binary: Found?", binFound
    print

    print "Search for", 15
    print "Linear: Found?", linearSearch(integers, 15)
    pos = binarySearch(integers, 15)
    binFound = pos != NOT_FOUND
    print "Binary: Found?", binFound
    print

    print "Search for", 16
    print "Linear: Found?", linearSearch(integers, 16)
    pos = binarySearch(integers, 16)
    binFound = pos != NOT_FOUND
    print "Binary: Found?", binFound
    print
    
    print "Search for", 17
    print "Linear: Found?", linearSearch(integers, 17)
    pos = binarySearch(integers, 17)
    binFound = pos != NOT_FOUND
    print "Binary: Found?", binFound


def linearSearch(searchlist, key):
    for elem in searchlist:
        if elem == key:
            return True
    return False

def binarySearch(searchlist, key):
    low = 0
    high = len(searchlist)-1
    while low <= high:
        mid = (low+high)/2
        if searchlist[mid] == key:
            return mid
        if searchlist[mid] < key: # search upper half
            low = mid+1
        else: # search lower half
            high = mid-1
    return NOT_FOUND

main()

search_compare.py 2/3

[
top][prev][next]
# Compare the linear and binary searching techniques by the number of
# comparisons.
# by Sara Sprenkle

def main():

    print "This program helps us to empirically compare search strategies"
    print

    #keys = [1, 4, 13, 19, 125, 126, 127]
    keys = [1, 4, 13, 19, 125, 126, 127, 19997, 19998, 19999]

    NUM_ENTRIES = 10000
    
    integers = range(1,NUM_ENTRIES*2,2)
    #print integers
    print "Creating list of size", NUM_ENTRIES*2, "starting at 1 and incrementing by 2"
    print

    print "%6s|%14s" % ("","# Comparisons")
    print "%6s| %6s %6s" % ("KEY", "LIN", "BIN")
    print "-"* 24
    
    total_lin_comp = 0.0
    total_bin_comp = 0.0
    
    for key in keys:
        lin_comp = linearSearch(integers,key)
        bin_comp = binarySearch(integers,key)
        print "%6d| %6d %6d" % (key, lin_comp, bin_comp)
        
        # update the total number of comparisons
        total_lin_comp += lin_comp
        total_bin_comp += bin_comp
    
    print 
    print "ON AVERAGE..."
    print "-"*50
    print
    print "The average number of comparisons per search for"
    print "\tlinear search was", total_lin_comp/len(keys)
    print "\tbinary search was", total_bin_comp/len(keys)
    print
    print "Disclaimer: May not be a completely fair comparison, but gives us an idea."


# Return the number of comparisons required to find the key.
def linearSearch(searchlist, key):
    numChecks = 0
    for elem in searchlist:
        numChecks += 1
        if elem == key:
            return numChecks
    return numChecks

# Return the number of comparisons required to find the key.
def binarySearch(searchlist, key):
    low = 0
    high = len(searchlist)-1
    numChecks = 0
    while low <= high:
        mid = (low+high)/2
        numChecks+= 2
        if searchlist[mid] == key:
            return numChecks
        numChecks+=1
        if searchlist[mid] < key:
            low = mid+1
        else:
            high = mid-1
    return numChecks

main()

search.py 3/3

[
top][prev][next]
# Demonstrate implementations of the linear search technique.
# Sara Sprenkle

def main():
    
    integers = range(1,20,2)
    print "The list: ", integers
    print
    
    print "Search for", 1
    print "Linear: Found?", linearSearch(integers,1)
    print
    
    print "Search for", 4
    print "Linear: Found?", linearSearch(integers,4)
    print

    print "Search for", 15
    print "Linear: Found?", linearSearch(integers, 15)
    print

    print "Search for", 16
    print "Linear: Found?", linearSearch(integers, 16)
    print
    
    print "Search for", 17
    print "Linear: Found?", linearSearch(integers, 17)


def linearSearch(searchlist, key):
    for elem in searchlist:
        if elem == key:
            return True
    return False

main()

Generated by GNU enscript 1.6.4.