-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__main__.py
49 lines (34 loc) · 1.21 KB
/
__main__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from random import seed
from random import randint
from bubblesort import Bubble_sort as bsort
from insertionsort import Insertion_sort as isort
from selectionsort import Selection_sort as ssort
from quicksort import Quick_sort as qsort
from mergesort import Merge_sort as msort
from helpers import clock_maker
def try_sort(sort_method, runs=5, array_len=1000, r_start=0, r_end=50):
attempts = []
cm_decorator = clock_maker(runs=0)
sort_method = cm_decorator(sort_method())
for _ in range(runs):
input_array = [randint(r_start, r_end) for x in range(array_len)]
try:
sorted_builtin = sorted(input_array)
sort_method(input_array)
attempts.append(sorted_builtin == input_array)
except:
pass
avg_runtime = cm_decorator.avgtime()
resolution = 'correctly sorted' if all(attempts) and len(attempts) == runs else 'did NOT sort'
print('Method {}. Avg time: {:.4f}s'.format(resolution, avg_runtime))
return avg_runtime
print('Insertion Sort:')
try_sort(isort)
print('Bubble Sort:')
try_sort(bsort)
print('Selection Sort:')
try_sort(ssort)
print('Quick Sort:')
try_sort(qsort)
print('Merge Sort:')
try_sort(msort)