/
quicksort.py
181 lines (137 loc) · 5.14 KB
/
quicksort.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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import numpy as np
from IPython.display import clear_output
from ipywidgets import *
from step import StepExample
from pjdiagram import *
import math
def drawArrowHead(ctx, x, y, x2, y2, arrow_length, arrow_degrees):
dx = x2 - x
dy = y2 - y
angle = math.atan2(dy, dx)
hx = x2 - arrow_length * math.cos(angle - arrow_degrees)
hy = y2 - arrow_length * math.sin(angle - arrow_degrees)
hx2 = x2 - arrow_length * math.cos(angle + arrow_degrees)
hy2 = y2 - arrow_length * math.sin(angle + arrow_degrees)
ctx.move_to(x2, y2)
ctx.line_to(hx, hy)
ctx.line_to(hx2, hy2)
ctx.close_path()
ctx.stroke_preserve()
ctx.fill()
sx = x2 - arrow_length * math.cos(angle)
sy = y2 - arrow_length * math.sin(angle)
ctx.move_to(sx, sy)
ctx.line_to(x, y)
ctx.stroke()
def draw_partition(ctx, pos, contents, pivot, swapA, swapB, cell_width=20, cell_height=20):
draw_array(ctx, pos, contents, highlight={swapA:True, swapB:True})
ax = cell_width*pivot + cell_width/2.0 + pos[0]
ay = cell_height + pos[1] + 5
ctx.set_source_rgb(0, 0, 0)
ctx.set_line_width(1.0)
drawArrowHead(ctx, ax, ay + 15, ax, ay, 10, math.radians(25))
def partition(lst, start, end):
pivot = lst[start]
first = start + 1
last = end
while first <= last:
# find the next element that is less than pivot
while first <= last and lst[first] <= pivot:
first += 1
# find the next element that is greater than pivot
while last >= first and lst[last] > pivot:
last -= 1
# and swap their values
if first < last:
lst[first], lst[last] = lst[last], lst[first]
lst[start], lst[last] = lst[last], lst[start]
return last
class PartitionExample(StepExample):
def __init__(self, lst, pivotIndex):
super(PartitionExample, self).__init__()
self.original = lst
self.data = list(self.original)
self.pivot = lst[pivotIndex]
self.first = 0
self.last = len(self.data) - 1
self.mode = 0
def drawCanvas(self, left=-1, right=-1):
with Canvas(600, 80) as ctx:
pivotIndex = self.data.index(self.pivot)
draw_partition(ctx, (1, 1), self.data, pivotIndex, left, right)
def on_reset(self, b):
self.data = list(self.original)
self.mode = 0
self.first = 0
self.last = len(self.data) - 1
clear_output(wait=True)
self.drawCanvas()
def on_next(self, b):
clear_output(wait=True)
if self.mode % 2 == 0:
while self.first < len(self.data)-1 and self.data[self.first] < self.pivot:
self.first += 1
# find the next element that is greater than pivot
while self.last > 0 and self.data[self.last] > self.pivot:
self.last -= 1
else:
self.data[self.first], self.data[self.last] = self.data[self.last], self.data[self.first]
self.drawCanvas(self.first, self.last)
self.mode += 1
def on_prev(self, b):
if len(self.prev_data) > 0:
clear_output(wait=True)
self.data = self.prev_data.pop()
self.drawCanvas()
def __call__(self):
self.drawCanvas()
self.next_button.on_click(self.on_next)
self.prev_button.on_click(self.on_prev)
self.reset_button.on_click(self.on_reset)
controls = HBox([self.prev_button, self.next_button, self.reset_button])
display(controls)
def partition_example(lst, pivotIndex):
return PartitionExample(lst, pivotIndex)()
class QuicksortExample(StepExample):
def __init__(self, lst):
super(QuicksortExample, self).__init__()
self.original = lst
self.data = list(self.original)
self.stack = []
self.stack.append((0, len(self.data)-1))
def drawCanvas(self, left=-1, right=-1, partition=-1):
with Canvas(600, 80) as ctx:
draw_partition(ctx, (1, 1), self.data, partition, left, right)
def on_reset(self, b):
self.data = list(self.original)
self.mode = 0
self.first = 0
self.last = len(self.data) - 1
clear_output(wait=True)
self.drawCanvas()
def on_next(self, b):
clear_output(wait=True)
# p = partition(lst, first, last)
first, last = self.stack.pop(0)
p = partition(self.data, first, last)
print(self.data)
print(p, first, last)
if (p - 1) - first > 1:
self.stack.append((first, p-1))
if last - (p+1) > 1:
self.stack.append((p+1, last))
self.drawCanvas(first, last, p)
def on_prev(self, b):
if len(self.prev_data) > 0:
clear_output(wait=True)
self.data = self.prev_data.pop()
self.drawCanvas()
def __call__(self):
self.drawCanvas()
self.next_button.on_click(self.on_next)
self.prev_button.on_click(self.on_prev)
self.reset_button.on_click(self.on_reset)
controls = HBox([self.prev_button, self.next_button, self.reset_button])
display(controls)
def quicksort_example(lst):
return QuicksortExample(lst)()