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()
# 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
def test(self, args): SelectionSort.sort(args) assert SelectionSort.is_sorted(args) SelectionSort.show(args)
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 items = []
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')
def test_selection_sort(): sort = SelectionSort() unsorted_numbers = [9,3,5,1,4,5,7] assert_equals(sort.sort(unsorted_numbers), sorted(unsorted_numbers))