def start_algorithm(): global data, result, numbers if not data: return if (algmenu.get() == 'Selection Sort'): selection_sort(data, drawData) drawData(data, ['Green' for x in range(len(data))]) result = "Selection Sort \n" + ' '.join(map(str, data)) data = copy.deepcopy(numbers) if (algmenu.get() == 'Insertion Sort'): insertion_sort(data, drawData) drawData(data, ['Green' for x in range(len(data))]) result = "Insertion Sort \n" + ' '.join(map(str, data)) data = copy.deepcopy(numbers) if (algmenu.get() == 'Bubble Sort'): bubble_sort(data, drawData) drawData(data, ['Green' for x in range(len(data))]) result = "Bubble Sort \n" + ' '.join(map(str, data)) data = copy.deepcopy(numbers) if (algmenu.get() == 'Quick Sort'): quick_sort(data, 0, len(data)-1, drawData) drawData(data, ['Green' for x in range(len(data))]) result = "Quick Sort \n" + ' '.join(map(str, data)) data = copy.deepcopy(numbers) if (algmenu.get() == 'Merge Sort'): merge_sort(data, 0, len(data)-1, drawData) drawData(data, ['Green' for x in range(len(data))]) result = "Merge Sort \n"' '.join(map(str, data)) data = copy.deepcopy(numbers)
def select_sort(): global array if var.get() == "Selection Sort": selection_sort(array, draw_array) elif var.get() == "Insertion Sort": insertion_sort(array, draw_array) elif var.get() == "Quicksort": quick_sort(array, 0, len(array) - 1, draw_array)
def test_selection_sort(self): """ Tests the selection_sort(list) method """ data = [3, 1, 10, 9] results = selection_sort(data) self.assertIsInstance(results, tuple) self.assertEqual(results[0], [1, 3, 9, 10]) data = random.sample(range(0, 100), 10) results = selection_sort(data) self.assertEqual(results[0], sorted(data))
def simulation(): bubble_time = 0 selection_time = 0 insertion_time = 0 merge_time = 0 quick_time = 0 tim_time = 0 for i in range(EXPERIMENTS): # create a list with some number of values from the given range my_list = random.sample(range(MAX_VALUE), LIST_SIZE) start = time.time() bubble_sort(my_list) bubble_time += time.time() - start my_list = random.sample(range(MAX_VALUE), LIST_SIZE) start = time.time() selection_sort(my_list) selection_time += time.time() - start my_list = random.sample(range(MAX_VALUE), LIST_SIZE) start = time.time() insertion_sort(my_list) insertion_time += time.time() - start my_list = random.sample(range(MAX_VALUE), LIST_SIZE) start = time.time() merge_sort(my_list) merge_time += time.time() - start my_list = random.sample(range(MAX_VALUE), LIST_SIZE) start = time.time() quick_sort(my_list) quick_time += time.time() - start my_list = random.sample(range(MAX_VALUE), LIST_SIZE) start = time.time() my_list.sort() tim_time += time.time() - start print('Bubble sort took', bubble_time/EXPERIMENTS*1000, 'ms') print('Selection sort took', selection_time/EXPERIMENTS*1000, 'ms') print('Insertion sort took', insertion_time/EXPERIMENTS*1000, 'ms') print('Merge sort took', merge_time/EXPERIMENTS*1000, 'ms') print('Quick sort took', quick_time/EXPERIMENTS*1000, 'ms') print('Timsort took', tim_time/EXPERIMENTS*1000, 'ms')
def test_repeated_numbers(): """ Test to see how a sorting algo deals with the same number repeatedly """ repeat_number_list = selection_sort([2, 2, 2, 2, 2, 2, 2, 4]) assert all(repeat_number_list[i] <= repeat_number_list[i + 1] for i in range(len(repeat_number_list) - 1))
def test_huge_list(): """Calls another function to test sorting algo against a really large randomly generated list""" huge_list = selection_sort([two_random_large_list()]) assert all(huge_list[i] <= huge_list[i + 1] for i in range(len(huge_list) - 1))
def test_selection_sort_none(self): self.assertEqual(selection_sort(None), 'Nothing to sort.')
def test_selection_sort_nums_positive_and_negative_odd(self): self.assertEqual(selection_sort([-12, -19, -20, 45, 91]), [-20, -19, -12, 45, 91])
def test_selection_sort_nums_negative_odd(self): self.assertEqual( selection_sort([-20, -12, -45, -19, -91]), [-91, -45, -20, -19, -12])
print("Starting Insertion Sort - ASC") st = time.time() insertion_sort(asc_array) print('Insertion sort took %.3f seconds' % ((time.time() - st))) print("") print("Starting Insertion Sort - DESC") st = time.time() insertion_sort(desc_array) print('Insertion sort took %.3f seconds' % ((time.time() - st))) # Selection Sort print("-----------------") print("Starting Selection Sort - RAND") st = time.time() selection_sort(rand_array) print('Selection sort took %.3f seconds' % ((time.time() - st))) print("") print("Starting Selection Sort - ASC") st = time.time() selection_sort(asc_array) print('Selection sort took %.3f seconds' % ((time.time() - st))) print("") print("Starting Selection Sort - DESC") st = time.time() selection_sort(desc_array) print('Selection sort took %.3f seconds' % ((time.time() - st))) # Merge Sort
def selection(*args): return selection_sort(*args)
def test_sorts_already_sorted(): expected = [num for num in range(42)] now_sorted = selection_sort(expected) assert expected == now_sorted
def test_TypeError(): """ Tests non-list types throw TypeError. """ with pytest.raises(TypeError): assert selection_sort('string')
def test_selection(): A = [i for i in range(100, 0, -1)] selection_sort(A) assert A == B
def __init__(self, name, listing): # Initilization steps self.name = name listing_copy = listing listing_copy2 = listing self.listing = listing_copy ################################################## # For each algorithm, the accuracy, time, and list # will be tracked using the methods shown below ################################################## # Python sort will be used as the master sort listing_copy_py = listing_copy start = timer() self.python_sorted_list = python_sort(listing_copy_py) end = timer() self.python_sorted_time = end - start # Bubble Sort will be tested against python sort listing_copy_bu = listing_copy start = timer() self.bubble_sorted_list = bubble_sort(listing_copy_bu) end = timer() self.bubble_sort_accurate = False if self.bubble_sorted_list == self.python_sorted_list: self.bubble_sort_accurate = True self.bubble_sort_time = end - start # Selection sort will be tested against python sort listing_copy_sel = listing_copy start = timer() self.selection_sorted_list = selection_sort(listing_copy_sel) end = timer() self.selection_sort_accurate = False if self.selection_sorted_list == self.python_sorted_list: self.selection_sort_accurate = True self.selection_sort_time = end - start # Merge sort will be tested against python sort listing_copy_mer = listing_copy start = timer() self.merge_sorted_list = merge_sort(listing_copy_mer) end = timer() self.merge_sort_accurate = False if self.merge_sorted_list == self.python_sorted_list: self.merge_sort_accurate == True self.merge_sort_time = end - start #Shell root sort will be tested against python sort listing_copy_she = listing_copy start = timer() self.shell_sorted_list = shell_sort(listing_copy_she) end = timer() self.shell_accurate = False if self.shell_sorted_list == self.python_sorted_list: self.shell_accurate = True self.shell_time = end - start #Insertion sort will be tested against python sort listing_copy_ins = listing_copy start = timer() self.insertion_sorted_list = insertion_sort(listing_copy_ins) end = timer() self.insertion_accurate = False if self.insertion_sorted_list == self.python_sorted_list: self.insertion_accurate = True self.insertion_time = end - start # Heap sort will be tested against python sort listing_copy_hea = listing_copy start = timer() self.heap_sorted_list = heap_sort(listing_copy_hea) end = timer() self.heap_accurate = False if self.heap_sorted_list == self.python_sorted_list: self.heap_accurate = True self.heap_time = end - start #Strand sort will be tested against python sort listing_copy_strand = listing_copy2 start = timer() self.strand_sorted_list = strand_sort(listing_copy_strand) end = timer() self.strand_accurate = False if self.strand_sorted_list == self.python_sorted_list: self.strand_root_accurate = True self.strand_time = end - start
def test_empty(): """ Tests empty and length 1 Lists return correctly. """ assert selection_sort([]) == [] assert selection_sort([8]) == [8]
def test_two(): """ Tests returns as expected. """ assert selection_sort([7, -3, 6, -99, 4, 1, 12, 8, 401, 5]) == [-99, -3, 1, 4, 5, 6, 7, 8, 12, 401]
def test_one(): """ Tests returns as expected. """ assert selection_sort([7, 2, 6, 3, 4, 1, 8, 5]) == [1, 2, 3, 4, 5, 6, 7, 8]
#!/usr/bin/env python3 import sort_utils from selection import selection_sort tests_passed = 0 total_tests = 0 test_name = "Test 1 - a" print(f"---- {test_name} ----------------------------") orig = li = [12, 7, 32, 5, 16, 4] li = list(orig) selection_sort(li) print(li) print("\n".join([ "---- Expected output -------------------------", "[12, 7, 32, 5, 16, 4]", "swap 0:12 <--> 5:4", "[4, 7, 32, 5, 16, 12]", "swap 1:7 <--> 3:5", "[4, 5, 32, 7, 16, 12]", "swap 2:32 <--> 3:7", "[4, 5, 7, 32, 16, 12]", "swap 3:32 <--> 5:12", "[4, 5, 7, 12, 16, 32]", "----------------------------------------------", ])) total_tests+= 1 expected = [4, 5, 7, 12, 16, 32]
def test_sorts_list_of_duplicates(): unsorted = [14, 33, 27, 10, 35, 19, 42, 44] expected = [10, 14, 19, 27, 33, 35, 42, 44] now_sorted = selection_sort(unsorted) assert expected == now_sorted
def test_sort(self): for j in range(10): array = [] for i in range(100): array.append(randint(-100, 100)) self.assertListEqual(selection_sort(array), sorted(array))
def test_selection_sort(array: List[int]): """Checks that returned array is the same as result of sorted()""" assert sorted(array) == selection_sort(array)
def test_fixed_list(): """ Uses a small static list for easy verification of a working sort algo""" fixed = selection_sort([one_list()]) assert all(fixed[i] <= fixed[i + 1] for i in range(len(fixed) - 1))
def test_selection_sort_empty(self): self.assertEqual(selection_sort([]), 'Nothing to sort.')
def test_selection_sort_not_nums(self): self.assertEqual(selection_sort([0, 'a', 3]), 'Can only sort lists of just numbers.') self.assertEqual(selection_sort(['a', 'b', 'c']), 'Can only sort lists of just numbers.')
def test_shuffled_list_gets_sorted(): expected = [num for num in range(42)] unsorted = expected[:] shuffle(unsorted) now_sorted = selection_sort(unsorted) assert expected == now_sorted
def test_selection_sort_single_value(self): self.assertEqual(selection_sort([5]), [5])
def test_sort_validates_expected_input(): with pytest.raises(TypeError): selection_sort('hello world')
def test_selection_sort_nums_positive_odd(self): self.assertEqual(selection_sort([20, 12, 45, 19, 91]), [12, 19, 20, 45, 91])
# list2=["TupolevTu-204", 107900, 7200] # list3=["IlushinIl-62", 280300, 11000] # list4=["AirbusA310", 164000, 11000] # list5=["Boeing-737", 52800, 10058] # list6=["Boeing-777", 242600, 10668] # frame = pd.DataFrame([list1,list2,list3,list4,list5,list6]) # frame.to_csv('plane.csv',index=False) def open_csv(csv_file: str): with open(csv_file) as file: reader = list(csv.reader(file)) list_of_planes = [] for line in reader: list_of_planes.append( plane_info.Plane(line[0], int(line[1]), int(line[2]))) return list_of_planes if __name__ == '__main__': plane_list = open_csv("plane.csv") for plane in plane_list: print(plane) print("") sorted_selection = selection.selection_sort(plane_list) for plane in sorted_selection: print(plane) print("") heapsort = heapsort.Heapsort() sorted_heap = heapsort.heap_sort(plane_list) for plane in sorted_heap: print(plane)
def selection(*args): return selection_sort(*args)