-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalgorithms.py
42 lines (31 loc) · 1.28 KB
/
algorithms.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
from helpers import *
class Algorithms:
def bubble_sort(self, draw_info, ascending=True):
lst = draw_info.lst
for i in range(len(lst) - 1):
for j in range(len(lst) - 1 - i):
num1 = lst[j]
num2 = lst[j + 1]
if (num1 > num2 and ascending) or (num1 < num2 and not ascending):
lst[j], lst[j + 1] = lst[j + 1], lst[j]
draw_list(
draw_info, {j: draw_info.GREEN, j + 1: draw_info.RED}, True)
yield True
return lst
def insertion_sort(self, draw_info, ascending=True):
lst = draw_info.lst
for i in range(1, len(lst)):
current = lst[i]
while True:
ascending_sort = i > 0 and lst[i - 1] > current and ascending
descending_sort = i > 0 and lst[i -
1] < current and not ascending
if not ascending_sort and not descending_sort:
break
lst[i] = lst[i - 1]
i = i - 1
lst[i] = current
draw_list(
draw_info, {i - 1: draw_info.GREEN, i: draw_info.RED}, True)
yield True
return lst