260 likes | 422 Views
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.
E N D
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 Xiaojuan Cai
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
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
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
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
Permutation1 Xiaojuan Cai
Permutation2 Xiaojuan Cai
Quiz Which output is in alphabetical order? A. permutation1 B. permutation2 C. both D. none of above
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
Θ(n) Finding majority element Xiaojuan Cai
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
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
Where are we? • Algorithm design techniques: induction • Permutation • Majority element • Radix sort • Selection sort, Insertion sort, Shell sort ... • Proofs and comparisons Xiaojuan Cai
? Xiaojuan Cai
? Xiaojuan Cai
? Xiaojuan Cai
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
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
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
Comparison http://www.sorting-algorithms.com/ Xiaojuan Cai
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
Comparison Xiaojuan Cai
Conclusion • Algorithm design techniques: induction • Permutation • Majority element • Radix sort • Selection sort, Insertion sort, Shell sort ... • Proofs and comparisons Xiaojuan Cai
Next permutation 1 3 2 Next_perm 2 1 3 1 4 3 2 5 Next_perm 1 4 3 5 2 Xiaojuan Cai
Exercises • Download hw3.pdf from our course homepage • Due on next Tuesday Xiaojuan Cai