Example #1
0
def find_smallest_diff_pair(numbers):
    """
    Does exactly what the whole program does, on subroutine level

    """

    # Can only find pairs in lists with more than one element
    if len(numbers) < 2:
        return None

    # Sort input list
    numbers = bsort(numbers)
        # To many exercises to use and analyse a more efficient algorithm

    # Walk through adjacent numbers and determine least difference with flood
    # peak algorithm
    # Initialize flood peak with first pair
    least_difference_yet = numbers[1] - numbers[0]
    least_tuple_yet      = (numbers[0], numbers[1])
    for i in range(0, len(numbers) - 1):
        cur_difference = numbers[i+1] - numbers[i]

        # Update least pair and difference yet if smaller pair found
        if 0 != cur_difference < least_difference_yet:
            least_tuple_yet      = (numbers[i], numbers[i+1])
            least_difference_yet = cur_difference

    return least_tuple_yet
Example #2
0
def profile(lists):
    for alist in lists:
        length = len(alist)
        start = time.time()
        something = sorting.bsort(alist)
        finish = time.time()
        btime = finish - start
        start = time.time()
        something = sorting.ssort(alist)
        finish = time.time()
        stime = finish - start
        start = time.time()
        something = sorting.msort(alist)
        finish = time.time()
        mtime = finish - start
        print("""The time taken to sort a list of length {0} is 
        {1} using the bubble sort algorithm, 
        {2} using the Selection sort algorithm, 
        and {3} using the merge sort algorithm""".format(length, btime, stime, mtime))
Example #3
0
    for i in range (size):
        answer.append(size - i)
    return (answer)

list100 - makelist(100)
list200 = makelist(200)
list400 = makelist(400)
list800 = makelist(800)
list1600 = makelist(1600)
lists = [list100, list200, list400, list800, list1600]

for alist in lists:
    length = len(alist)
    
    start = time.time()
    anything = sorting.bsort(alist)
    finish = time.time()
    btime = finish - start
    
    start = time.time()
    anything = sorting.ssort(alist)
    finish = time.time()
    stime = finish - start
    
    start = time.time()
    anything = sorting.msort(alist)
    finish = time.time()
    mtime = finish - start
    
    print ('''The list of length {0} took:
    {1} seconds to sort using a bubble sort,
Example #4
0
 def test2(self):
     '''Test 2 checks that the bubble sort can sort a list of 4 items reversed.
     '''
     inlist = [4,3,2,1]
     outlist = [1,2,3,4]
     assert (sorting.bsort(inlist)==outlist) #test 2 fails - list not sorted by bubble sort