The agenda for today :
Coming attractions (and things to read about)
Questions about anything before we start?
First let's do a live coding walkthrough of the homework. I've posted a few related programs over in the answers folder.
OK, time to dig into sorting algorithms. Please start reading about these : the various approaches, their properties, time and memory efficiency, and other properties.
Here are a few classics worth understanding.
bubble sort
quick sort
heap sort
0
1 2
3 4 5 6
7,8 9,10 11,12 13,14
left child at (2*i+1)
right child at (2*i+2)
radix sort : O(n) (but ...)
Come back together & compare notes.
[2, 1, 10, 12]
, implement one of those.We probably won't get to this today, but here's where we're going next :
I would like to discuss a bit more about how to understand when an algorithm is working correctly ... and how that is related to "proof by induction".
* What is "proof by induction" ?
I think this is clearest with an example.
* Prove that sum(range(1, 1+n)) == n*(n+1)/2 by induction.
(Let's see if we can understand the math proof of this.)
The on to the code version of the calculation :
* write some code to calculate that sum with
an "accumulator loop"
And finally, let's add an explicit "loop invariant" assertion which is (a) True at the start of each loop, and (b) at the end of the loop, is the claim that our result is correct.
I'll use the attached files to discuss this.