/
Run_Sorts.py
97 lines (77 loc) · 2.59 KB
/
Run_Sorts.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# Import random for generating arrays
from random import randint
# Import Sorting Algorithms
import sorting
# Import texttable to create results table
import texttable
# Import matplotlib to create graph of data
import matplotlib.pyplot as plt; plt.rcdefaults()
import numpy as np
import matplotlib.pyplot as plt
# Take in the lists of sorts, generate an array of length 'size' for each sort then time and record each
def time(size):
import time
result = [size]
unsort = generate_array(size)
array = unsort[:]
t0 = time.clock()
run_sort = sorting.bubble_sort(array)
t1 = time.clock() - t0
result.append(round(t1, 6))
array = unsort[:]
t0 = time.clock()
run_sort = sorting.insertion_sort(array)
t1 = time.clock() - t0
result.append(round(t1, 6))
array = unsort[:]
t0 = time.clock()
run_sort = sorting.selection_sort(array)
t1 = time.clock() - t0
result.append(round(t1, 6))
array = unsort[:]
t0 = time.clock()
run_sort = sorting.merge_sort(array)
t1 = time.clock() - t0
result.append(round(t1, 6))
array = unsort[:]
t0 = time.clock()
run_sort = sorting.shell_sort(array)
t1 = time.clock() - t0
result.append(round(t1, 6))
array = unsort[:]
t0 = time.clock()
run_sort = sorting.quick_sort(array, 0, len(array))
t1 = time.clock() - t0
result.append(round(t1, 6))
return result
# Generate an a random array of length 'size'
def generate_array(size):
unsort = []
while len(unsort) < 10 ** size:
unsort.append(randint(1, 10 * size))
return unsort
# Create a table to record times and quantities of each sort
results = []
for i in range(5):
if 1 < i:
results.append(time(i))
# Create table, add data and print table
tab = texttable.Texttable()
headings = ["Data Size (10 ** n)", "Bubble", "Insertion", "Selection", "Merge", "Shell", "Quick"]
tab.header(headings)
for result in results:
tab.add_row(result)
s = tab.draw()
print(s)
print("* all time values have been rounded to 3 decimal places for table presentation\n"
"* Exact values are shown in the chart")
# Chart Presentation of the Data
for result in results:
objects = ("Bubble", "Insertion", "Selection", "Merge", "Shell", "Quick")
y_pos = np.arange(len(objects))
performance = result[1:]
plt.bar(y_pos, performance, align='center', alpha=0.2)
plt.xticks(y_pos, objects)
plt.ylabel('Time')
plt.title('Sorting Performance on Data Set 10 ** ' + str(result[0]))
plt.show()