Analysis of Algorithms CS 477/677 Sorting – Part B Instructor: George Bebis (Chapter 7)

Slides:



Advertisements
Similar presentations
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Advertisements

Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 6.
Comp 122, Spring 2004 Divide and Conquer (Merge Sort)
1 Divide-and-Conquer The most-well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances 2. Solve smaller instances.
1 Divide & Conquer Algorithms. 2 Recursion Review A function that calls itself either directly or indirectly through another function Recursive solutions.
September 12, Algorithms and Data Structures Lecture III Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Chapter 4: Divide and Conquer Master Theorem, Mergesort, Quicksort, Binary Search, Binary Trees The Design and Analysis of Algorithms.
Lecture 2: Divide and Conquer algorithms Phan Thị Hà Dương
Chapter 2. Getting Started. Outline Familiarize you with the to think about the design and analysis of algorithms Familiarize you with the framework to.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 5.
Spring 2015 Lecture 5: QuickSort & Selection
Analysis of Algorithms CS 477/677 Randomizing Quicksort Instructor: George Bebis (Appendix C.2, Appendix C.3) (Chapter 5, Chapter 7)
Quicksort Ack: Several slides from Prof. Jim Anderson’s COMP 750 notes. UNC Chapel Hill1.
Introduction to Algorithms Rabie A. Ramadan rabieramadan.org 4 Some of the sides are exported from different sources.
Quicksort Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
Lecture 8 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
CS 253: Algorithms Chapter 7 Mergesort Quicksort Credit: Dr. George Bebis.
Sorting. Input: A sequence of n numbers a 1, …, a n Output: A reordering a 1 ’, …, a n ’, such that a 1 ’ < … < a n ’
Ch. 7 - QuickSort Quick but not Guaranteed. Ch.7 - QuickSort Another Divide-and-Conquer sorting algorithm… As it turns out, MERGESORT and HEAPSORT, although.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
Chapter 4: Divide and Conquer The Design and Analysis of Algorithms.
Lecture 2: Divide and Conquer I: Merge-Sort and Master Theorem Shang-Hua Teng.
Analysis of Algorithms CS 477/677 Midterm Exam Review Instructor: George Bebis.
Quicksort CIS 606 Spring Quicksort Worst-case running time: Θ(n 2 ). Expected running time: Θ(n lg n). Constants hidden in Θ(n lg n) are small.
CS Main Questions Given that the computer is the Great Symbol Manipulator, there are three main questions in the field of computer science: What kinds.
Analysis of Algorithms CS 477/677
1 QuickSort Worst time:  (n 2 ) Expected time:  (nlgn) – Constants in the expected time are small Sorts in place.
Computer Algorithms Lecture 10 Quicksort Ch. 7 Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR.
10 Algorithms in 20th Century Science, Vol. 287, No. 5454, p. 799, February 2000 Computing in Science & Engineering, January/February : The Metropolis.
Chapter 7 Quicksort Ack: This presentation is based on the lecture slides from Hsu, Lih- Hsing, as well as various materials from the web.
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “ Introduction to the Design & Analysis of Algorithms, ” 2 nd ed., Ch. 1 Chapter.
10/14/ Algorithms1 Algorithms - Ch2 - Sorting.
1 Designing algorithms There are many ways to design an algorithm. Insertion sort uses an incremental approach: having sorted the sub-array A[1…j - 1],
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.
CMPT 438 Algorithms. Why Study Algorithms? Necessary in any computer programming problem ▫Improve algorithm efficiency: run faster, process more data,
Introduction to Algorithms Jiafen Liu Sept
Divide-and-Conquer The most-well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances 2.Solve smaller instances.
QuickSort (Ch. 7) Like Merge-Sort, based on the three-step process of divide- and-conquer. Input: An array A[1…n] of comparable elements, the starting.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 7.
COSC 3101A - Design and Analysis of Algorithms 2 Asymptotic Notations Continued Proof of Correctness: Loop Invariant Designing Algorithms: Divide and Conquer.
Divide-and-Conquer UNC Chapel HillZ. Guo. Divide-and-Conquer It’s a technique instead of an algorithm Recursive in structure – Divide the problem into.
1 Computer Algorithms Lecture 8 Sorting Algorithms Some of these slides are courtesy of D. Plaisted, UNC and M. Nicolescu, UNR.
COSC 3101A - Design and Analysis of Algorithms 4 Quicksort Medians and Order Statistics Many of these slides are taken from Monica Nicolescu, Univ. of.
2IS80 Fundamentals of Informatics Fall 2015 Lecture 6: Sorting and Searching.
David Luebke 1 2/19/2016 Priority Queues Quicksort.
 Design and Analysis of Algorithms تصميم وتحليل الخوارزميات (311 عال) Chapter 2 Sorting (insertion Sort, Merge Sort)
