Ejemplo n.º 1
0
    def test_insertion_sort(self):
        print('Insertion sort random scenario')
        result = insertion_sort(self.arr)

        print('Insertion sort worst case scenario')
        result = insertion_sort(sorted(self.arr, reverse=True))

        print('Insertion sort best case scenario')
        result = insertion_sort(sorted(self.arr))
        self.assertEquals(result, sorted(self.arr))
Ejemplo n.º 2
0
def exp1(path, st=0, n=1000, sp=1, n2=1, do_sort=False, do_reverse=False):
    times = np.zeros((N,2))
    for i in range(st, n, sp):
        print('Round i:{}'.format(i))
        t1 = time()
        for j in range(n2):
            random.seed(9876)
            unsorted = [random.randint(0, n) for _ in range(i)]
            if do_sort:
                unsorted = sorted(unsorted, reverse=do_reverse)
            # print(unsorted)
            sort = insertion_sort(unsorted) # sort list of 10 randomly selected numbers
            # print('Insertion Sort:{}'.format(sort))
            assert sort == sorted(unsorted)
        times[i, 0] = time() - t1

        # random.seed(9876)
        # unsorted = (1000)*[random.randint(0, N) for _ in range(i)]
        t1 = time()
        for j in range(n2):
            random.seed(9876)
            unsorted = [random.randint(0, n) for _ in range(i)]
            if do_sort:
                unsorted = sorted(unsorted, reverse=do_reverse)
            # print(unsorted)
            sort = merge_sort(unsorted) # sort list of 10 randomly selected numbers
            # print('Merge Sort:{}'.format(sort))
            assert sort == sorted(unsorted)
        times[i, 1] = time() - t1
    np.save(path, times)
Ejemplo n.º 3
0
    def test_insertion_sort(self):
        alist = [54, 26, 93, 17, 77, 31, 44, 55, 20]
        sorted_list = [17, 20, 26, 31, 44, 54, 55, 77, 93]

        self.assertEqual(sorting_algorithms.insertion_sort(alist), sorted_list)
Ejemplo n.º 4
0
import sorting_algorithms

print('This script will sort your array of numbers')

string_of_numbers = input('Print your numbers, devided by commas\n')

list_of_strings = string_of_numbers.split(',')

list_of_numbers = [int(x) for x in list_of_strings]

print(f'Your list of numbers is {list_of_numbers}')

user_choice = input('Instruction: \n'
                    'Choose sorting algorithm: \n'
                    'For bubble sort choose "b"\n'
                    'For insertion sort choose "i"\n'
                    'For quick sort choose "q"\n'
                    'For selection sort choose "s"\n')

if user_choice == 'b':
    sorting_algorithms.bubble_sort(list_of_numbers)
elif user_choice == 'i':
    sorting_algorithms.insertion_sort(list_of_numbers)
elif user_choice == 'q':
    sorting_algorithms.quick_sort(list_of_numbers)
elif user_choice == 's':
    sorting_algorithms.selection_sort(list_of_numbers)
else:
    user_choice = input('Incorrect choice.')

print(f'Your sorted list is {list_of_numbers}')
Ejemplo n.º 5
0
 def test_insertion_sort_duplicate_numbers(self):
     input = [6, 4, 3, 1, 6, 2]
     sorted_input = insertion_sort(input)
     self.assertEqual([1, 2, 3, 4, 6, 6], sorted_input)
Ejemplo n.º 6
0
 def test_insertion_sort_list_already_sorted(self):
     input = [1, 2, 3, 4, 5]
     sorted_input = insertion_sort(input)
     self.assertEqual([1, 2, 3, 4, 5], sorted_input)
Ejemplo n.º 7
0
 def test_insertion_sort_basic(self):
     input = [5, 3, 1, 4, 2]
     sorted_input = insertion_sort(input)
     self.assertEqual([1, 2, 3, 4, 5], sorted_input)
Ejemplo n.º 8
0
import sorting_algorithms

print(sorting_algorithms.selection_sort([54, 26, 93, 17, 77, 31, 44, 55, 20]))
print(sorting_algorithms.bubble_sort([54, 26, 93, 17, 77, 31, 44, 55, 20]))
print(sorting_algorithms.quick_sort([54, 26, 93, 17, 77, 31, 44, 55, 20]))
print(sorting_algorithms.insertion_sort([54, 26, 93, 17, 77, 31, 44, 55, 20]))