|
| 1 | +import random |
| 2 | +import time |
| 3 | +from typing import Callable, Optional |
| 4 | + |
| 5 | +import matplotlib.pyplot as plt |
| 6 | + |
| 7 | +from sorting import Sorting |
| 8 | + |
| 9 | + |
| 10 | +class SortingTimeComparison: |
| 11 | + def __init__(self, print_scores_to_console: bool = True, plot_graph: bool = True, |
| 12 | + print_total_runtime_to_console: bool = True, |
| 13 | + no_of_elements: Optional[list[int]] = None) -> None: |
| 14 | + self.__start_time: float = time.time() |
| 15 | + self.__no_of_elements: list[int] = no_of_elements |
| 16 | + if self.__no_of_elements is None: |
| 17 | + self.__no_of_elements = [ |
| 18 | + 250, 500, 3000, |
| 19 | + 5000, 7000, |
| 20 | + 10000, 15000, 20000, |
| 21 | + # 100000, 1000000 |
| 22 | + ] |
| 23 | + self.__algorithms: list[list[float]] = [[], [], [], [], []] |
| 24 | + self.__legends: list[str] = ['Quick', 'Merge', 'Selection', 'Insertion', 'Bubble'] |
| 25 | + self.__time_taken: list[dict[str, float]] = [ |
| 26 | + {'quick_sort': 0, 'merge_sort': 0, 'selection_sort': 0, 'insertion_sort': 0, 'bubble_sort': 0} |
| 27 | + for _ in range(len(self.__no_of_elements))] |
| 28 | + self.__index: int = 0 |
| 29 | + self.__run_algorithms() |
| 30 | + if plot_graph: |
| 31 | + self.plot_graph() |
| 32 | + if print_scores_to_console: |
| 33 | + self.print_scores() |
| 34 | + if print_total_runtime_to_console: |
| 35 | + print(f'Total Runtime: {time.time() - self.__start_time} seconds') |
| 36 | + |
| 37 | + def __timed_sort(self, sort: Callable[[list[float]], list[float]], unsorted_list: list[float]) -> list[float]: |
| 38 | + start: float = time.time() |
| 39 | + result: list[float] = sort(unsorted_list) |
| 40 | + duration: float = time.time() - start |
| 41 | + self.__time_taken[self.__index][str(sort.__name__)] = duration |
| 42 | + return result |
| 43 | + |
| 44 | + def __run_algorithms(self) -> None: |
| 45 | + for number in self.__no_of_elements: |
| 46 | + test_list: list[int] = [random.randint(-1000000, 1000000) for _ in range(number)] |
| 47 | + self.__timed_sort(Sorting.quick_sort, test_list) |
| 48 | + self.__timed_sort(Sorting.merge_sort, test_list) |
| 49 | + self.__timed_sort(Sorting.selection_sort, test_list) |
| 50 | + self.__timed_sort(Sorting.insertion_sort, test_list) |
| 51 | + self.__timed_sort(Sorting.bubble_sort, test_list) |
| 52 | + self.__index += 1 |
| 53 | + self.__save_scores() |
| 54 | + |
| 55 | + def __save_scores(self) -> None: |
| 56 | + j: int = 0 |
| 57 | + for dictionary in self.__time_taken: |
| 58 | + i: int = 0 |
| 59 | + j += 1 |
| 60 | + for key, value in dictionary.items(): |
| 61 | + if i % 5 == 0: |
| 62 | + i = 0 |
| 63 | + self.__algorithms[i].append(value) |
| 64 | + i += 1 |
| 65 | + |
| 66 | + def print_scores(self) -> None: |
| 67 | + j: int = 0 |
| 68 | + for dictionary in self.__time_taken: |
| 69 | + i: int = 0 |
| 70 | + print('Number of elements= ', self.__no_of_elements[j], end=' : ') |
| 71 | + j += 1 |
| 72 | + for key, value in dictionary.items(): |
| 73 | + if i % 5 == 0: |
| 74 | + i = 0 |
| 75 | + i += 1 |
| 76 | + print(key.replace('_', ' ').capitalize(), ' : ', value, end=', ') |
| 77 | + print() |
| 78 | + |
| 79 | + def plot_graph(self) -> None: |
| 80 | + fig: plt.Figure = plt.figure() |
| 81 | + fig.add_subplot(1, 1, 1) |
| 82 | + plt.xlabel("Number of Elements") |
| 83 | + plt.ylabel("Time in Seconds") |
| 84 | + plt.plot(self.__no_of_elements, self.__algorithms[0], 'go-') |
| 85 | + plt.plot(self.__no_of_elements, self.__algorithms[1], 'co-') |
| 86 | + plt.plot(self.__no_of_elements, self.__algorithms[2], 'bo-') |
| 87 | + plt.plot(self.__no_of_elements, self.__algorithms[3], 'mo-') |
| 88 | + plt.plot(self.__no_of_elements, self.__algorithms[4], 'ro-') |
| 89 | + plt.legend(self.__legends) |
| 90 | + plt.show() |
| 91 | + |
| 92 | + |
| 93 | +if __name__ == '__main__': |
| 94 | + sorting_time_comparison = SortingTimeComparison(print_scores_to_console=False, plot_graph=False) |
| 95 | + sorting_time_comparison.print_scores() |
| 96 | + sorting_time_comparison.plot_graph() |
0 commit comments