200 likes | 622 Views
Lecture 7: Induction & Sorting. Today. Reading JS Ch. 5.2 – 5.3 (Recursion, Design), Ch. 6 (Sorting) Objectives Induction Correctness of Selection sort Complexity of Selection sort. Announcements. Assignment 2 starter code posted How to copy the starter code to my workspace?
E N D
Today • Reading • JS Ch. 5.2 – 5.3 (Recursion, Design), Ch. 6 (Sorting) • Objectives • Induction • Correctness of Selection sort • Complexity of Selection sort
Announcements • Assignment 2 starter code posted • How to copy the starter code to my workspace? • JavaDoc – what’s expected? • Quiz on Friday • Big-O • Induction • Harvey Mudd Career Fair
Induction • Let P(n) be some proposition • To prove P(n) is true for all n ≥ 0 • (Step One) Base case: Prove P(0) • (Step Two) Assume P(k) is true for k ≥ 0 • (Step Three) Use this assumption to prove P(k+1).
Induction • Practice Examples • Prove 1+ 2 + … + n = [n(n+1)]/2 for all n ≥ 0 • Prove 20 + 21 + …+ 2n = 2n+1 – 1 for all n ≥ 0 • Prove 2n < n! for all n ≥ 4 • Induction can be used to prove • Mathematical statements • Correctness of an algorithm • Complexity of an algorithm
Selection Sort 30 10 26 34 18 5 20 14 14 5 30 10 26 34 18 20 1. Find smallest 2. Swap 3. Repeat 5 10 30 26 34 18 20 14 5 10 14 26 34 18 20 30 5 10 14 18 34 26 20 30
Selection Sort /** * Sorts an integer array using iterative selection sort * @param array array of integers to be sorted */ private static void selectionSortIterative(int[] array) { for(inti = 0; i < array.length; ++i) { int min = indexOfSmallest(array, i); swap(array, i, min); } }
Selection Sort (helper) /** * @param array array of integers * @paramstartIndex valid index into array * @return index of smallest value in array[startIndex...n] */ protected static intindexOfSmallest(int[] array, intstartIndex) { int smallest = startIndex; for(inti = startIndex+1; i < array.length; ++i) { if(array[i] < array[smallest]) { smallest = i; } } return smallest; }
Correctness of Selection Sort using Induction (on board) • Consider what must be true after every iteration of the for-loop in selectionSortIterative
Complexity of Selection sort using Induction (on board) • Count the number of comparisons performed for each iteration of the for-loop in selectionSortIterative
Strong Induction • Sometimes need to assume more than just the previous case, so instead • Prove P(0) • For n > 0, use P(k) for all k < n as assumption in order to prove P(n).
Proof Example • fastPower(x,n) algorithm to calculate xn: • if n == 0 then return 1 • if n is even, return fastPower(x*x,n/2) • if n is odd, return x*fastPower(x,n-1) • Proof by induction on n (on board) • Base case: n ==0 • Induction case: Assume fastPower(x,k) is xk for all k < n.Show fastPower(x,n) is xn
InsertionSort • Similar: To sort array of n elements: • Sort first n-1 elements • Insert last element in correct position • How long to insert new element into sorted list of n elements?
Complexity of Selection sort using Induction • SelectionSort(array, n-1) • n = array.length • Takes time n(n-1)/2, • Results in O(n2) complexity • Iterative version of selection sort is in text.
Complexity of Selection sort using Induction • Count number of comparisons of elts from array • All comparisons are in indexOfLargest(array,n) • At most n comparisons. • Prove # of comparisons in selectionSort(array,n) is 1 + 2 + ... + n. • Base case: n = 0: No comparisons • Assume true for selectionSort(array,k-1): 1 + 2 + ... + (k-1) • Show for k elements: • indexOfLargest(array,k) takes k comparisons, • swap takes none. • By induction selectionSort(array,k-1) takes 1 + 2 +...+(k-1). • Therefore total: 1 + 2 + ... + (k-1) + k
Complexity of Selection Sort • Count the number of comparisons of elements
Weekly Assignment 2 • Build Frequency Lists: Draw pictures. • Significantly harder than last lab. Use TA’s on Wednesday and Thursday nights. • Lab will be filled on Sunday night with CS 51 students! • Friday quiz
Correctness • For all n ≥ 0 where n < array.length, after running selectionSort(array,n), array[0..n] is sorted in non-descending order. • P(n): After running selectionSort(array, n), array[0...n] is sorted in non-descending order. • Base case: prove P(0) • selectionSort(array,0) does nothing, but array[0..0] has only one element and hence is in order.
Induction Case • Suppose P(k) is true, i.e. after calling selectionSort(array,k), the array[0..k] is sorted in non-descending order • Prove P(k+1). • Call of selectionSort(array,k+1) starts by finding index of largest element in array[0..k+1] and swaps with element in array[k+1]. • By induction assumption, recursive call of selectionSort(array,k) leaves array[0..k] in order, and array[k+1] is larger, so array[0..k+1] is in order. ✔