CSC153, Class 23: Sorting Overview: * The problem of sorting * How to design a sorting algorithm * Two standard sorting algorithms * Lab (if there's time) Notes: * Questions on the homework? * Questions on the reading on searching? * Questions on anything? ---------------------------------------- Review: * Sequential search * Binary search * Random search If we're going to use binary search, we need our array to be "in order". * We need techniques for putting collections of stuff in order. Why else to put things in order * Aesthetics * Permit humans to apply binary search or some variant thereof. * Lets you group things. * Lets you quickly select "top N" Sam's technique for designing sorting (and other) algorithms * Do it "by hand" * Reflect on what you did * Reflect on key aspects * Encode in Scheme * Cross your fingers or Formally analyze Husker Du Oge-Sort (Insertion Sort): * Separate the stuff to sort into two parts + Things I haven't looked at yet + Things I've already put in order * Repeatedly + Grab something you haven't looked at + Put it in the "correct" place in the ordered stuff Running time? We grab something N times (1 step each) We put it in ordered stuff N times Find the appropriate place: log_2(n) using binary search Put it in the appropriate place: Stack of CD's: 1 Insertion sorting a pile of CDs is O(n*log_2(n)) * Fastest known speed for a sorting algorithm based on comparing elements: O(n*log_2(n)) What's the running time if the input is a list? * Finding the appropriate place is O(n) * Insertion is either O(1) or O(n) ;;; Precondition: ;;; val is an integer ;;; lst is a list of integers sorted in non-decreasing ;;; order (define insert (lambda (val lst) (cond ((null? lst) (cons val null)) ((<= val (car lst)) (cons val lst)) (else (cons (car lst) (insert val (cdr lst))))))) The <= should be a parameter. THen we can insert into any type of list. If the input is a list, insertion sort is O(n*n) What if the thing being sorted is a vector? * Finding the correct place takes O(log_2(n)) * To insert into a vector, you need to shift stuff around. Potentially O(n) stuff. So insertion sort is O(n*(n+log_2(n)+c)) = O(n^2) On average, the thing to insert is in the middle. 1/2 + 1 + 3/2 + 4/2 + 5/2 + ... n/2 = n*(n+1)/4 ---- Other common technique: Selection sort * Repeatedly select the largest or smallest and shove at the front. ---------------------------------------- Precondition for insert-string (string<=? (list-ref strings i) (list-ref strings (+ i 1))) "for all reasonable i" for 0 <= i < (- (string-length strings) 1) How do we get time not to display the result? > (let ((stuff (time (...)))) (newline)) > (time (car (insertion-sort ...))) ---------------------------------------- Things in Sam's stack of CD's * Husker Du: Classic Noise-Pop from Minneapolis (same period as the Replacements) * Material Issue: Chicago-style Power Pop * Ric Ocasek: Former Car's lead * Michelle Shocked: Fun Folk * Sonic Youth: Noise * Vinal Avenue String Band: Boston Bluegrass