(n)
Perform a Binary search on a list of size n.

    Returns the number of key comparisons made and
    the time taken for the algorithm to run.
Perform a Binary search on a list of size n.
Perform a Binary search on a list of size n .
def test_binary_search(n):
    """
    Perform a Binary search on a list of size n.

    Returns the number of key comparisons made and
    the time taken for the algorithm to run.
    """
    sample_list = list(range(n))  # create a sorted list of n keys
    search_key = randint(0, n - 1)

    print("\nBinary Searching for", search_key, "in a list of", n, "items")

    start = time.time()
    key_comparisons_made = binary_search_count(sample_list, search_key)
    end = time.time()

    result = "For binary search of {} items, {} comparisons of keys were used"
    time_taken = "Time taken: {:.4f} milliseconds elapsed"
    print(result.format(n, key_comparisons_made))
    print(time_taken.format((end - start) * 1000))
def test_binary_search ( n ) : sample_list = list ( range ( n ) ) # create a sorted list of n keys search_key = randint ( 0 , n - 1 ) print ( "\nBinary Searching for" , search_key , "in a list of" , n , "items" ) start = time . time ( ) key_comparisons_made = binary_search_count ( sample_list , search_key ) end = time . time ( ) result = "For binary search of {} items, {} comparisons of keys were used" time_taken = "Time taken: {:.4f} milliseconds elapsed" print ( result . format ( n , key_comparisons_made ) ) print ( time_taken . format ( ( end - start ) * 1000 ) )