-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsorting.py
78 lines (64 loc) · 2.11 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
def draw_list(L: list) -> None:
import os
# fixes ANSI codes on Windows
# an explanation can be found here: https://bugs.python.org/msg291732
os.system('')
print("\033[H", end='') # move cursor to (0,0)
print("\033[0J", end='') # erase screen
output = ""
M = max(L)
for line_index in range(1, M+1):
line = ""
for height in L:
if line_index <= height:
line += '|'
else:
line += ' '
line += '\n'
output = line + output
print(output)
def bubble_sort(L: list, draw: bool = False) -> None:
for k in range(1, len(L)):
for index in range(len(L)-k):
if L[index] > L[index+1]:
L[index], L[index+1] = L[index+1], L[index]
if draw:
draw_list(L)
def _insert(L: list, index: int, draw: bool = False) -> None:
key = L[index]
while index > -1 and key <= L[index]:
L[index] = L[index-1]
index -= 1
if draw:
draw_list(L)
L[index+1] = key
if draw:
draw_list(L)
def insert_sort(L: list, draw: bool = False) -> None:
for ind in range(len(L)):
_insert(L, ind, draw)
def _selection(L: list, begin: int, draw: bool = False) -> int:
imin = begin
for i in range(begin, len(L)):
if L[i] < L[imin]:
imin = i
if draw:
draw_list(L)
return imin
def selection_sort(L: list, draw: bool = False) -> None:
for i in range(len(L)):
imin = _selection(L, i, draw)
L[i], L[imin] = L[imin], L[i]
def quick_sort(L: list) -> list:
# this one doesn't get the luxury of visual representation
# because I can't figure out how to do that with a recursive algorithm without summoning some eldritch horrors along the way
if len(L) < 2:
return L
pivot = L[-1]
l, r = [], []
for element in L[:-1]:
if element < pivot:
l += [element]
else:
r += [element]
return quick_sort(l) + [pivot] + quick_sort(r)