def test_counting_on_large_list(self):
     T1 = [5, 1, 6, 78, 5, 1, 2, 5, 1]
     counting_sort(T1)
     assert T1 == [1, 1, 1, 2, 5, 5, 5, 6, 78]
     T2 = [5, 1, 2, 56, 2, 6, 78, 8, 90, 124]
     counting_sort(T2)
     assert T2 == [1, 2, 2, 5, 6, 8, 56, 78, 90, 124]
     T3 = [56, 1, 3, 634, 7, 8, 2, 123, 1, 2]
     bucket_sort(T3)
     assert T3 == [1, 1, 2, 2, 3, 7, 8, 56, 123, 634]
Ejemplo n.º 2
0
def main():
    """Read command-line arguments and test sorting algorithms."""
    import sys
    args = sys.argv[1:]  # Ignore script file name

    if len(args) == 0:
        script = sys.argv[0]  # Get script file name
        print('Usage: {} sort num max'.format(script))
        print('Test sorting algorithm `sort` with a list of `num` integers')
        print('    randomly sampled from the range [1...`max`] (inclusive)')
        print('\nExample: {} bubble_sort 10 20'.format(script))
        print('Initial items: [3, 15, 4, 7, 20, 6, 18, 11, 9, 7]')
        print('Sorted order?  False')
        print('Sorting items with bubble_sort(items)')
        print('Sorted items:  [3, 4, 6, 7, 7, 9, 11, 15, 18, 20]')
        print('Sorted order?  True')
        return

    # Get sort function by name
    if len(args) >= 1:
        sort_name = args[0]
        # Terrible hack abusing globals
        if sort_name in globals():
            sort_function = globals()[sort_name]
        else:
            # Don't explode, just warn user and show list of sorting functions
            print('Sorting function {!r} does not exist'.format(sort_name))
            print('Available sorting functions:')
            for name in globals():
                if 'sort' in name:
                    print('    {}'.format(name))
            return

    # Get num_items and max_value, but don't explode if input is not an integer
    try:
        num_items = int(args[1]) if len(args) >= 2 else 20
        max_value = int(args[2]) if len(args) >= 3 else 50
        # print('Num items: {}, max value: {}'.format(num_items, max_value))
    except ValueError:
        print('Integer required for `num` and `max` command-line arguments')
        return

    # Test sort function
    counting_sort(num_items)
 def test_counting_small(self):
     T1 = [19, 5, 14, 8, 5]
     counting_sort(T1)
     assert T1 == [5, 5, 8, 14, 19]
     T2 = [56, 12, 12, 8]
     counting_sort(T2)
     assert T2 == [8, 12, 12, 56]
     T3 = [5, 2, 3, 5]
     counting_sort(T3)
     assert T3 == [2, 3, 5, 5]
 def test_counting_with_negative(self):
     T1 = [-10, -5, -50, 40]
     counting_sort(T1)
     assert T1 == [-50, -10, -5, 40]
     T2 = [-25, -9, 56, 20]
     counting_sort(T2)
     assert T2 == [-25, -9, 20, 56]
     T3 = [-4, 20, -15, 50]
     counting_sort(T3)
     assert T3 == [-15, -4, 20, 50]
Ejemplo n.º 5
0
 def test_counting_sort(self):
     for _ in range(100):
         random_and_sorted = _generate_testcase()
         SortInt.counting_sort(random_and_sorted[0])
         assert random_and_sorted[0] == random_and_sorted[1]