CSC151.01 2006S, Class 44: Merge Sort Admin: * Reading for Friday: Quicksort. * Are there questions on HW15? * EC for tomorrow's convo. * EC and free pizza for tomorrow's Thursday Extra. * We'll spend a few minutes going over the insert! procedure. Overview: * insert! * Key Ideas. * Lab * Reflection. /Insertion Sort/ * Key idea in list-based insertion sort: You insert a value that you've just prepared into a list that is already sorted. * Parameters to insert * The value to insert * The sorted list into which to insert the value * THe may-precede? that determines the ordering of the list. * Vector based insertion sort in which we work "in place" (modifying the vector) +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | 2| 3| 7| 8|14|18| 1|17| 5|17|17|11|10| 7| 0| 5| +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ * Idea: Insert into the beginning of the vector, in increasing order * After a few steps, we've divided the vector into things that are sorted and things that aren't sored. sorted | unsorted +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | 2| 3| 7| 8|14|18| 1|17| 5|17|17|11|10| 7| 0| 5| +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ After inserting 1 sorted | unsorted +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | 1| 2| 3| 7| 8|14|18|17| 5|17|17|11|10| 7| 0| 5| +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ After inserting teh 17 After inserting 1 sorted | unsorted +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | 1| 2| 3| 7| 8|14|17|18| 5|17|17|11|10| 7| 0| 5| +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ We want to write this insertion procedure, called insert! * Length of the vector or something similar (the length of the sorted region) * value to insert (as in the list) * vec (like the sorted list) * may-precede?, which determines the ordering /Merge Sort: Key Ideas?/ * Divide whatever you're trying to sort in half * Sort the halves * Put the sorted halves back together /Lab/ * How do you decide whether (list "Jager" "Leah") or (list "Moore" "Tom") comes first? * Take the car, which gives you the last name * Compare the cars using string