Sorting Algorithms Merge Sort Quick Sort Hairong Zhao New Jersey Institute of Technology.
Lecture # 6 1 Advance Analysis of Algorithms. Divide-and-Conquer Divide the problem into a number of subproblems Similar sub-problems of smaller size.
Nothing is particularly hard if you divide it into small jobs. Henry Ford Nothing is particularly hard if you divide it into small jobs. Henry Ford.
Sorting. 2 The Sorting Problem Input: A sequence of n numbers a 1, a 2,..., a n Output: A permutation (reordering) a 1 ’, a 2 ’,..., a n ’ of the input.
BITS Pilani Pilani Campus Data Structure and Algorithms Design Dr. Maheswari Karthikeyan Lecture3.
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.
Algorithms Sorting – Part 3.
CMPT 438 Algorithms.
Analysis of Algorithms CS 477/677
Algorithm Design & Analysis
CSC 413/513: Intro to Algorithms
Analysis of Algorithms CS 477/677
Ch 7: Quicksort Ming-Te Chi
Algorithms and Data Structures Lecture III
Lecture No 6 Advance Analysis of Institute of Southern Punjab Multan
CS 583 Analysis of Algorithms
Divide and Conquer (Merge Sort)
CS 3343: Analysis of Algorithms
Divide & Conquer Algorithms
Design and Analysis of Algorithms
Presentation transcript:

Analysis of Algorithms CS 477/677 Sorting – Part B Instructor: George Bebis (Chapter 7)

2 Sorting Insertion sort –Design approach: –Sorts in place: –Best case: –Worst case: Bubble Sort –Design approach: –Sorts in place: –Running time: Yes  (n)  (n 2 ) incremental Yes  (n 2 ) incremental

3 Sorting Selection sort –Design approach: –Sorts in place: –Running time: Merge Sort –Design approach: –Sorts in place: –Running time: Yes  (n 2 ) incremental No Let’s see!! divide and conquer

4 Divide-and-Conquer Divide the problem into a number of sub-problems –Similar sub-problems of smaller size Conquer the sub-problems –Solve the sub-problems recursively –Sub-problem size small enough  solve the problems in straightforward manner Combine the solutions of the sub-problems –Obtain the solution for the original problem

5 Merge Sort Approach To sort an array A[p.. r]: Divide –Divide the n-element sequence to be sorted into two subsequences of n/2 elements each Conquer –Sort the subsequences recursively using merge sort –When the size of the sequences is 1 there is nothing more to do Combine –Merge the two sorted subsequences

6 Merge Sort Alg.: MERGE-SORT (A, p, r) if p < r Check for base case then q ←  (p + r)/2  Divide MERGE-SORT (A, p, q) Conquer MERGE-SORT (A, q + 1, r) Conquer MERGE (A, p, q, r) Combine Initial call: MERGE-SORT (A, 1, n) p r q

7 Example – n Power of q = Divide

8 Example – n Power of Conquer and Merge

