Ejemplo n.º 1
0
def merge_sort_basic_random(items):
    alg = randint(1, 5)
    if alg is 1:
        print('Using Bubble Sort.')
        print('Running Times:')
        print('Worst Case:')
        list1 = bubble_sort(items[:len(items) / 2])
        list2 = bubble_sort(items[len(items) / 2:])
    elif alg is 2:
        print('Using Selection Sort.')
        print('Running Times:')
        print('Worst Case:')
        list1 = selection_sort(items[:len(items) / 2])
        list2 = selection_sort(items[len(items) / 2:])
    elif alg is 3:
        print('Using Insertion Sort.')
        print('Running Times:')
        print('Worst Case:')
        list1 = insertion_sort(items[:len(items) / 2])
        list2 = insertion_sort(items[len(items) / 2:])
    elif alg is 4:
        print('Using Tree Sort.')
        print('Running Times:')
        print('Worst Case:')
        list1 = tree_sort(items[:len(items) / 2])
        list2 = tree_sort(items[len(items) / 2:])
    elif alg is 5:
        print('Using Cocktail Shaker Sort.')
        print('Running Times:')
        print('Worst Case:')
        list1 = cocktail_shaker_sort(items[:len(items) / 2])
        list2 = cocktail_shaker_sort(items[len(items) / 2:])

    return merge(list1, list2)
Ejemplo n.º 2
0
    def test_static_sorts(self):
        # Note: None of these methods should affect the original lists!
        original = self.unordered

        sorting.selection_sort(self.unordered)
        self.assertEqual(original, self.unordered)

        sorting.insertion_sort(self.unordered)
        self.assertEqual(original, self.unordered)

        sorting.merge_sort(self.unordered)
        self.assertEqual(original, self.unordered)
Ejemplo n.º 3
0
    def test_static_sorts(self):
        # Note: None of these methods should affect the original lists!
        original = self.unordered

        sorting.selection_sort(self.unordered)
        self.assertEqual(original, self.unordered)

        sorting.insertion_sort(self.unordered)
        self.assertEqual(original, self.unordered)

        sorting.merge_sort(self.unordered)
        self.assertEqual(original, self.unordered)
Ejemplo n.º 4
0
 def test_selection_sort(self):
     array = sorting.selection_sort(sorting.arr)
     for i in range(5):
         a = random.randint(0, len(array) - 1)
         b = random.randint(0, len(array) - 1)
         if (a > b):
             assert array[a] > array[b]
         elif (a <= b):
             assert array[a] <= array[b]
Ejemplo n.º 5
0
def sort_list(choice, lista):

    #classe Monitor gera objetos relatorio
    time1 = timeit.default_timer()
    if ((choice) == 0):
        relatorio = insertion_sort(lista)
    elif ((choice) == 1):
        relatorio = selection_sort(lista)
    elif ((choice) == 2):
        relatorio = merge_sort(lista)
    elif ((choice) == 3):
        relatorio = quick_sort(lista)
#    else:
#        CompMov = bubble_sort(lista)
    time2 = timeit.default_timer()  #(time2)-(time1)

    relatorio.time = (time2 - time1)
    relatorio.text = "Tempo para ordenar dados: "
    return (relatorio)
Ejemplo n.º 6
0
def time(size):
    import time
    result = [size]
    unsort = generate_array(size)

    array = unsort[:]
    t0 = time.clock()
    run_sort = sorting.bubble_sort(array)
    t1 = time.clock() - t0
    result.append(round(t1, 6))

    array = unsort[:]
    t0 = time.clock()
    run_sort = sorting.insertion_sort(array)
    t1 = time.clock() - t0
    result.append(round(t1, 6))

    array = unsort[:]
    t0 = time.clock()
    run_sort = sorting.selection_sort(array)
    t1 = time.clock() - t0
    result.append(round(t1, 6))

    array = unsort[:]
    t0 = time.clock()
    run_sort = sorting.merge_sort(array)
    t1 = time.clock() - t0
    result.append(round(t1, 6))

    array = unsort[:]
    t0 = time.clock()
    run_sort = sorting.shell_sort(array)
    t1 = time.clock() - t0
    result.append(round(t1, 6))

    array = unsort[:]
    t0 = time.clock()
    run_sort = sorting.quick_sort(array, 0, len(array))
    t1 = time.clock() - t0
    result.append(round(t1, 6))

    return result
Ejemplo n.º 7
0
import sorting

a = [2, 4, 5, 73, 35, 6, 4, 45]
print(sorting.selection_sort(a))
a = [5, 6546, 7, 8, 97, 46, 657, 35, 7]
print(sorting.insertion_sort(a))
Ejemplo n.º 8
0
    def test_selection_sort(self):
        test = sorting.selection_sort(self.unordered)
        self.assertEqual(test, self.ordered)

        test = sorting.selection_sort(self.unordered_with_dupes)
        self.assertEqual(test, self.ordered_with_dupes)
