Feb 14
Comments on last round of homework have been posted.
The assignment for next week is up ... we'll start
today in class on some similar work.
What does "in place" mean?
Today :
Work through in class writing code and examples (with
printing to see intermediate results and step counting)
of the 3 sorting algorithms we've been discussing.
We'll use integers for data to keep things simple.
* Implement a binary min heap and its API
using a sequential list to store the data.
h = Heap() # make an empty one
h.push(i) # add a number to the heap
value = h.peek() # get smallest, no removal
value = h.pop() # remove smallest number
What is O() behavior of each of these?
How can we use these to implement
sorted_list = heapsort([2,1,5,10,3,2])
* Write code for a recursive mergesort :
sorted_list = mergesort([2,1,5,10,3,2])
* Explain what the quicksort "partition" function is for,
how it does its thing, and how it is used to implement
quicksort recursively, where ifoo means "index_for_foo"
and is a numeric offset into list[].
new_ipivot = partition(list, istart, iend, ipivot)
array = [2,1,5,10,3,2]
quicksort(array) # no return; sorted in place
Aside:
# in C, this code has a bug. Can you see it?
middleIndex = (leftIndex + rightIndex)/2;
In class: we wrote heap.py (works as far as we got), merge.py (untested, buggy), and in_place.py (example of what that means). All uploaded here.
I also uploaded my own quicksort.py code,
trying to illustrate the partition stuff
we didn't get through last time.