|
| 1 | +from typing import Optional |
| 2 | + |
| 3 | + |
| 4 | +class Sorting: |
| 5 | + @staticmethod |
| 6 | + def quick_sort(unsorted_list: list[float]) -> list[float]: |
| 7 | + sorted_list: list[float] = unsorted_list.copy() |
| 8 | + Sorting.__quick_sort_implementation(sorted_list) |
| 9 | + return sorted_list |
| 10 | + |
| 11 | + @staticmethod |
| 12 | + def __quick_sort_implementation(unsorted_list: list[float], low: Optional[int] = None, |
| 13 | + high: Optional[int] = None) -> list[float]: |
| 14 | + if low is None and high is None: |
| 15 | + low = 0 |
| 16 | + high = len(unsorted_list) - 1 |
| 17 | + |
| 18 | + def partition(p_list: list[float], p_low: int, p_high: int) -> int: |
| 19 | + i: int = p_low - 1 |
| 20 | + pivot: float = p_list[p_high] |
| 21 | + for j in range(p_low, p_high): |
| 22 | + if p_list[j] <= pivot: |
| 23 | + i += 1 |
| 24 | + p_list[i], p_list[j] = p_list[j], p_list[i] |
| 25 | + p_list[i + 1], p_list[p_high] = p_list[p_high], p_list[i + 1] |
| 26 | + return i + 1 |
| 27 | + |
| 28 | + if len(unsorted_list) == 1: |
| 29 | + return unsorted_list |
| 30 | + if low < high: |
| 31 | + p_i: int = partition(unsorted_list, low, high) |
| 32 | + Sorting.__quick_sort_implementation(unsorted_list, low=low, high=p_i - 1) |
| 33 | + Sorting.__quick_sort_implementation(unsorted_list, low=p_i + 1, high=high) |
| 34 | + |
| 35 | + @staticmethod |
| 36 | + def merge_sort(unsorted_list: list[float]) -> list[float]: |
| 37 | + sorted_list: list[float] = unsorted_list.copy() |
| 38 | + if len(sorted_list) > 1: |
| 39 | + mid: int = len(sorted_list) // 2 |
| 40 | + left: list[float] = Sorting.merge_sort(sorted_list[:mid]) |
| 41 | + right: list[float] = Sorting.merge_sort(sorted_list[mid:]) |
| 42 | + i: int = 0 |
| 43 | + j: int = 0 |
| 44 | + k: int = 0 |
| 45 | + while i < len(left) and j < len(right): |
| 46 | + if left[i] < right[j]: |
| 47 | + sorted_list[k] = left[i] |
| 48 | + i += 1 |
| 49 | + else: |
| 50 | + sorted_list[k] = right[j] |
| 51 | + j += 1 |
| 52 | + k += 1 |
| 53 | + while i < len(left): |
| 54 | + sorted_list[k] = left[i] |
| 55 | + i += 1 |
| 56 | + k += 1 |
| 57 | + while j < len(right): |
| 58 | + sorted_list[k] = right[j] |
| 59 | + j += 1 |
| 60 | + k += 1 |
| 61 | + return sorted_list |
| 62 | + |
| 63 | + @staticmethod |
| 64 | + def selection_sort(unsorted_list: list[float]) -> list[float]: |
| 65 | + sorted_list: list[float] = unsorted_list.copy() |
| 66 | + for i in range(len(sorted_list)): |
| 67 | + min_idx: int = i |
| 68 | + for j in range(i + 1, len(sorted_list)): |
| 69 | + if sorted_list[min_idx] > sorted_list[j]: |
| 70 | + min_idx = j |
| 71 | + sorted_list[i], sorted_list[min_idx] = sorted_list[min_idx], sorted_list[i] |
| 72 | + return sorted_list |
| 73 | + |
| 74 | + @staticmethod |
| 75 | + def insertion_sort(unsorted_list: list[float]) -> list[float]: |
| 76 | + sorted_list: list[float] = unsorted_list.copy() |
| 77 | + for i in range(1, len(sorted_list)): |
| 78 | + key: float = sorted_list[i] |
| 79 | + j: int = i - 1 |
| 80 | + while j >= 0 and key < sorted_list[j]: |
| 81 | + sorted_list[j + 1] = sorted_list[j] |
| 82 | + j -= 1 |
| 83 | + sorted_list[j + 1] = key |
| 84 | + return sorted_list |
| 85 | + |
| 86 | + @staticmethod |
| 87 | + def bubble_sort(unsorted_list: list[float]) -> list[float]: |
| 88 | + sorted_list: list[float] = unsorted_list.copy() |
| 89 | + for i in range(len(sorted_list) - 1): |
| 90 | + for j in range(0, len(sorted_list) - i - 1): |
| 91 | + if sorted_list[j] > sorted_list[j + 1]: |
| 92 | + sorted_list[j], sorted_list[j + 1] = sorted_list[j + 1], sorted_list[j] |
| 93 | + return sorted_list |
| 94 | + |
| 95 | + |
| 96 | +if __name__ == '__main__': |
| 97 | + array = [9, 100.55, 100.46, 8, 1, 9999, 5] |
| 98 | + print("Before:", array) |
| 99 | + print("Quick:", Sorting.quick_sort(array)) |
| 100 | + print("Merge:", Sorting.merge_sort(array)) |
| 101 | + print("Selection:", Sorting.selection_sort(array)) |
| 102 | + print("Insertion:", Sorting.insertion_sort(array)) |
| 103 | + print("Bubble:", Sorting.bubble_sort(array)) |
| 104 | + print("After:", array) |
0 commit comments