cs3003insertsortLectureGuide
Insertion Sort
Lecture outline
Context setting
Unit 4 -> Lists, Tuples and Dictionaries -> Illustrative Programs
It is very important to use a data type in a real-world case, to understand its true value and benefit.
We are covering three types of sorts
Comparison Algorithms - Insertion Sort and Selection Sort
Divide and Conquer Algorithms - Merge Sort, Quick Sort
Agenda
Over the next 20 minutes, we shall see how lists can be used to implement InsertionSort
SHOW and TELL - Show Example, And Tell Theory
versus Tell Theory, Show Example
a more effective style to create engagement
VERY VERY effective when you students participate
Even more effective when students KNOW something already
Pre-req quiz
What is the code to iterate over all the elements in a list?
What is the code to iterate over all the elements in a list, starting from the 2nd element in the list?
If
al= [11, 12, 13, 14, 15]
, andal[2] = 15
are executed, what will beal
contain?If you have more time, http://j.mp/insertTDD
Finish the http://bit.ly/insortCC
http://bit.ly/insortVideoKey
Show of hands as to many of you students reviewed all the above questions and have answers written down in your notebooks? - 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
Use a Pack of Cards
http://j.mp/compareSort - the first 35 seconds of the video
Interactive Coding
Stage 0
Manual sorting, like you were to sort a list of cards
use a custom built
insort
function
Stage 1
Define an iteration through the list, and do this for every element in the list
Initialize the right variables and execute the code
Place the entire code inside inside a function definition and call it
insertionsort
Stage 2
Understanding how to write the insort
function which is used in insertionsort
using the CloudCoder exercise http://j.mp/insortCC
Insertion Sort Description
In
insertionsort
, given a key, a copy of a pre-determined element in the list, we insert 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.
Pseudo Code
Have seen the example, it becomes very easy to define the pseudo code for InsertionSort
More detailed pseudo-code:
Source Code
The Python code to implement the pseudo code above:
The following code can be used to test the above code:
Summary
We incrementally developed InsertionSort in a "manual mode"
We arrived at the pseudo code
We wrote the equivalent code for InsertionSort
Last 2 Minute
respond to Questions from Students
review the relevant Question Paper snippets
Ask 1 or more students questions from Pre-requisites
Practice insertionsort at http://j.mp/insertionSortCC
The million insert challenge http://j.mp/millionInsert
Program File for Demo
Modify this file as you deem fit to demonstrate on PythonAnywhere.com or on the mu editor
This is the script I use in my sample video http://bit.ly/insertSortVideo2
Last updated