Lecture 3 Induction & Sort(1) Algorithm design techniques: induction Selection sort, Insertion sort, Shell sort...

Slides:



Advertisements
Similar presentations
Order Analysis of Algorithms Debdeep Mukhopadhyay IIT Madras.
Advertisements

Analysis of Algorithms
Analysis of Algorithms CS 477/677 Linear Sorting Instructor: George Bebis ( Chapter 8 )
CSE332: Data Abstractions Lecture 14: Beyond Comparison Sorting Dan Grossman Spring 2010.
Lecture 4 Sort(2) Merge sort Quick sort
CSC 2300 Data Structures & Algorithms March 16, 2007 Chapter 7. Sorting.
CSCE 3110 Data Structures & Algorithm Analysis
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 5.
Spring 2015 Lecture 5: QuickSort & Selection
Sorting Algorithms. Motivation Example: Phone Book Searching Example: Phone Book Searching If the phone book was in random order, we would probably never.
1 Sorting Problem: Given a sequence of elements, find a permutation such that the resulting sequence is sorted in some order. We have already seen: –Insertion.
CS 253: Algorithms Chapter 8 Sorting in Linear Time Credit: Dr. George Bebis.
Algorithm Design Techniques: Induction Chapter 5 (Except Sections 5.6 and 5.7)
Algorithm Design Techniques: Induction Chapter 5 (Except Section 5.6)
1 Data Structures A program solves a problem. A program solves a problem. A solution consists of: A solution consists of:  a way to organize the data.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (3) Recurrence Relation 11/11 ~ 11/14/2008 Yang Song.
Analysis of Algorithms CS 477/677
Analysis of Algorithms CS 477/677
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 5 Linear-time sorting Can we do better than comparison sorting? Linear-time sorting.
Section Section Summary Recursive Algorithms Proving Recursive Algorithms Correct Recursion and Iteration (not yet included in overheads) Merge.
Sorting Chapter 6 Chapter 6 –Insertion Sort 6.1 –Quicksort 6.2 Chapter 5 Chapter 5 –Mergesort 5.2 –Stable Sorts Divide & Conquer.
Computer Algorithms Lecture 11 Sorting in Linear Time Ch. 8
Sorting in Linear Time Lower bound for comparison-based sorting
CSE 373 Data Structures Lecture 15
HOW TO SOLVE IT? Algorithms. An Algorithm An algorithm is any well-defined (computational) procedure that takes some value, or set of values, as input.
1 More Sorting radix sort bucket sort in-place sorting how fast can we sort?
Lecture 2 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Elementary Sorting Algorithms Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
1 Chapter 7: Sorting (Insertion Sort, Shellsort) CE 221 Data Structures and Algorithms Izmir University of Economics Text: Read Weiss, § 7.1 – 7.4.
The Selection Problem. 2 Median and Order Statistics In this section, we will study algorithms for finding the i th smallest element in a set of n elements.
CSE332: Data Abstractions Lecture 14: Beyond Comparison Sorting Dan Grossman Spring 2012.
Fall 2015 Lecture 4: Sorting in linear time
Getting Started Introduction to Algorithms Jeff Chastine.
Sorting CS 110: Data Structures and Algorithms First Semester,
Algorithms IS 320 Spring 2015 Sorting. 2 The Sorting Problem Input: –A sequence of n numbers a 1, a 2,..., a n Output: –A permutation (reordering) a 1.
Sorting and Searching by Dr P.Padmanabham Professor (CSE)&Director
2IS80 Fundamentals of Informatics Fall 2015 Lecture 6: Sorting and Searching.
1 Ch. 2: Getting Started. 2 About this lecture Study a few simple algorithms for sorting – Insertion Sort – Selection Sort (Exercise) – Merge Sort Show.
Lecture 2 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
1 2/21/2016 MATH 224 – Discrete Mathematics Sequences and Sums A sequence of the form ar 0, ar 1, ar 2, ar 3, ar 4, …, ar n, is called a geometric sequence.
CSC317 1 So far so good, but can we do better? Yes, cheaper by halves... orkbook/cheaperbyhalf.html.
A Introduction to Computing II Lecture 7: Sorting 1 Fall Session 2000.
1 Computer Algorithms Tutorial 2 Mathematical Induction Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR.
1 Chapter 7: Sorting (Insertion Sort, Shellsort) CE 221 Data Structures and Algorithms Izmir University of Economics Text: Read Weiss, § 7.1 – 7.4.
Lecture 5 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
September 18, Algorithms and Data Structures Lecture II Simonas Šaltenis Aalborg University
Section Recursion 2  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
Section Recursion  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
CS6045: Advanced Algorithms Sorting Algorithms. Sorting Input: sequence of numbers Output: a sorted sequence.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 4.
Lecture 2 Algorithm Analysis
Recursive Algorithms Section 5.4.
Chapter 2 (16M) Sorting and Searching
Sorting in linear time Idea: if we can assume there are only k possible values to sort, we have extra information about where each element might need.
Analysis of Algorithms CS 477/677
Lecture 5 Algorithm Analysis
ICS 353: Design and Analysis of Algorithms
Lecture 3 Induction Algorithm design techniques: induction
Chapter 8-2: Sorting in Linear Time
Lecture 5 Algorithm Analysis
ICS 353: Design and Analysis of Algorithms
ICS 353: Design and Analysis of Algorithms
Lecture 5 Algorithm Analysis
Chapter 8-2: Sorting in Linear Time
Analysis of Algorithms
ICS 353: Design and Analysis of Algorithms
Lecture 5 Algorithm Analysis
Presentation transcript:

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 5/27/2016Xiaojuan Cai2

