Algorithms
and
Data
Structures

Spring 2022
course
site
-->

March 14

The agenda for today :

Coming attractions (and things to read about)

Questions about anything before we start?

homework

First let's do a live coding walkthrough of the homework. I've posted a few related programs over in the answers folder.

sorting

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 ...)

group exercises in class

Come back together & compare notes.

induction, invariants, and loops

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.

https://cs.bennington.college /courses /spring2022 /algorithms /notes /march14
last modified Mon March 14 2022 3:22 pm

attachments [paper clip]

  last modified size
TXT insert.py Mon Mar 14 2022 03:22 pm 1.4K
TXT proof.txt Sun Mar 13 2022 10:47 pm 819B
TXT sum.py Sun Mar 13 2022 10:47 pm 486B