-
Notifications
You must be signed in to change notification settings - Fork 0
/
most_used_sort_algs.py
102 lines (75 loc) · 2.77 KB
/
most_used_sort_algs.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
import itertools
def merge_sort(lst: list) -> None:
def merge(initial: list, left: list, right: list) -> None:
left_idx = right_idx = init_idx = 0
while left_idx < len(left) and right_idx < len(right):
if left[left_idx] < right[right_idx]:
initial[init_idx] = left[left_idx]
left_idx += 1
else:
initial[init_idx] = right[right_idx]
right_idx += 1
init_idx += 1
while left_idx < len(left):
initial[init_idx] = left[left_idx]
left_idx += 1
init_idx += 1
while right_idx < len(right):
initial[init_idx] = right[right_idx]
right_idx += 1
init_idx += 1
if (list_length := len(lst)) == 1:
return
partition_on = list_length // 2
left, right = lst[partition_on:], lst[:partition_on]
merge_sort(left)
merge_sort(right)
merge(lst, left, right)
def quick_sort(lst: list) -> None:
def quick_sort_r(lst: list, start: int, end: int) -> None:
if start >= end:
return
pivot = partition(lst, start, end)
quick_sort_r(lst, start, pivot - 1)
quick_sort_r(lst, pivot + 1, end)
def partition(lst: list, start: int, end: int) -> int:
pivot = lst[end]
low_idx = start
for i in range(start, end):
if lst[i] < pivot:
lst[low_idx], lst[i] = lst[i], lst[low_idx]
low_idx += 1
lst[end], lst[low_idx] = lst[low_idx], lst[end]
return low_idx
quick_sort_r(lst, 0, len(lst) - 1)
def heap_sort(lst: list) -> None:
def heapify(lst: list, n: int, i: int) -> None:
largest_idx = i
left_child_idx = i * 2 + 1
right_child_idx = left_child_idx + 1
if left_child_idx < n and lst[largest_idx] < lst[left_child_idx]:
largest_idx = left_child_idx
if right_child_idx < n and lst[largest_idx] < lst[right_child_idx]:
largest_idx = right_child_idx
if largest_idx != i:
lst[i], lst[largest_idx] = lst[largest_idx], lst[i]
heapify(lst, n, largest_idx)
list_length = len(lst)
for i in range(list_length // 2 - 1, -1, -1):
heapify(lst, list_length, i)
for i in range(list_length - 1, 0, -1):
lst[i], lst[0] = lst[0], lst[i]
heapify(lst, i, 0)
def bubble_sort(lst: list) -> None:
it_count: int = 0
swapped: bool = True
list_length: int = len(lst)
while swapped:
swapped = False
for j in range(list_length - it_count - 1):
if lst[j + 1] < lst[j]:
lst[j + 1], lst[j] = lst[j], lst[j + 1]
swapped = True
it_count += 1
if not swapped:
break