-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsorting.py
104 lines (93 loc) · 3.9 KB
/
sorting.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
97
98
99
100
101
102
103
104
from typing import Optional
class Sorting:
@staticmethod
def quick_sort(unsorted_list: list[float]) -> list[float]:
sorted_list: list[float] = unsorted_list.copy()
Sorting.__quick_sort_implementation(sorted_list)
return sorted_list
@staticmethod
def __quick_sort_implementation(unsorted_list: list[float], low: Optional[int] = None,
high: Optional[int] = None) -> list[float]:
if low is None and high is None:
low = 0
high = len(unsorted_list) - 1
def partition(p_list: list[float], p_low: int, p_high: int) -> int:
i: int = p_low - 1
pivot: float = p_list[p_high]
for j in range(p_low, p_high):
if p_list[j] <= pivot:
i += 1
p_list[i], p_list[j] = p_list[j], p_list[i]
p_list[i + 1], p_list[p_high] = p_list[p_high], p_list[i + 1]
return i + 1
if len(unsorted_list) == 1:
return unsorted_list
if low < high:
p_i: int = partition(unsorted_list, low, high)
Sorting.__quick_sort_implementation(unsorted_list, low=low, high=p_i - 1)
Sorting.__quick_sort_implementation(unsorted_list, low=p_i + 1, high=high)
@staticmethod
def merge_sort(unsorted_list: list[float]) -> list[float]:
sorted_list: list[float] = unsorted_list.copy()
if len(sorted_list) > 1:
mid: int = len(sorted_list) // 2
left: list[float] = Sorting.merge_sort(sorted_list[:mid])
right: list[float] = Sorting.merge_sort(sorted_list[mid:])
i: int = 0
j: int = 0
k: int = 0
while i < len(left) and j < len(right):
if left[i] < right[j]:
sorted_list[k] = left[i]
i += 1
else:
sorted_list[k] = right[j]
j += 1
k += 1
while i < len(left):
sorted_list[k] = left[i]
i += 1
k += 1
while j < len(right):
sorted_list[k] = right[j]
j += 1
k += 1
return sorted_list
@staticmethod
def selection_sort(unsorted_list: list[float]) -> list[float]:
sorted_list: list[float] = unsorted_list.copy()
for i in range(len(sorted_list)):
min_idx: int = i
for j in range(i + 1, len(sorted_list)):
if sorted_list[min_idx] > sorted_list[j]:
min_idx = j
sorted_list[i], sorted_list[min_idx] = sorted_list[min_idx], sorted_list[i]
return sorted_list
@staticmethod
def insertion_sort(unsorted_list: list[float]) -> list[float]:
sorted_list: list[float] = unsorted_list.copy()
for i in range(1, len(sorted_list)):
key: float = sorted_list[i]
j: int = i - 1
while j >= 0 and key < sorted_list[j]:
sorted_list[j + 1] = sorted_list[j]
j -= 1
sorted_list[j + 1] = key
return sorted_list
@staticmethod
def bubble_sort(unsorted_list: list[float]) -> list[float]:
sorted_list: list[float] = unsorted_list.copy()
for i in range(len(sorted_list) - 1):
for j in range(0, len(sorted_list) - i - 1):
if sorted_list[j] > sorted_list[j + 1]:
sorted_list[j], sorted_list[j + 1] = sorted_list[j + 1], sorted_list[j]
return sorted_list
if __name__ == '__main__':
array = [9, 100.55, 100.46, 8, 1, 9999, 5]
print("Before:", array)
print("Quick:", Sorting.quick_sort(array))
print("Merge:", Sorting.merge_sort(array))
print("Selection:", Sorting.selection_sort(array))
print("Insertion:", Sorting.insertion_sort(array))
print("Bubble:", Sorting.bubble_sort(array))
print("After:", array)