-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalgorithms.py
More file actions
149 lines (127 loc) · 4.08 KB
/
Copy pathalgorithms.py
File metadata and controls
149 lines (127 loc) · 4.08 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
from visualizer import update
def bubble_sort(screen, lst):
"""Bubble sort implementation in Python."""
for i in range(len(lst)):
for j in range(len(lst) - i - 1):
if lst[j] > lst[j + 1]:
lst[j], lst[j + 1] = lst[j + 1], lst[j]
update(screen, lst.copy(), {j, j + 1, len(lst) - i})
update(screen, lst.copy(), {})
def selection_sort(screen, lst):
"""Selection sort implementation in Python."""
for i in range(len(lst)):
smallest = i
for j in range(i + 1, len(lst)):
if lst[j] < lst[smallest]:
smallest = j
update(screen, lst.copy(), {i - 1, j, smallest})
lst[smallest], lst[i] = lst[i], lst[smallest]
update(screen, lst.copy(), {i - 1, i, smallest})
update(screen, lst.copy(), {})
def insertion_sort(screen, lst):
"""Insertion sort implementation in Python."""
for i in range(1, len(lst)):
key = lst[i]
j = i - 1
while j >= 0 and lst[j] > key:
lst[j + 1], lst[j] = lst[j], lst[j + 1]
update(screen, lst.copy(), {i, j, j + 1})
j -= 1
lst[j + 1], key = key, lst[j + 1]
update(screen, lst.copy(), {i, j + 1})
update(screen, lst.copy(), {})
def merge_sort(screen, lst):
"""Used to call merge sort."""
_merge_sort(screen, lst, 0, len(lst) - 1)
update(screen, lst.copy(), {})
def _merge_sort(screen, lst, low, high):
"""Merge sort implementation in Python."""
if low >= high:
update(screen, lst.copy(), {})
else:
mid = (low + high) // 2
_merge_sort(screen, lst, low, mid)
_merge_sort(screen, lst, mid + 1, high)
_merge(screen, lst, low, mid, high)
def _merge(screen, lst, low, mid, high):
"""Helper function for merge sort."""
merged = []
i = low
j = mid + 1
while i <= mid and j <= high:
if lst[i] < lst[j]:
merged.append(lst[i])
update(screen, lst.copy(), {i, j})
i += 1
else:
merged.append(lst[j])
update(screen, lst.copy(), {i, j})
j += 1
while i <= mid:
merged.append(lst[i])
update(screen, lst.copy(), {i})
i += 1
while j <= high:
merged.append(lst[j])
update(screen, lst.copy(), {j})
j += 1
for k in range(len(merged)):
lst[k + low] = merged[k]
update(screen, lst.copy(), {k + low})
def quick_sort(screen, lst):
"""Used to call quick sort."""
_quick_sort(screen, lst, 0, len(lst) - 1)
update(screen, lst.copy(), {})
def _quick_sort(screen, lst, low, high):
"""Quick sort implementation in Python."""
if low >= high:
update(screen, lst.copy(), {})
else:
pivot = _partition(screen, lst, low, high)
_quick_sort(screen, lst, low, pivot)
_quick_sort(screen, lst, pivot + 1, high)
def _partition(screen, lst, low, high):
"""Helper function for quick sort."""
pivot = lst[low]
i = low
j = high
while True:
while lst[i] < pivot:
update(screen, lst.copy(), {i, j, low})
i += 1
while lst[j] > pivot:
update(screen, lst.copy(), {i, j, low})
j -= 1
if i >= j:
return j
lst[i], lst[j] = lst[j], lst[i]
update(screen, lst.copy(), {i, j, low})
i += 1
j -= 1
def heap_sort(screen, lst):
"""Heap sort implementation in Python."""
for i in range(len(lst) // 2 - 1, -1, -1):
_heapify(screen, lst, len(lst), i)
for i in range(len(lst) - 1, 0, -1):
lst[i], lst[0] = lst[0], lst[i]
update(screen, lst.copy(), {0, i})
_heapify(screen, lst, i, 0)
update(screen, lst.copy(), {})
def _heapify(screen, lst, size, i):
"""Helper function for heap sort."""
largest = i
left = 2 * i + 1
right = 2 * i + 2
update(screen, lst.copy(), {size, largest})
if left < size:
if lst[largest] < lst[left]:
largest = left
update(screen, lst.copy(), {size, largest, left})
if right < size:
if lst[largest] < lst[right]:
largest = right
update(screen, lst.copy(), {size, largest, right})
if largest != i:
lst[i], lst[largest] = lst[largest], lst[i]
update(screen, lst.copy(), {size, largest, i})
_heapify(screen, lst, size, largest)