-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathcs3003insertsortLectureGuide.md.html
More file actions
201 lines (194 loc) · 10.7 KB
/
cs3003insertsortLectureGuide.md.html
File metadata and controls
201 lines (194 loc) · 10.7 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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>cs3003insertsortLectureGuide</title>
<link rel="stylesheet" href="https://stackedit.io/style.css" />
</head>
<body class="stackedit">
<div class="stackedit__left">
<div class="stackedit__toc">
<ul>
<li><a href="#insertion-sort">Insertion Sort</a></li>
<li><a href="#context-setting">Context setting</a></li>
<li><a href="#agenda">Agenda</a></li>
<li><a href="#pre-req-quiz">Pre-req quiz</a>
<ul>
<li><a href="#use-a-pack-of-cards">Use a Pack of Cards</a></li>
<li><a href="#interactive-coding">Interactive Coding</a></li>
<li><a href="#stage-0">Stage 0</a></li>
<li><a href="#stage-1">Stage 1</a></li>
<li><a href="#stage-2">Stage 2</a></li>
<li><a href="#insertion-sort-description">Insertion Sort Description</a></li>
<li><a href="#pseudo-code">Pseudo Code</a></li>
<li><a href="#source-code">Source Code</a></li>
<li><a href="#summary">Summary</a></li>
<li><a href="#last-2-minute">Last 2 Minute</a></li>
<li><a href="#program-file-for-demo">Program File for Demo</a></li>
</ul>
</li>
</ul>
</div>
</div>
<div class="stackedit__right">
<div class="stackedit__html">
<h1 id="insertion-sort">Insertion Sort</h1>
<p><em>Lecture outline</em></p>
<h1 id="context-setting">Context setting</h1>
<p>Unit 4 -> Lists, Tuples and Dictionaries -> Illustrative Programs</p>
<ul>
<li>It is very important to use a data type in a real-world case, to understand its true value and benefit.</li>
<li>We are covering three types of sorts
<ul>
<li><strong>Comparison</strong> Algorithms - Insertion Sort and Selection Sort</li>
<li><strong>Divide and Conquer</strong> Algorithms - Merge Sort, Quick Sort</li>
</ul>
</li>
</ul>
<h1 id="agenda">Agenda</h1>
<ul>
<li>
<p>Over the next 20 minutes, we shall see how lists can be used to implement <strong>InsertionSort</strong></p>
</li>
<li>
<p><strong>SHOW and TELL</strong> - Show Example, And Tell Theory</p>
<ul>
<li>versus Tell Theory, Show Example</li>
<li>a more effective style to create engagement
<ul>
<li>VERY VERY effective when you students participate</li>
<li>Even more effective when students KNOW something already</li>
</ul>
</li>
</ul>
</li>
</ul>
<h1 id="pre-req-quiz">Pre-req quiz</h1>
<pre><code>- Are you ready?
- Do you know something already
- Known to Unknown
</code></pre>
<p><img src="http://j.mp/indexList" alt="index"></p>
<ul>
<li>What is the code to iterate over all the elements in a list?</li>
<li>What is the code to iterate over all the elements in a list, starting from the 2nd element in the list?</li>
<li>If <code>al= [11, 12, 13, 14, 15]</code>, and <code>al[2] = 15</code> are executed, what will be <code>al</code> contain?</li>
<li>If you have more time, <a href="http://j.mp/insertTDD">http://j.mp/insertTDD</a></li>
<li>Finish the <a href="http://bit.ly/insortCC">http://bit.ly/insortCC</a>
<ul>
<li><a href="http://bit.ly/insortVideoKey">http://bit.ly/insortVideoKey</a></li>
</ul>
</li>
</ul>
<p>Show of hands as to many of you students reviewed all the above questions and have answers written down in your notebooks?<br>
- best practice of a engineering student is to come prepared for the class. You must spend at least 2 hours reviewing the material taught in class, and preparing for the class</p>
<h2 id="use-a-pack-of-cards">Use a Pack of Cards</h2>
<p><a href="http://j.mp/compareSort">http://j.mp/compareSort</a> - the first 35 seconds of the video</p>
<h2 id="interactive-coding">Interactive Coding</h2>
<h2 id="stage-0">Stage 0</h2>
<ul>
<li>Manual sorting, like you were to sort a list of cards
<ul>
<li>use a custom built <code>insort</code> function</li>
</ul>
</li>
</ul>
<h2 id="stage-1">Stage 1</h2>
<ol start="4">
<li>Define an iteration through the list, and do this for every element in the list</li>
<li>Initialize the right variables and execute the code</li>
<li>Place the entire code inside inside a function definition and call it <code>insertionsort</code></li>
</ol>
<p><img src="http://bit.ly/insertionSortPNG" alt="insert"></p>
<h2 id="stage-2">Stage 2</h2>
<p>Understanding how to write the <code>insort</code> function which is used in <code>insertionsort</code> using the CloudCoder exercise <a href="http://j.mp/insortCC">http://j.mp/insortCC</a></p>
<h2 id="insertion-sort-description">Insertion Sort Description</h2>
<ul>
<li>In <code>insertionsort</code>, given a <strong>key</strong>, a copy of a pre-determined element in the list, we <strong>insert</strong> it at the appropriate location after comparing it with the unsorted elements of the list. We do this repeatedly for all the elements in the list.</li>
</ul>
<p><img src="http://j.mp/insert1PNG" alt="insert1"><br>
<img src="http://j.mp/insert2PNG" alt="insert2"><br>
<img src="http://j.mp/insert3PNG" alt="insert3"></p>
<h2 id="pseudo-code">Pseudo Code</h2>
<p>Have seen the example, it becomes very easy to define the pseudo code for InsertionSort</p>
<pre><code>mark first element as sorted
for each unsorted element X
'extract' the element X as 'key'
insert key at the relevant index so list remains sorted
return sorted list
</code></pre>
<p>More detailed pseudo-code:</p>
<pre><code>mark first element as sorted
for each unsorted element X
extract the element X
for j = lastSortedIndex (position -1) down to 0
if current element j > X
decrement j by 1
else break loop and insert X here
return sorted list
</code></pre>
<h2 id="source-code">Source Code</h2>
<p>The Python code to implement the pseudo code above:</p>
<pre class=" language-python"><code class="prism language-python"><span class="token keyword">def</span> <span class="token function">insertionsort</span><span class="token punctuation">(</span>alist<span class="token punctuation">)</span><span class="token punctuation">:</span>
<span class="token triple-quoted-string string">'''insertionsort algorithm implemented
using insort function
'''</span>
<span class="token keyword">for</span> i <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token builtin">len</span><span class="token punctuation">(</span>alist<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
key <span class="token operator">=</span> alist<span class="token punctuation">[</span>i<span class="token punctuation">]</span>
insort<span class="token punctuation">(</span>alist<span class="token punctuation">,</span> key<span class="token punctuation">,</span> i<span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"inter"</span><span class="token punctuation">,</span> alist<span class="token punctuation">)</span>
<span class="token keyword">return</span> alist
<span class="token keyword">def</span> <span class="token function">insort</span><span class="token punctuation">(</span>alist<span class="token punctuation">,</span> key<span class="token punctuation">,</span> j<span class="token punctuation">)</span><span class="token punctuation">:</span>
<span class="token triple-quoted-string string">'''insort inserts 'key' into the
sorted alist[:j] so that it remains sorted
'j' is the current index of 'key' in alist
'''</span>
<span class="token keyword">while</span> j <span class="token operator">></span> <span class="token number">0</span> <span class="token operator">and</span> alist<span class="token punctuation">[</span>j<span class="token number">-1</span><span class="token punctuation">]</span> <span class="token operator">></span> key<span class="token punctuation">:</span>
alist<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">=</span> alist<span class="token punctuation">[</span>j<span class="token number">-1</span><span class="token punctuation">]</span>
j <span class="token operator">-=</span> <span class="token number">1</span>
alist<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">=</span> key
</code></pre>
<p>The following code can be used to test the above code:</p>
<pre class=" language-python"><code class="prism language-python"><span class="token comment"># Test case using random shuffle</span>
<span class="token keyword">from</span> random <span class="token keyword">import</span> shuffle
alist <span class="token operator">=</span> <span class="token builtin">list</span><span class="token punctuation">(</span><span class="token builtin">range</span><span class="token punctuation">(</span><span class="token number">11</span><span class="token punctuation">,</span> <span class="token number">19</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
shuffle<span class="token punctuation">(</span>alist<span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"Unsorted"</span><span class="token punctuation">,</span> alist<span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"Sorted"</span><span class="token punctuation">,</span> insertionsort<span class="token punctuation">(</span>alist<span class="token punctuation">)</span><span class="token punctuation">)</span>
</code></pre>
<h2 id="summary">Summary</h2>
<ol>
<li>We incrementally developed InsertionSort in a “manual mode”</li>
<li>We arrived at the pseudo code</li>
<li>We wrote the equivalent code for InsertionSort</li>
</ol>
<h2 id="last-2-minute">Last 2 Minute</h2>
<ul>
<li>
<p>respond to Questions from Students</p>
<ul>
<li>review the relevant Question Paper snippets</li>
<li>Ask 1 or more students questions from Pre-requisites</li>
</ul>
</li>
<li>
<p>Practice insertionsort at <a href="http://j.mp/insertionSortCC">http://j.mp/insertionSortCC</a></p>
<ul>
<li>The million insert challenge <a href="http://j.mp/millionInsert">http://j.mp/millionInsert</a></li>
</ul>
</li>
<li>
<p>Review <a href="https://observablehq.com/@pcsilcan/selection-sort/2?collection=@pcsilcan/sorting-algorithms">http://j.mp/selectVisual</a> for the next class</p>
</li>
</ul>
<h2 id="program-file-for-demo">Program File for Demo</h2>
<p>Modify this file as you deem fit to demonstrate on <a href="http://PythonAnywhere.com">PythonAnywhere.com</a> or on the mu editor</p>
<ul>
<li></li>
<li>This is the script I use in my sample video <a href="http://bit.ly/insertSortVideo2">http://bit.ly/insertSortVideo2</a></li>
</ul>
</div>
</div>
</body>
</html>