|
| 1 | +from timeit import default_timer as timer |
| 2 | +import random |
| 3 | + |
| 4 | + |
| 5 | +def insertion_sort(list_to_sort): |
| 6 | + for step in range(1, len(list_to_sort)): |
| 7 | + key = list_to_sort[step] |
| 8 | + j = step - 1 |
| 9 | + while j >= 0 and list_to_sort[j] > key: |
| 10 | + list_to_sort[j + 1] = list_to_sort[j] |
| 11 | + j -= 1 |
| 12 | + list_to_sort[j + 1] = key |
| 13 | + |
| 14 | + |
| 15 | +def quick_sort(list_to_sort): |
| 16 | + def partition(sub_list, low, hi): |
| 17 | + pivot = sub_list[(low + hi) // 2] |
| 18 | + left = low |
| 19 | + right = hi |
| 20 | + while left <= right: |
| 21 | + while sub_list[left] < pivot: |
| 22 | + left += 1 |
| 23 | + while sub_list[right] > pivot: |
| 24 | + right -= 1 |
| 25 | + if left <= right: |
| 26 | + sub_list[left], sub_list[right] = sub_list[right], sub_list[left] |
| 27 | + left += 1 |
| 28 | + right -= 1 |
| 29 | + return left, right |
| 30 | + |
| 31 | + def quick_sort_fun(list_to_sort, low, hi): |
| 32 | + if low < hi: |
| 33 | + left, right = partition(list_to_sort, low, hi) |
| 34 | + quick_sort_fun(list_to_sort, low, right) |
| 35 | + quick_sort_fun(list_to_sort, left, hi) |
| 36 | + |
| 37 | + quick_sort_fun(list_to_sort, 0, len(list_to_sort) - 1) |
| 38 | + |
| 39 | + |
| 40 | +# shell sort using Knuth's sequence |
| 41 | +def shell_sort(list_to_sort): |
| 42 | + def sublist_sort(list_to_sort, start_index, gap): |
| 43 | + for i in range(start_index + gap, len(list_to_sort), gap): |
| 44 | + current_val = list_to_sort[i] |
| 45 | + index = i |
| 46 | + while index >= gap and list_to_sort[index - gap] > current_val: |
| 47 | + list_to_sort[index] = list_to_sort[index - gap] |
| 48 | + index -= gap |
| 49 | + list_to_sort[index] = current_val |
| 50 | + |
| 51 | + n = len(list_to_sort) |
| 52 | + gap = 1 |
| 53 | + while gap < n // 3: |
| 54 | + gap = 3 * gap + 1 |
| 55 | + while gap > 0: |
| 56 | + for i in range(gap): |
| 57 | + sublist_sort(list_to_sort, i, gap) |
| 58 | + gap //= 3 |
| 59 | + |
| 60 | + |
| 61 | +def heap_sort(list_to_sort): |
| 62 | + def heapify(list_to_sort, n, i): |
| 63 | + largest_index = i |
| 64 | + left_index = 2 * i + 1 |
| 65 | + right_index = 2 * i + 2 |
| 66 | + |
| 67 | + if left_index < n and list_to_sort[i] < list_to_sort[left_index]: |
| 68 | + largest_index = left_index |
| 69 | + if right_index < n and list_to_sort[largest_index] < list_to_sort[right_index]: |
| 70 | + largest_index = right_index |
| 71 | + if largest_index != i: |
| 72 | + list_to_sort[i], list_to_sort[largest_index] = list_to_sort[largest_index], list_to_sort[i] |
| 73 | + heapify(list_to_sort, n, largest_index) |
| 74 | + |
| 75 | + n = len(list_to_sort) |
| 76 | + for i in range(n, -1, -1): |
| 77 | + heapify(list_to_sort, n, i) |
| 78 | + for i in range(n - 1, 0, -1): |
| 79 | + list_to_sort[i], list_to_sort[0] = list_to_sort[0], list_to_sort[i] |
| 80 | + heapify(list_to_sort, i, 0) |
| 81 | + |
| 82 | + |
| 83 | +def benchmark_sorting_algorithms(functions_list, list_to_sort): |
| 84 | + print("BENCHMARK START (Times in ms)") |
| 85 | + |
| 86 | + def benchmark_one_function(function_to_benchmark, *arguments): |
| 87 | + start = timer() |
| 88 | + function_to_benchmark(*arguments) |
| 89 | + end = timer() |
| 90 | + print("{0}: {1}".format(function_to_benchmark.__name__, (end - start) * 1000)) |
| 91 | + |
| 92 | + for function in functions_list: |
| 93 | + benchmark_one_function(function, list_to_sort.copy()) |
| 94 | + |
| 95 | + |
| 96 | +def main(): |
| 97 | + list_to_sort_1 = [random.randint(-10000, 10000) for _ in range(10000)] |
| 98 | + list_to_sort_2 = list(range(10000)) |
| 99 | + list_to_sort_3 = list(range(10000, 0, -1)) |
| 100 | + |
| 101 | + print("Random list") |
| 102 | + benchmark_sorting_algorithms([quick_sort, heap_sort, shell_sort, insertion_sort], list_to_sort_1) |
| 103 | + print("Increasing list") |
| 104 | + benchmark_sorting_algorithms([quick_sort, heap_sort, shell_sort, insertion_sort], list_to_sort_2) |
| 105 | + print("Decreasing list") |
| 106 | + benchmark_sorting_algorithms([quick_sort, heap_sort, shell_sort, insertion_sort], list_to_sort_3) |
| 107 | + |
| 108 | + |
| 109 | +if __name__ == "__main__": |
| 110 | + main() |
0 commit comments