Techniques based on Recursions Induction or Tail recursion Iteration, Proof: invariants by induction Non-overlapping subproblems Divide-and-conquer Overlapping subproblems Dynamic programming 5/27/2016Xiaojuan Cai3

Techniques based on Recursions Induction or Tail recursion ‣ Iteration, ‣ Proof: invariants by induction Non-overlapping subproblems ‣ Divide-and-conquer Overlapping subproblems ‣ Dynamic programming 5/27/2016Xiaojuan Cai4

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

Permutation /27/2016Xiaojuan Cai6 Problem: Permuation Input: an array of n elements Output: the permutations of n elements

Permutation1 5/27/2016Xiaojuan Cai7

Permutation2 5/27/2016Xiaojuan Cai8

Permutation a1a1 a2a2 a3a3 a4a4 Permutation 1: Permutation 2: a1a1 a2a2 a3a3 a4a4 5/27/2016Xiaojuan Cai9

Permutation 1 5/27/2016Xiaojuan Cai10 Ω(n ⋅ n!)

Permutation 2 5/27/2016Xiaojuan Cai11 Ω(n ⋅ n!)

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

Polynomial Problem: PolynomialEval Input: a[0...n], and x Output: the value of p(x) William George Horner (source from Wikipedia) Horner’s rule 5/27/2016Xiaojuan Cai13

Horner’s rule 5/27/2016Xiaojuan Cai14

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 none 1 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. 5/27/2016Xiaojuan Cai15

Finding majority element Θ(n) 5/27/2016Xiaojuan Cai16

Proof of correctness Lemma (Invariants) Given a[1..N], Candidates(i) returns the majority element of a[i...N] if there exists a majority element. Proof. (Inductive proof) Base step. Lemma holds for i = N Inductive step. Induction Hypothesis. Assume for j > i, the lemma holds. Prove it holds for i. 5/27/2016Xiaojuan Cai17

Radix sort All numbers in the array consists of EXACTLY k digits

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. 5/27/2016Xiaojuan Cai19

Where are we? Algorithm design techniques: induction Permutation Majority element Radix sort Selection sort, Insertion sort, Shell sort... ‣ Proofs and comparisons 5/27/2016Xiaojuan Cai20

? 5/27/2016Xiaojuan Cai21

? 5/27/2016Xiaojuan Cai22

? 5/27/2016Xiaojuan Cai23

Selection sort i jmin Best caseWorst case #Comparison: #Interchange: 5/27/2016Xiaojuan Cai24

Selection sort in final order ↑ 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 ↑. 5/27/2016Xiaojuan Cai25

Insertion sort Best caseWorst case #Comparison: #Move: 5/27/2016Xiaojuan Cai26

Insertion sort in order ↑ not yet seen Lemma (Invariants) Entries to the left of ↑ (including ↑) are in ascending order. Entries to the right of ↑ have not yet been seen. 5/27/2016Xiaojuan Cai27

Shell sort (3-1) Correctness: The last round of shell sort is insertion sort. 5/27/2016Xiaojuan Cai28

Comparison worstaveragebestremarks selection N 2 / 2 N exchanges insertion N 2 / 2N 2 / 4Nfor small N or partially ordered shell ??Ntight code, subquadratic 5/27/2016Xiaojuan Cai29

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. in-place?stable? selection ?? insertion ?? shell ?? 5/27/2016Xiaojuan Cai30

Comparison in- place? stable?worst averag e bestremarks selection xN 2 / 2 N exchanges insertion xxN 2 / 2N 2 / 4N for small N or partially ordered shell x??Ntight code, subquadratic 5/27/2016Xiaojuan Cai31

Conclusion 5/27/2016Xiaojuan Cai32 Algorithm design techniques: induction Permutation Majority element Radix sort Selection sort, Insertion sort, Shell sort... ‣ Proofs and comparisons

Next permutation 5/27/2016Xiaojuan Cai Next_perm Next_perm

Exercises Download hw3.pdf from our course homepage Due on next Tuesday 5/27/2016Xiaojuan Cai34