Initial commit
[sort-animations.git] / insertion-sort.ipynb
1 {
2 "cells": [
3 {
4 "cell_type": "code",
5 "execution_count": 1,
6 "metadata": {},
7 "outputs": [],
8 "source": [
9 "import random\n",
10 "import time\n",
11 "import re\n",
12 "from IPython.display import clear_output\n",
13 "from PIL import Image, ImageDraw, ImageColor, ImageFont"
14 ]
15 },
16 {
17 "cell_type": "code",
18 "execution_count": 58,
19 "metadata": {},
20 "outputs": [],
21 "source": [
22 "ROWS = 300\n",
23 "COLUMNS = 720\n",
24 "RANGE = 360"
25 ]
26 },
27 {
28 "cell_type": "code",
29 "execution_count": 59,
30 "metadata": {},
31 "outputs": [],
32 "source": [
33 "initial_items = [int((float(i) * RANGE) / float(COLUMNS)) for i in range(COLUMNS)]"
34 ]
35 },
36 {
37 "cell_type": "code",
38 "execution_count": 4,
39 "metadata": {},
40 "outputs": [],
41 "source": [
42 "def draw_frame(rows, frame_number, prefix='frame'):\n",
43 " im = Image.new('RGB', (COLUMNS * 1, ROWS * 1))\n",
44 "\n",
45 " draw = ImageDraw.Draw(im)\n",
46 " for r in range(len(rows)):\n",
47 " if frame_number >= len(rows[r]):\n",
48 " row = rows[r][-1]\n",
49 " else:\n",
50 " row = rows[r][frame_number]\n",
51 "# for (r, row) in enumerate(grid):\n",
52 " for (c, cell) in enumerate(row):\n",
53 " rx = c * 1\n",
54 " ry = r * 1\n",
55 " # print(rx, ry)\n",
56 " draw.rectangle([rx, ry, rx + 1, ry + 1], \n",
57 " fill=ImageColor.getrgb(\"hsl({}, 100%, 50%)\".format(cell)))\n",
58 "\n",
59 " im.save('{}{:04}.png'.format(prefix, frame_number), 'PNG')"
60 ]
61 },
62 {
63 "cell_type": "code",
64 "execution_count": 42,
65 "metadata": {},
66 "outputs": [],
67 "source": [
68 "def insertsort_step(items, sorted_limit):\n",
69 " inserting = items[sorted_limit]\n",
70 " i = len([item for item in items[:sorted_limit] if item < inserting])\n",
71 "# print('sl = {}, i = {}; items = {}; slices {} {} {} {}'.format(sorted_limit, i, items, items[:i], inserting, items[i:sorted_limit], items[sorted_limit + 1:]))\n",
72 " items = items[:i] + [inserting] + items[i:sorted_limit] + items[sorted_limit + 1:]\n",
73 " return items"
74 ]
75 },
76 {
77 "cell_type": "code",
78 "execution_count": 13,
79 "metadata": {},
80 "outputs": [
81 {
82 "data": {
83 "text/plain": [
84 "['a', 'b']"
85 ]
86 },
87 "execution_count": 13,
88 "metadata": {},
89 "output_type": "execute_result"
90 }
91 ],
92 "source": [
93 "list('abcde')[:2]"
94 ]
95 },
96 {
97 "cell_type": "code",
98 "execution_count": 35,
99 "metadata": {},
100 "outputs": [],
101 "source": [
102 "def insertsort(items, step_history):\n",
103 " for sorted_limit in range(1, len(items)):\n",
104 " step_history += [items[:]]\n",
105 " items = insertsort_step(items, sorted_limit)\n",
106 " step_history += [items[:]]"
107 ]
108 },
109 {
110 "cell_type": "code",
111 "execution_count": 60,
112 "metadata": {},
113 "outputs": [],
114 "source": [
115 "histories = []\n",
116 "for _ in range(ROWS):\n",
117 " items = initial_items[:]\n",
118 " random.shuffle(items)\n",
119 " step_history = []\n",
120 " insertsort(items, step_history)\n",
121 " histories += [step_history]"
122 ]
123 },
124 {
125 "cell_type": "code",
126 "execution_count": 50,
127 "metadata": {},
128 "outputs": [],
129 "source": [
130 "# histories"
131 ]
132 },
133 {
134 "cell_type": "code",
135 "execution_count": 46,
136 "metadata": {
137 "scrolled": true
138 },
139 "outputs": [
140 {
141 "data": {
142 "text/plain": [
143 "[0,\n",
144 " 10,\n",
145 " 20,\n",
146 " 30,\n",
147 " 40,\n",
148 " 50,\n",
149 " 60,\n",
150 " 70,\n",
151 " 80,\n",
152 " 90,\n",
153 " 100,\n",
154 " 110,\n",
155 " 120,\n",
156 " 130,\n",
157 " 140,\n",
158 " 150,\n",
159 " 160,\n",
160 " 170,\n",
161 " 180,\n",
162 " 190,\n",
163 " 200,\n",
164 " 210,\n",
165 " 220,\n",
166 " 230,\n",
167 " 240,\n",
168 " 250,\n",
169 " 260,\n",
170 " 270,\n",
171 " 280,\n",
172 " 290,\n",
173 " 300,\n",
174 " 310,\n",
175 " 320,\n",
176 " 330,\n",
177 " 340,\n",
178 " 350]"
179 ]
180 },
181 "execution_count": 46,
182 "metadata": {},
183 "output_type": "execute_result"
184 }
185 ],
186 "source": [
187 "# histories[2][-1]"
188 ]
189 },
190 {
191 "cell_type": "code",
192 "execution_count": 53,
193 "metadata": {},
194 "outputs": [
195 {
196 "data": {
197 "text/plain": [
198 "720"
199 ]
200 },
201 "execution_count": 53,
202 "metadata": {},
203 "output_type": "execute_result"
204 }
205 ],
206 "source": [
207 "len(histories[0])"
208 ]
209 },
210 {
211 "cell_type": "code",
212 "execution_count": 61,
213 "metadata": {},
214 "outputs": [],
215 "source": [
216 "! rm insertsort*"
217 ]
218 },
219 {
220 "cell_type": "code",
221 "execution_count": null,
222 "metadata": {},
223 "outputs": [],
224 "source": [
225 "for frame_n in range(max(len(h) for h in histories)):\n",
226 " draw_frame(histories, frame_n, prefix='insertsort')"
227 ]
228 },
229 {
230 "cell_type": "code",
231 "execution_count": 65,
232 "metadata": {},
233 "outputs": [
234 {
235 "data": {
236 "text/plain": [
237 "719"
238 ]
239 },
240 "execution_count": 65,
241 "metadata": {},
242 "output_type": "execute_result"
243 }
244 ],
245 "source": [
246 "frame_n"
247 ]
248 },
249 {
250 "cell_type": "code",
251 "execution_count": null,
252 "metadata": {
253 "scrolled": true
254 },
255 "outputs": [],
256 "source": [
257 "! apngasm all-insertsort.png insertsort*png 1 10"
258 ]
259 },
260 {
261 "cell_type": "code",
262 "execution_count": null,
263 "metadata": {},
264 "outputs": [],
265 "source": []
266 }
267 ],
268 "metadata": {
269 "kernelspec": {
270 "display_name": "Python 3",
271 "language": "python",
272 "name": "python3"
273 },
274 "language_info": {
275 "codemirror_mode": {
276 "name": "ipython",
277 "version": 3
278 },
279 "file_extension": ".py",
280 "mimetype": "text/x-python",
281 "name": "python",
282 "nbconvert_exporter": "python",
283 "pygments_lexer": "ipython3",
284 "version": "3.5.3"
285 }
286 },
287 "nbformat": 4,
288 "nbformat_minor": 2
289 }