def run_selection_sort(): ll = np.random.randint(-10, 10, 10) #[7, 6, 5, 4, 3, 2, 1] ss = SelectionSort(ll) res = ss.sort() print("SelectionSort") print(f"before: {ll}") print(f"after: {res}") print()
def test_mixed_integers(self): my_list = SelectionSort( [5, 1, 10, 0, -2, 11, -3, 2, -100, 61, -176743, 163, -176742, 9]) sorted_ll = list( sorted([ 5, 1, 10, 0, -2, 11, -3, 2, -100, 61, -176743, 163, -176742, 9 ])) self.assertListEqual(sorted_ll, my_list.cleaned())
from bubble_sort import BubbleSort from selection_sort import SelectionSort from sort_strategy import SortStrategy if __name__ == '__main__': # the list to sort my_list = [1,2,3,4,5,6,7] # bubbke sort algo b = BubbleSort() # Selection sort algo s = SelectionSort() # choosing the strategy - bubble sort print("Bubble sort :") strategy = SortStrategy(b) sorted_list = strategy.sort(my_list) print("result ", sorted_list) # changing the strategy to Selection sort print("Selection sort :") strategy.algo = s sorted_list = strategy.sort(my_list)
def test_positive_integers(self): my_list = SelectionSort([4, 10, 2, 3, 8, 10, 100, 5, 8, 0, 1, 101, 17]) sorted_ll = list(sorted([4, 10, 2, 3, 8, 10, 100, 5, 8, 0, 1, 101, 17])) self.assertListEqual(sorted_ll, my_list.cleaned())
def test_float_numbers(self): my_list = SelectionSort([0.01, 0.1, 1.21, 1.20, -1.1]) sorted_ll = list(sorted([0.01, 0.1, 1.21, 1.20, -1.1])) self.assertListEqual(sorted_ll, my_list.cleaned())
def test_negative_integers(self): my_list = SelectionSort([-1, -2, -100, -66, -3, -1, -7, -200]) sorted_ll = list(sorted([-1, -2, -100, -66, -3, -1, -7, -200])) self.assertListEqual(sorted_ll, my_list.cleaned())
# -*- coding: utf-8 -*- import random import string from timeit import default_timer as timer from selection_sort import SelectionSort print "-" * 10 + "sorting numbers" + "_" * 10 items = [] for i in range(0, 10): items.append(random.randint(2, 999)) print "original items: %r" % items ssort = SelectionSort(items) # calculate execution time for our selection sort method start = timer() ssort.sort() end = timer() duration1 = end - start # calculate execution time for python built-in sort method start = timer() items.sort() end = timer() duration2 = end - start assert ssort.items == items print "sorted items: %r" % ssort.items print "Duration: our selection sort method - %ds, python builtin sort - %ds" % ( duration1, duration2) print "-" * 10 + "sorting alpha characters" + "_" * 10
from counting_sort import CountingSort from radix_sort import RadixSort from heap_sort import HeapSort from merge_sort import MergeSort from quick_sort import QuickSort from bubble_sort import BubbleSort from selection_sort import SelectionSort from insertion_sort import InsertionSort from time import time import random options = { 1: ("Bubble Sort", BubbleSort()), 2: ("Selection Sort", SelectionSort()), 3: ("Insertion Sort", InsertionSort()), 4: ("Quick Sort", QuickSort()), 5: ("Merge Sort", MergeSort()), 6: ("Radix Sort", RadixSort()), 7: ("Counting Sort", CountingSort()), 8: ("Heap Sort", HeapSort()) } print("Sorting Options: ") for i in options.keys(): print("Option {} : {}".format(i, options[i][0])) while True: option = int(input("Enter sorting option: ")) sort_alg = options[option][1] start = time() arr = [random.randint(-100000, 100000) for _ in range(10)] print(arr)