9 Example – n Not a Power of q = q = 9 q = Divide

10 Example – n Not a Power of Conquer and Merge

11 Merging Input: Array A and indices p, q, r such that p ≤ q < r –Subarrays A[p.. q] and A[q r] are sorted Output: One single sorted subarray A[p.. r] p r q

12 Merging Idea for merging: –Two piles of sorted cards Choose the smaller of the two top cards Remove it and place it in the output pile –Repeat the process until one pile is empty –Take the remaining input pile and place it face-down onto the output pile p r q A1  A[p, q] A2  A[q+1, r] A[p, r]

13 Example: MERGE(A, 9, 12, 16) prq

14 Example: MERGE(A, 9, 12, 16)

15 Example (cont.)

16 Example (cont.)

17 Example (cont.) Done!

18 Merge - Pseudocode Alg.: MERGE(A, p, q, r) 1.Compute n 1 and n 2 2.Copy the first n 1 elements into L[1.. n 1 + 1] and the next n 2 elements into R[1.. n 2 + 1] 3.L[n 1 + 1] ←  ; R[n 2 + 1] ←  4. i ← 1; j ← 1 5. for k ← p to r 6. do if L[ i ] ≤ R[ j ] 7. then A[k] ← L[ i ] 8. i ← i else A[k] ← R[ j ] 10. j ← j + 1 pq rq + 1 L R   p r q n1n1 n2n2

19 Running Time of Merge (assume last for loop) Initialization (copying into temporary arrays): –  (n 1 + n 2 ) =  (n) Adding the elements to the final array: - n iterations, each taking constant time   (n) Total time for Merge: –  (n)

20 Analyzing Divide-and Conquer Algorithms The recurrence is based on the three steps of the paradigm: –T(n) – running time on a problem of size n –Divide the problem into a subproblems, each of size n/b: takes D(n) –Conquer (solve) the subproblems aT(n/b) –Combine the solutions C(n)  (1) if n ≤ c T(n) = aT(n/b) + D(n) + C(n) otherwise

21 MERGE-SORT Running Time Divide: –compute q as the average of p and r: D(n) =  (1) Conquer: –recursively solve 2 subproblems, each of size n/2  2T (n/2) Combine: –MERGE on an n -element subarray takes  (n) time  C(n) =  (n)  (1) if n =1 T(n) = 2T(n/2) +  (n) if n > 1

22 Solve the Recurrence T(n) = cif n = 1 2T(n/2) + cnif n > 1 Use Master’s Theorem: Compare n with f(n) = cn Case 2: T(n) = Θ(nlgn)

23 Merge Sort - Discussion Running time insensitive of the input Advantages: –Guaranteed to run in  (nlgn) Disadvantage –Requires extra space  N

24 Sorting Challenge 1 Problem: Sort a file of huge records with tiny keys Example application: Reorganize your MP-3 files Which method to use? A.merge sort, guaranteed to run in time  NlgN B.selection sort C.bubble sort D.a custom algorithm for huge records/tiny keys E.insertion sort

25 Sorting Files with Huge Records and Small Keys Insertion sort or bubble sort? –NO, too many exchanges Selection sort? –YES, it takes linear time for exchanges Merge sort or custom method? –Probably not: selection sort simpler, does less swaps

26 Sorting Challenge 2 Problem: Sort a huge randomly-ordered file of small records Application: Process transaction record for a phone company Which sorting method to use? A.Bubble sort B.Selection sort C.Mergesort guaranteed to run in time  NlgN D.Insertion sort

27 Sorting Huge, Randomly - Ordered Files Selection sort? –NO, always takes quadratic time Bubble sort? –NO, quadratic time for randomly-ordered keys Insertion sort? –NO, quadratic time for randomly-ordered keys Mergesort? –YES, it is designed for this problem

