CSC297.Java, Class 27: Sorting Admin: * Did you do your Homework? * Some of it * Let's fix the problems with Yvonne's work * New homework * Carefully write the preconditions for sort, swap, findSmallest Overview: * Selection sort * Analysis * Insertion sort Morals from Selection Sort * The comparator should be a parameter of the sorting algorithm (and of the "find smallest"). * Be careful about boundary conditions * Selection sort is typically an "in place" algorithm Asymptotic Analysis ; Selectionsort ; Use "N" for length for i = 0 to length -1 do N repetitions swap(i, findSmallest(starting at i)) 1 for swap N for findSmallest Running time is N*(N+1) is in O(N^2) Two perspectives: * At worst, findSmallest takes N * Alex's findSmallest takes decresasing amounts N + N-1 + N-2 + ... * Still gives is O(N^2) Insertion Sort * Divide your world into "stuff that's in order" and "stuff that's not in order" * Repeatedly remove one item from "stuff that's not in order" and put it in the right place in "stuff that's in order". * How do we do this with arrays? * Choose an index that serves as the boundary between "ordered" and "not ordered". * Repeatedly increase that index and "fix things up" Analysis for i = 0 to n-1 N repetition insert(i) N steps O(N^2(