-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathselectSortLectureGuide.md.html
More file actions
253 lines (246 loc) · 16.3 KB
/
selectSortLectureGuide.md.html
File metadata and controls
253 lines (246 loc) · 16.3 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>cs3003selectSortLectureGuide</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="#selection-sort">Selection 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="#interactive-coding">Interactive Coding</a></li>
<li><a href="#stage---1">Stage - 1</a></li>
<li><a href="#stage-2">Stage 2</a></li>
<li><a href="#selection-sort-description">Selection Sort Description</a></li>
<li><a href="#pseudo-code">Pseudo Code</a></li>
<li><a href="#part-1.5">Part 1.5</a></li>
<li><a href="#part-2">Part 2</a></li>
<li><a href="#a-more-efficient-selection-sort">A More Efficient Selection Sort</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="selection-sort">Selection 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>
<p>It is very important to use a data type in a real-world case, to understand its true value and benefit.</p>
</li>
<li>
<p>We are covering three types of sorts</p>
<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>
<li>
<p>We have already seen the power of Lists when we covered <strong>InsertionSort</strong></p>
<ul>
<li>Select a <code>value</code> from the unsorted sub-list</li>
<li>Decide where to <strong>insert</strong> it in the already_sorted sub-list</li>
<li>Right shift all the values and then insert the <code>value</code></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>SelectionSort</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>
<ul>
<li><a href="http://j.mp/indexMinCC">http://j.mp/indexMinCC</a> - is the best way to be prepared for SelectionSort</li>
<li>how do you iterate over all the elements in a list, except the last one? Write the python code to do this</li>
<li>What is the value of <code>[1, 2, 3, 4][0:]</code> and <code>[1, 2, 3, 4][1:]</code> and <code>[1, 2, 3, 4][2:]</code>?</li>
<li>What is the built-in function which is used to find the minimum value of a list of numbers? Create a list containing 10 random numbers. Write the python code to find the minimum value in the list.</li>
<li>What is the list method to find out the index of a value in a list?
<ul>
<li>How many arguments does the method accept?</li>
<li>What is the second argument for?</li>
</ul>
</li>
<li>What are the ways you can swap the value in two variables?
<ul>
<li>What is the most pythonic way to do this?
<ul>
<li>Word count - Python beats Java, like crazy!</li>
</ul>
</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="interactive-coding">Interactive Coding</h2>
<h2 id="stage---1">Stage - 1</h2>
<ol>
<li>
<p>Example of list containing <code>[1, 2, 3, 5, 6, 4]</code></p>
<ol start="2">
<li>Already sorted list, except the number 4</li>
</ol>
</li>
<li>
<p><strong>Select</strong> the minimum value in all of the unsorted list</p>
<ol start="3">
<li><code>1</code> is the minimum - place it in index <code>0</code></li>
<li><code>2</code> is the minimum - place it in index <code>1</code></li>
<li><code>3</code> is the minimum - place it in index <code>2</code></li>
<li><code>4</code> is the minimum - prepare to swap with 5
<ol start="7">
<li><code>[1, 2, 3, 4, 6, 5]</code></li>
</ol>
</li>
<li><code>5</code> is the minimum - prepare to swap with 6
<ol start="8">
<li><code>[1, 2, 3, 4, 5, 6]</code></li>
</ol>
</li>
</ol>
</li>
<li>
<p>If necessary (<code>i != min_i</code>) swap the minimum value from the unsorted sublist with the element in the sorted sub-list</p>
</li>
</ol>
<hr>
<h2 id="stage-2">Stage 2</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>selectionsort</code></li>
</ol>
<p><img src="http://bit.ly/select2PNG" alt="select"></p>
<h2 id="selection-sort-description">Selection Sort Description</h2>
<ul>
<li>In <code>selectionsort</code>, the position of the update is pre-determined, starting from the beginning of the list. We then go <strong>select</strong> the minimum value among the unsorted elements of the list, and swap it with the element in the pre-determined location.</li>
</ul>
<h2 id="pseudo-code">Pseudo Code</h2>
<p>Have seen the example, it becomes very easy to define the pseudo code for SelectionSort</p>
<pre><code>repeat (numOfElements - 1) times
for each of the unsorted elements
select the minimum value among them
swap minimum with first unsorted position
</code></pre>
<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">selectionsort</span><span class="token punctuation">(</span>alist<span class="token punctuation">)</span><span class="token punctuation">:</span>
n <span class="token operator">=</span> <span class="token builtin">len</span><span class="token punctuation">(</span>alist<span class="token punctuation">)</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>n<span class="token number">-1</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
unsorted <span class="token operator">=</span> alist<span class="token punctuation">[</span>i<span class="token punctuation">:</span><span class="token punctuation">]</span>
smallest <span class="token operator">=</span> <span class="token builtin">min</span><span class="token punctuation">(</span>unsorted<span class="token punctuation">)</span>
min_i <span class="token operator">=</span> alist<span class="token punctuation">.</span>index<span class="token punctuation">(</span>smallest<span class="token punctuation">,</span> i<span class="token punctuation">)</span>
alist<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">,</span>alist<span class="token punctuation">[</span>min_i<span class="token punctuation">]</span> <span class="token operator">=</span> alist<span class="token punctuation">[</span>min_i<span class="token punctuation">]</span><span class="token punctuation">,</span> alist<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">"intermediary"</span><span class="token punctuation">,</span> alist<span class="token punctuation">)</span>
<span class="token keyword">return</span> alist
</code></pre>
<h2 id="part-1.5">Part 1.5</h2>
<p><em>Break Activity for the 20th min</em> which also helps students review what was covered until now.</p>
<ul>
<li>Lead with the question: <strong>Can you improve on the above logic?</strong> How can you do it? Please discuss with your immediate neighbour.
<ul>
<li>if you know how to code it, write down the code in Python as well</li>
<li>Go back and improve the pseudo code as well</li>
</ul>
</li>
</ul>
<pre class=" language-python"><code class="prism language-python"><span class="token keyword">def</span> <span class="token function">selectionsort</span><span class="token punctuation">(</span>alist<span class="token punctuation">)</span><span class="token punctuation">:</span>
n <span class="token operator">=</span> <span class="token builtin">len</span><span class="token punctuation">(</span>alist<span class="token punctuation">)</span>
<span class="token comment"># STEP 0 - iterate through the list</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>n<span class="token number">-1</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
min_i <span class="token operator">=</span> i
<span class="token comment"># STEP 1 - update mini_i with index </span>
<span class="token comment"># of min value in unsorted alist[i+1:]</span>
<span class="token keyword">for</span> j <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span>i<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">,</span> n<span class="token punctuation">)</span><span class="token punctuation">:</span>
<span class="token keyword">if</span> alist<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator"><</span> alist<span class="token punctuation">[</span>min_i<span class="token punctuation">]</span><span class="token punctuation">:</span>
min_i <span class="token operator">=</span> j
<span class="token comment"># STEP 2 - swap it with element at index 'i'</span>
<span class="token keyword">if</span> min_i <span class="token operator">!=</span> i<span class="token punctuation">:</span>
alist<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">,</span> alist<span class="token punctuation">[</span>min_i<span class="token punctuation">]</span> <span class="token operator">=</span> alist<span class="token punctuation">[</span>min_i<span class="token punctuation">]</span><span class="token punctuation">,</span> alist<span class="token punctuation">[</span>i<span class="token punctuation">]</span>
<span class="token keyword">return</span> alist
</code></pre>
<h2 id="part-2">Part 2</h2>
<ul>
<li>OPTIONAL - generate a list containing tuples, where each tuple contains both the element and the index of the element in the list</li>
<li>OPTIONAL - use a built-in function to find out the tuple containing the least value in the list</li>
</ul>
<h2 id="a-more-efficient-selection-sort">A More Efficient Selection Sort</h2>
<ul>
<li>uses <code>tuples</code> and <code>list comprehension</code> to make the code very efficient and eminently readable.</li>
</ul>
<pre class=" language-python"><code class="prism language-python"><span class="token keyword">def</span> <span class="token function">selectsort</span><span class="token punctuation">(</span>alist<span class="token punctuation">)</span><span class="token punctuation">:</span>
n <span class="token operator">=</span> <span class="token builtin">len</span><span class="token punctuation">(</span>alist<span class="token punctuation">)</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>n<span class="token number">-1</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
unsorted <span class="token operator">=</span> alist<span class="token punctuation">[</span>i<span class="token punctuation">:</span><span class="token punctuation">]</span>
<span class="token punctuation">(</span>minval<span class="token punctuation">,</span> min_i<span class="token punctuation">)</span> <span class="token operator">=</span> <span class="token builtin">min</span><span class="token punctuation">(</span><span class="token punctuation">(</span>v<span class="token punctuation">,</span> i<span class="token punctuation">)</span> \
<span class="token keyword">for</span> i<span class="token punctuation">,</span> v <span class="token keyword">in</span> <span class="token builtin">enumerate</span><span class="token punctuation">(</span>unsorted<span class="token punctuation">)</span><span class="token punctuation">)</span>
alist<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">,</span> alist<span class="token punctuation">[</span>min_i<span class="token operator">+</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> alist<span class="token punctuation">[</span>min_i<span class="token operator">+</span>i<span class="token punctuation">]</span><span class="token punctuation">,</span> alist<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">"intermediary"</span><span class="token punctuation">,</span> alist<span class="token punctuation">)</span>
<span class="token keyword">return</span> alist
tlist <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">85</span><span class="token punctuation">,</span> <span class="token number">69</span><span class="token punctuation">,</span> <span class="token number">12</span><span class="token punctuation">,</span> <span class="token number">12</span><span class="token punctuation">,</span> <span class="token number">54</span><span class="token punctuation">,</span> <span class="token number">36</span><span class="token punctuation">,</span> <span class="token number">45</span><span class="token punctuation">,</span> <span class="token number">5</span><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> tlist<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> selectsort<span class="token punctuation">(</span>tlist<span class="token punctuation">)</span><span class="token punctuation">)</span>
</code></pre>
<h2 id="summary">Summary</h2>
<ol>
<li>We incrementally developed SelectionSort in a “manual mode”</li>
<li>We arrived at the pseudo code</li>
<li>We wrote the equivalent code for SelectionSort</li>
</ol>
<p>In reflection, <strong>SelectionSort</strong> is just the opposite of <strong>InsertionSort</strong>. The worst case complexity of this SelectionSort is <code>O(n^2)</code></p>
<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><a href="http://j.mp/selectionSortCC">http://j.mp/selectionSortCC</a> and <a href="http://j.mp/indexMinCC">http://j.mp/indexMinCC</a></p>
</li>
<li>
<p>Review <a href="http://j.mp/mergeVisual">http://j.mp/mergeVisual</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><a href="http://j.mp/selectPAW">http://j.mp/selectPAW</a></li>
<li>This is the script I use in my sample video <a href="http://bit.ly/selectionVideo2">http://bit.ly/selectionVideo2</a></li>
</ul>
</div>
</div>
</body>
</html>