|
| 1 | +""" |
| 2 | +One of reasons for learning about algorithms is |
| 3 | +that they can affect the efficiency of a program. |
| 4 | +Therefore, it will be helpful for learners |
| 5 | +to see each algorithm's sorting process speed |
| 6 | +and compare the results of different algorithms. |
| 7 | +
|
| 8 | +This function, stopwatch_sort, will return a list of sorting results, |
| 9 | +so it can be used to see how long each sorting algorithm |
| 10 | +takes to complete the sorting process. |
| 11 | +""" |
| 12 | + |
| 13 | +import random |
| 14 | +import time |
| 15 | + |
| 16 | +from binary_insertion_sort import binary_insertion_sort |
| 17 | +from bubble_sort import bubble_sort_iterative |
| 18 | +from bucket_sort import bucket_sort |
| 19 | +from counting_sort import counting_sort |
| 20 | +from heap_sort import heap_sort |
| 21 | +from merge_sort import merge_sort |
| 22 | +from quick_sort import quick_sort |
| 23 | +from radix_sort import radix_sort |
| 24 | +from selection_sort import selection_sort |
| 25 | + |
| 26 | + |
| 27 | +def stopwatch_sort(func_list: list, number_of_integers: int = 10000) -> list: |
| 28 | + """ |
| 29 | + implementation of comparing sorting algorithms |
| 30 | + :param func_list: list of sorting functions |
| 31 | + :param number_of_integers: number of randomly chosen integers to sort |
| 32 | + :return: list of results, where each result is a list including: |
| 33 | + the function name, the number of sorted integers, |
| 34 | + whether the integers were sorted properly, |
| 35 | + and milliseconds taken to complete the sorting process. |
| 36 | +
|
| 37 | + For example, when the following code is executed: |
| 38 | + results = stopwatch_sort([binary_insertion_sort, bubble_sort_iterative], 8000) |
| 39 | +
|
| 40 | + The results will be similar to: |
| 41 | + [['binary_insertion_sort', 8000, True, 2186.258316040039], |
| 42 | + ['bubble_sort_iterative', 8000, True, 7760.7762813568115]] |
| 43 | +
|
| 44 | + Examples: |
| 45 | + >>> first_results = stopwatch_sort([binary_insertion_sort], 5000) |
| 46 | + >>> len(first_results) |
| 47 | + 1 |
| 48 | + >>> len(first_results[0]) |
| 49 | + 4 |
| 50 | + >>> first_results[0][0] |
| 51 | + 'binary_insertion_sort' |
| 52 | + >>> first_results[0][1] |
| 53 | + 5000 |
| 54 | + >>> first_results[0][2] |
| 55 | + True |
| 56 | + >>> float(first_results[0][3]) >= 0 |
| 57 | + True |
| 58 | + >>> second_results = stopwatch_sort([binary_insertion_sort, merge_sort]) |
| 59 | + >>> len(second_results) |
| 60 | + 2 |
| 61 | + >>> len(second_results[1]) |
| 62 | + 4 |
| 63 | + >>> second_results[1][0] |
| 64 | + 'merge_sort' |
| 65 | + >>> second_results[1][1] |
| 66 | + 10000 |
| 67 | + >>> second_results[1][2] |
| 68 | + True |
| 69 | + >>> float(second_results[1][3]) >= 0 |
| 70 | + True |
| 71 | + """ |
| 72 | + |
| 73 | + range_multiplier = 2 |
| 74 | + int_range = ( |
| 75 | + number_of_integers * range_multiplier |
| 76 | + ) # Extendable range of random choice |
| 77 | + input_integers = [random.randint(0, int_range) for _ in range(number_of_integers)] |
| 78 | + sorted_integers = sorted(input_integers) |
| 79 | + |
| 80 | + result_list = [] |
| 81 | + |
| 82 | + for func in func_list: |
| 83 | + # To prevent input_integers from being changed by function |
| 84 | + instance_integers = input_integers.copy() |
| 85 | + |
| 86 | + # Record the start and end time of sorting |
| 87 | + start_time = time.time() |
| 88 | + sorted_numbers = func(instance_integers) |
| 89 | + end_time = time.time() |
| 90 | + |
| 91 | + # Each result consists of four elements |
| 92 | + func_name = func.__name__ |
| 93 | + length_of_sorted_numbers = len(sorted_numbers) |
| 94 | + properly_sorted = sorted_numbers == sorted_integers |
| 95 | + process_time_milliseconds = (end_time - start_time) * 1000 |
| 96 | + process_result = [ |
| 97 | + func_name, |
| 98 | + length_of_sorted_numbers, |
| 99 | + properly_sorted, |
| 100 | + process_time_milliseconds, |
| 101 | + ] |
| 102 | + result_list.append(process_result) |
| 103 | + |
| 104 | + return result_list |
| 105 | + |
| 106 | + |
| 107 | +if __name__ == "__main__": |
| 108 | + user_input = input("Enter how many random numbers to be sorted: ") |
| 109 | + user_input_int = int(user_input) |
| 110 | + algorithm_list = [ |
| 111 | + binary_insertion_sort, |
| 112 | + bubble_sort_iterative, |
| 113 | + bucket_sort, |
| 114 | + counting_sort, |
| 115 | + heap_sort, |
| 116 | + merge_sort, |
| 117 | + quick_sort, |
| 118 | + radix_sort, |
| 119 | + selection_sort, |
| 120 | + ] |
| 121 | + |
| 122 | + results = stopwatch_sort(algorithm_list, user_input_int) |
| 123 | + for result in results: |
| 124 | + print(result) |
0 commit comments