1 / 20

Lecture 7: Induction & Sorting

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?

farren
Download Presentation

Lecture 7: Induction & Sorting

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 7: Induction & Sorting

  2. Today • Reading • JS Ch. 5.2 – 5.3 (Recursion, Design), Ch. 6 (Sorting) • Objectives • Induction • Correctness of Selection sort • Complexity of Selection sort

  3. 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

  4. 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).

  5. 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

  6. 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

  7. 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); } }

  8. 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; }

  9. Correctness of Selection Sort using Induction (on board) • Consider what must be true after every iteration of the for-loop in selectionSortIterative

  10. Complexity of Selection sort using Induction (on board) • Count the number of comparisons performed for each iteration of the for-loop in selectionSortIterative

  11. 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).

  12. 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

  13. Extra Slides

  14. 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?

  15. 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.

  16. 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

  17. Complexity of Selection Sort • Count the number of comparisons of elements

  18. 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

  19. 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.

  20. 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. ✔

More Related