28 Sorting Challenge 3 Problem: sort a file that is already almost in order Applications: –Re-sort a huge database after a few changes –Doublecheck that someone else sorted a file Which sorting method to use? A.Mergesort, guaranteed to run in time  NlgN B.Selection sort C.Bubble sort D.A custom algorithm for almost in-order files E.Insertion sort

29 Sorting Files That are Almost in Order Selection sort? –NO, always takes quadratic time Bubble sort? –NO, bad for some definitions of “almost in order” –Ex: B C D E F G H I J K L M N O P Q R S T U V W X Y Z A Insertion sort? –YES, takes linear time for most definitions of “almost in order” Mergesort or custom method? –Probably not: insertion sort simpler and faster

30 Quicksort Sort an array A[p…r] Divide –Partition the array A into 2 subarrays A[p..q] and A[q+1..r], such that each element of A[p..q] is smaller than or equal to each element in A[q+1..r] –Need to find index q to partition the array ≤ A[p…q]A[q+1…r]

31 Quicksort Conquer –Recursively sort A[p..q] and A[q+1..r] using Quicksort Combine –Trivial: the arrays are sorted in place –No additional work is required to combine them –The entire array is now sorted A[p…q]A[q+1…r] ≤

32 QUICKSORT Alg.: QUICKSORT (A, p, r) if p < r then q  PARTITION (A, p, r) QUICKSORT (A, p, q) QUICKSORT (A, q+1, r) Recurrence: Initially: p=1, r=n PARTITION()) T(n) = T(q) + T(n – q) + f(n)

33 Partitioning the Array Choosing PARTITION() –There are different ways to do this –Each has its own advantages/disadvantages Hoare partition (see prob. 7-1, page 159) –Select a pivot element x around which to partition –Grows two regions A[p…i]  x x  A[j…r] A[p…i]  xx  A[j…r] i j

34 Example ij ij ij ij ij A[p…r] ij A[p…q]A[q+1…r] pivot x=5

35 Example

36 Partitioning the Array Alg. PARTITION (A, p, r) 1. x  A[p] 2. i  p – 1 3. j  r while TRUE 5. do repeat j  j – 1 6. until A[j] ≤ x 7. do repeat i  i until A[i] ≥ x 9. if i < j 10. then exchange A[i]  A[j] 11. else return j Running time:  (n) n = r – p i j A: arar apap ij=q A: A[p…q]A[q+1…r] ≤ p r Each element is visited once!

37 Recurrence Alg.: QUICKSORT (A, p, r) if p < r then q  PARTITION (A, p, r) QUICKSORT (A, p, q) QUICKSORT (A, q+1, r) Recurrence: Initially: p=1, r=n T(n) = T(q) + T(n – q) + n

38 Worst Case Partitioning Worst-case partitioning –One region has one element and the other has n – 1 elements –Maximally unbalanced Recurrence: q=1 T(n) = T(1) + T(n – 1) + n, T(1) =  (1) T(n) = T(n – 1) + n = n n - 1 n - 2 n n n n n - 1 n  (n 2 ) When does the worst case happen?

39 Best Case Partitioning Best-case partitioning –Partitioning produces two regions of size n/2 Recurrence: q=n/2 T(n) = 2T(n/2) +  (n) T(n) =  (nlgn) (Master theorem)

40 Case Between Worst and Best 9-to-1 proportional split Q(n) = Q(9n/10) + Q(n/10) + n

41 How does partition affect performance?

42 How does partition affect performance?

43 Performance of Quicksort Average case –All permutations of the input numbers are equally likely –On a random input array, we will have a mix of well balanced and unbalanced splits –Good and bad splits are randomly distributed across throughout the tree Alternate of a good and a bad split Nearly well balanced split n n (n – 1)/2 n (n – 1)/2 + 1 Running time of Quicksort when levels alternate between good and bad splits is O(nlgn) combined partitioning cost: 2n-1 =  (n) partitioning cost: n =  (n)