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())
def test_sort(self): for item in self.datas: for filename, ret in item.items(): print('Reading ' + filename) a = [] with open(os.path.join(self.data_path, filename), 'r') as f: lines = f.readlines() [a.extend(line.split()) for line in lines] SelectionSort.selection_sort(a) self.assertListEqual(a, ret)
# operation repetition (to take avarages of benchmark times) for z in range(0, m): # repetition for different orders of array size for i in range(1,n): l = 10**i # order of array size arr = [int(random.random()*100) for j in range(l)] # create an array of random numbers with incremental order alist = [0]*len(arr) # initialize an empty array of same size times[i-1][0] = l # add the order as the first column in the matrix # a loop to go through all the sorting algorithms for g in range(1, 6): start = time.clock() # get a CPU clock tick if g == 1: temp = SelectionSort.sort(dup(alist, arr)) elif g == 2: temp = InsertionSort.sort(dup(alist, arr)) elif g == 3: temp = MergeSort.sort(dup(alist, arr)) elif g == 4: temp = QuickSort.sort(dup(alist, arr)) elif g == 5: temp = CountSort.sort(dup(alist, arr)) end = time.clock() - start # get a CPU clock tick and estimate algorithm r setin(i, end, g) endit = time.clock() - begin # estimate overal calculation time print ('Total time Elapsed:', endit, 'seconds.') # show the benchmark matrix
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(self, args): SelectionSort.sort(args) assert SelectionSort.is_sorted(args) SelectionSort.show(args)
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())
def selection_sort(array): start = timeit.default_timer() copied_array = copy.copy(array) SelectionSort.sort(array=copied_array) end = timeit.default_timer() return format(end - start, '.5f')
# -*- 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
# run selection sort from selection_sort import SelectionSort s = SelectionSort() print(s.sort_min([1, 5, 3, 5, 20, 4])) print(s.sort_max([2, 8, 5, 4, 11]))
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)
def test_selection_sort(): sort = SelectionSort() unsorted_numbers = [9,3,5,1,4,5,7] assert_equals(sort.sort(unsorted_numbers), sorted(unsorted_numbers))
def test_selection_sort(self): lst = SelectionSort() self.assertEqual([1, 1, 2, 3, 5, 6, 7, 8, 9], lst.selection_sort([6, 3, 5, 1, 8, 2, 7, 1, 9]))
def test_selection_sort(self): sortedList = SelectionSort.selection_sort(self.targetList) self.assertTrue(sortedList == self.checkList)