Ejemplo n.º 9
0
 def test_selection_sort(self):
     """Tests that test list is correctly sorted."""
     self.assertEqual(sorted(self.test_list), selection_sort(self.test_list))
Ejemplo n.º 10
0
 def test_selection_sort(self):
     """Tests that test list is correctly sorted."""
     self.assertEqual(sorted(self.test_list),
                      selection_sort(self.test_list))
 def test_selection_sort(self):
     selection_sort(self.unsorted_array)
     self.assertEqual(self.unsorted_array, self.sorted_array)
Ejemplo n.º 12
0
from numpy.random import rand

from sorting import selection_sort, insertion_sort, bubble_sort, merge_sort, quick_sort

array_to_sort = (rand(10) * 100).tolist()

print(selection_sort(array_to_sort.copy()) == sorted(array_to_sort))

print(insertion_sort(array_to_sort.copy()) == sorted(array_to_sort))

print(bubble_sort(array_to_sort.copy()) == sorted(array_to_sort))

print(merge_sort(array_to_sort.copy()) == sorted(array_to_sort))

print(quick_sort(array_to_sort.copy()) == sorted(array_to_sort))
Ejemplo n.º 13
0
    args = parser.parse_args()
    print("Algorithm used: {}".format(args.algorithm))
    print()
    print("Unsorted list: {}".format(args.items))
    print()

    if type(args.items) == str:
        items = [int(i) for i in args.items[1:-1].split(",")]
    else:
        items = args.items

    if args.algorithm == "bubble sort":
        sorted_list = sorting.bubble_sort(items)

    elif args.algorithm == "selection sort":
        sorted_list = sorting.selection_sort(items)

    elif args.algorithm == "insertion sort":
        sorted_list = sorting.insertion_sort(items)

    elif args.algorithm == "shell sort":
        sorted_list = sorting.shell_sort(items, args.sequence)

    elif args.algorithm == "heap sort":
        sorted_list = sorting.heap_sort(items)

    elif args.algorithm == "merge sort":
        sorted_list = sorting.merge_sort(items)

    elif args.algorithm == "quick sort":
        sorted_list = sorting.quick_sort(items)
Ejemplo n.º 14
0
 def test_selection_hold(self):
     data = [5, 7, 9, 1, 0, 6]
     assert selection_sort(data) == [0, 1, 5, 6, 7, 9]
from searching import binary_search
from sorting import selection_sort
import random

array = list(range(56, 789, 9))
# print(binary_search(array, 317))

array = list(range(0, 301))
random.shuffle(array)
print(array)
print(selection_sort(array))

Ejemplo n.º 16
0
    def test_selection_sort(self):
        test = sorting.selection_sort(self.unordered)
        self.assertEqual(test, self.ordered)

        test = sorting.selection_sort(self.unordered_with_dupes)
        self.assertEqual(test, self.ordered_with_dupes)
Ejemplo n.º 17
0
 def test_selection_sort(self):
     u_list = [8, 2, 6, 4, 5]
     s_list = [2, 4, 5, 6, 8]
     self.assertEqual(selection_sort(u_list)['u_list'], s_list)
Ejemplo n.º 18
0
def test_insertion_sort():
	data = populate_random_data()
	sorting.selection_sort(data)

	assert sorted(data) == data
Ejemplo n.º 19
0
 def test_selection_sort(self):
     sorting.selection_sort(self.list_a)
     sorting.selection_sort(self.list_b)
     sorting.selection_sort(self.list_c)
     sorting.selection_sort(self.list_d)
     sorting.selection_sort(self.list_one)
     sorting.selection_sort(self.list_two)
     self.assertEqual(self.list_a, self.list_sorted)
     self.assertEqual(self.list_b, self.list_sorted)
     self.assertEqual(self.list_c, self.list_sorted)
     self.assertEqual(self.list_d, self.list_sorted)
     self.assertEqual(self.list_one, [1])
     self.assertEqual(self.list_two, [-10, -5])
Ejemplo n.º 20
0
def test_selection_sort(input_arr, expected_arr):
    sorting.selection_sort(input_arr)
    assert input_arr == expected_arr
Ejemplo n.º 21
0
def test_selection_sort():
    arr = [9,8,15,11,12,4,5,7,3,0]
    selection_sort(arr)
    assert arr == [0,3,4,5,7,8,9,11,12,15]
Ejemplo n.º 22
0
import random
from sorting import insertion_sort, selection_sort

lista = random.sample(range(1, 100), 30)

if __name__ == "__main__":
    print("Antes do selection sort")
    print(lista)
    selection_sort(lista)
    print("Depois do selection sort")
    print(lista)