... start recording.
Any questions about anything? ( Bueler? )
Start by finishing the in_class.py from last time which had a bug ...
This algorithm is very close to "Selection Sort", which adds in an in-place simplification : swap the smallest to the i'th start of the array; increasing i each time. (It "selects" the next smallest each time through the outer loop.) So ... let's see if we can code that with a simple test in class.
Discuss assignments for next two weeks ... including the midterm project.
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.
after class :
The two files that I used to explain this,
proof.txt and sum.py, are both attached below.
... 15 sorting algorithms in 6 minutes
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 ...)
I've created a sorting.c template for you . (And here's a pretty version for us to look at.)
Let me also remind you how to compile and run that on jupyter.bennington in a terminal :
# upload file in jupyter , or `wget URL` to fetch it over the internet
$ gcc sorting.c -o sorting
$ ./sorting
And let's add this useful utility :
void swap(int* xptr, int* yptr){
// swap the integers at the two given addresses.
int temp = *xptr;
*xptr = *yptr;
*yptr = temp;
}
// elsewhere :
swap(&array[i], &array[j]); // swap the i'th and j'th elements of the array.
(Note that this could also be implemented with this API instead : swap(int i, int j, int* array)
.
What would that look like?)
We decided to postpone the breakout room pair programming until Monday.
And I've extended the deadline for the next assignment until Tuesday, so you can ask questions in class about any of that if you like.