1 / 26

Lecture 3 Induction & Sort(1)

Lecture 3 Induction & Sort(1). Algorithm design techniques: induction Selection sort, Insertion sort, Shell sort. ROADMAP. Algorithm design techniques: induction Permutation Majority element Radix sort Selection sort, Insertion sort, Shell sort ... Proofs and comparisons.

Download Presentation

Lecture 3 Induction & Sort(1)

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 3 Induction & Sort(1) • Algorithm design techniques: induction • Selection sort, Insertion sort, Shell sort ...

  2. ROADMAP • Algorithm design techniques: induction • Permutation • Majority element • Radix sort • Selection sort, Insertion sort, Shell sort ... • Proofs and comparisons Xiaojuan Cai

  3. Techniques based on Recursions • Induction or Tail recursion • Iteration, • Proof: invariants by induction • Non-overlapping subproblems • Divide-and-conquer • Overlapping subproblems • Dynamic programming Xiaojuan Cai

  4. Techniques based on Recursions • Induction or Tail recursion • Iteration, • Proof: invariants by induction • Non-overlapping subproblems • Divide-and-conquer • Overlapping subproblems • Dynamic programming Xiaojuan Cai

  5. Induction example Selection sort Problem size: 6 9, 8, 9, 6, 2, 56 Problem size: 5 2, 8, 9, 6, 9, 56 Algorithm Design: problem size n --> n-1 Correctness Proof: by mathematical induction. Xiaojuan Cai

  6. Permutation • Problem: Permuation • Input: an array of n elements • Output: the permutations of n elements 1 2 3 1 3 2 2 1 3 2 3 1 3 1 2 3 2 1 Xiaojuan Cai

  7. Permutation1 Xiaojuan Cai

  8. Permutation2 Xiaojuan Cai

  9. Quiz Which output is in alphabetical order? A. permutation1 B. permutation2 C. both D. none of above

  10. Observation If two different elements in the original sequence are removed, then the majority in the original sequence remains the majority in the new sequence. Finding majority element Problem: MajorityElement Input: a sequence of integers A[1...n] Output: An integer a in A that appears more than n/2 times, otherwise, output none. 1 2 3 3 4 2 2 2 3 2 none 1 5 1 1 4 2 1 3 1 1 1 Xiaojuan Cai

  11. Θ(n) Finding majority element Xiaojuan Cai

  12. Radix sort All numbers in the array consists of EXACTLYk digits. 7467 1247 3275 6792 9187 9134 4675 1239 6792 9134 3275 4675 7467 1247 9187 1239 9134 1239 1247 7467 3275 4675 9187 6792 9134 9187 1239 1247 3275 7467 4675 6792 1239 1247 3275 4675 6792 7467 9134 9187

  13. Proof of correctness Lemma (Invariants) In Radix sort, if the i-th digits are sorted, then the numbers consisting of i, i-1, ..,1 digits are sorted.. Proof. (Inductive proof) Base step. Lemma holds for i = 1 Inductive step. Induction Hypothesis. Assume for j < i, the lemma holds. Prove it holds for i. Xiaojuan Cai

  14. Where are we? • Algorithm design techniques: induction • Permutation • Majority element • Radix sort • Selection sort, Insertion sort, Shell sort ... • Proofs and comparisons Xiaojuan Cai

  15. ? Xiaojuan Cai

  16. ? Xiaojuan Cai

  17. ? Xiaojuan Cai

  18. Selection sort Lemma (Invariants) • Entries the left of ↑ (including ↑) fixed and in ascending order. • No entry to right of ↑ is smaller than any entry to the left of ↑. ↑ in final order Xiaojuan Cai

  19. Insertion sort Lemma (Invariants) • Entries to the left of ↑ (including ↑) are in ascending order. • Entries to the right of ↑ have not yet been seen. in order not yet seen ↑ Xiaojuan Cai

  20. Shell sort (3-1) 13 6 24 43 1 51 9 2 10 Correctness: The last round of shell sort is insertion sort. Xiaojuan Cai

  21. Comparison http://www.sorting-algorithms.com/ Xiaojuan Cai

  22. Stability and in-place Definition: Stability A stable sort preserves the relative order of items with equal keys. Definition: In-place A sorting algorithm is in-place if the extra memory it uses ≤ c log N. Xiaojuan Cai

  23. Comparison Xiaojuan Cai

  24. Conclusion • Algorithm design techniques: induction • Permutation • Majority element • Radix sort • Selection sort, Insertion sort, Shell sort ... • Proofs and comparisons Xiaojuan Cai

  25. Next permutation 1 3 2 Next_perm 2 1 3 1 4 3 2 5 Next_perm 1 4 3 5 2 Xiaojuan Cai

  26. Exercises • Download hw3.pdf from our course homepage • Due on next Tuesday Xiaojuan Cai

More Related