- Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsorting_time_comparison.py
96 lines (85 loc) · 3.77 KB
/
sorting_time_comparison.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
importrandom
importtime
fromtypingimportCallable, Optional
importmatplotlib.pyplotasplt
fromsortingimportSorting
classSortingTimeComparison:
def__init__(self, print_scores_to_console: bool=True, plot_graph: bool=True,
print_total_runtime_to_console: bool=True,
no_of_elements: Optional[list[int]] =None) ->None:
self.__start_time: float=time.time()
self.__no_of_elements: list[int] =no_of_elements
ifself.__no_of_elementsisNone:
self.__no_of_elements= [
250, 500, 3000,
5000, 7000,
10000, 15000, 20000,
# 100000, 1000000
]
self.__algorithms: list[list[float]] = [[], [], [], [], []]
self.__legends: list[str] = ['Quick', 'Merge', 'Selection', 'Insertion', 'Bubble']
self.__time_taken: list[dict[str, float]] = [
{'quick_sort': 0, 'merge_sort': 0, 'selection_sort': 0, 'insertion_sort': 0, 'bubble_sort': 0}
for_inrange(len(self.__no_of_elements))]
self.__index: int=0
self.__run_algorithms()
ifplot_graph:
self.plot_graph()
ifprint_scores_to_console:
self.print_scores()
ifprint_total_runtime_to_console:
print(f'Total Runtime: {time.time() -self.__start_time} seconds')
def__timed_sort(self, sort: Callable[[list[float]], list[float]], unsorted_list: list[float]) ->list[float]:
start: float=time.time()
result: list[float] =sort(unsorted_list)
duration: float=time.time() -start
self.__time_taken[self.__index][str(sort.__name__)] =duration
returnresult
def__run_algorithms(self) ->None:
fornumberinself.__no_of_elements:
test_list: list[int] = [random.randint(-1000000, 1000000) for_inrange(number)]
self.__timed_sort(Sorting.quick_sort, test_list)
self.__timed_sort(Sorting.merge_sort, test_list)
self.__timed_sort(Sorting.selection_sort, test_list)
self.__timed_sort(Sorting.insertion_sort, test_list)
self.__timed_sort(Sorting.bubble_sort, test_list)
self.__index+=1
self.__save_scores()
def__save_scores(self) ->None:
j: int=0
fordictionaryinself.__time_taken:
i: int=0
j+=1
forkey, valueindictionary.items():
ifi%5==0:
i=0
self.__algorithms[i].append(value)
i+=1
defprint_scores(self) ->None:
j: int=0
fordictionaryinself.__time_taken:
i: int=0
print('Number of elements= ', self.__no_of_elements[j], end=' : ')
j+=1
forkey, valueindictionary.items():
ifi%5==0:
i=0
i+=1
print(key.replace('_', ' ').capitalize(), ' : ', value, end=', ')
print()
defplot_graph(self) ->None:
fig: plt.Figure=plt.figure()
fig.add_subplot(1, 1, 1)
plt.xlabel("Number of Elements")
plt.ylabel("Time in Seconds")
plt.plot(self.__no_of_elements, self.__algorithms[0], 'go-')
plt.plot(self.__no_of_elements, self.__algorithms[1], 'co-')
plt.plot(self.__no_of_elements, self.__algorithms[2], 'bo-')
plt.plot(self.__no_of_elements, self.__algorithms[3], 'mo-')
plt.plot(self.__no_of_elements, self.__algorithms[4], 'ro-')
plt.legend(self.__legends)
plt.show()
if__name__=='__main__':
sorting_time_comparison=SortingTimeComparison(print_scores_to_console=False, plot_graph=False)
sorting_time_comparison.print_scores()
sorting_time_comparison.plot_graph()