Difference between revisions of "CSC111 BinarySearch.py"

From dftwiki3
Jump to: navigation, search
(Created page with '--~~~~ ---- <source lang="python"> # binarySearch.py # D. Thiebaut # # Searches a sorted array of random numbers for a number. import random #----------------------------------…')
 
Line 7: Line 7:
 
# Searches a sorted array of random numbers for a number.
 
# Searches a sorted array of random numbers for a number.
  
import random
+
from random import randrange
  
 
#------------------------------------------------------------------
 
#------------------------------------------------------------------
Line 14: Line 14:
 
     A= []
 
     A= []
 
     for i in range( n ):
 
     for i in range( n ):
         A.append( int( random.randrange( n * 100 ) ) )
+
         A.append( randrange( n * 10 ) )
 
      
 
      
 
     A.sort()
 
     A.sort()
Line 44: Line 44:
 
      
 
      
 
     A = createArray( 20 )
 
     A = createArray( 20 )
     print A
+
     print( A )
  
 
     while True:
 
     while True:
 
         print
 
         print
         key = input( "search for what number?  " )
+
         key = int( input( "search for what number?  " ) )
 
         success, index = binsearch( A, 0, len( A )-1, key )
 
         success, index = binsearch( A, 0, len( A )-1, key )
         if success:
+
         if success == True:
             print "found %d at index %d" % ( key, index )
+
             print( "found %d at index %d" % ( key, index ) )
 
         else:
 
         else:
             print "%d not in A" % key
+
             print"%d not in A" % key )
  
  

Revision as of 20:40, 28 April 2014

--D. Thiebaut 13:05, 26 April 2010 (UTC)


# binarySearch.py
# D. Thiebaut
#
# Searches a sorted array of random numbers for a number.

from random import randrange

#------------------------------------------------------------------
def createArray( n ):
    """Creates a list of n random numbers in sorted order"""
    A= []
    for i in range( n ):
        A.append( randrange( n * 10 )  )
    
    A.sort()
    return A

#------------------------------------------------------------------
def binsearch( A, low, high, key ):
    """a recursive function that searches the list A to see if
    it contains the item key between the indexes "low" and "high"
    """

    if low>high:
        return False, -1
    
    mid = ( low + high )/2
    if A[mid]==key:
        return True, mid

    if key < A[mid]:
        return binsearch( A, low, mid-1, key )
    else:
        return binsearch( A, mid+1, high, key )


#------------------------------------------------------------------
def main():
    """prompts the user for a number and returns wether the number is
    in the list or not"""
    
    A = createArray( 20 )
    print( A )

    while True:
        print
        key = int( input( "search for what number?  " ) )
        success, index = binsearch( A, 0, len( A )-1, key )
        if success == True:
            print( "found %d at index %d" % ( key, index ) )
        else:
            print(  "%d not in A" % key )


main()