CSC 2300 Data Structures & Algorithms March 23, 2007 Chapter 7. Sorting.

Slides:



Advertisements
Similar presentations
David Luebke 1 4/22/2015 CS 332: Algorithms Quicksort.
Advertisements

Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
Theory of Computing Lecture 3 MAS 714 Hartmut Klauck.
CS 3343: Analysis of Algorithms Lecture 14: Order Statistics.
Introduction to Algorithms Jiafen Liu Sept
Chapter 4: Divide and Conquer Master Theorem, Mergesort, Quicksort, Binary Search, Binary Trees The Design and Analysis of Algorithms.
QuickSort 4 February QuickSort(S) Fast divide and conquer algorithm first discovered by C. A. R. Hoare in If the number of elements in.
Divide and Conquer. Recall Complexity Analysis – Comparison of algorithm – Big O Simplification From source code – Recursive.
Spring 2015 Lecture 5: QuickSort & Selection
Nattee Niparnan. Recall  Complexity Analysis  Comparison of Two Algos  Big O  Simplification  From source code  Recursive.
Quicksort CS 3358 Data Structures. Sorting II/ Slide 2 Introduction Fastest known sorting algorithm in practice * Average case: O(N log N) * Worst case:
Median/Order Statistics Algorithms
25 May Quick Sort (11.2) CSE 2011 Winter 2011.
Quicksort COMP171 Fall Sorting II/ Slide 2 Introduction * Fastest known sorting algorithm in practice * Average case: O(N log N) * Worst case: O(N.
Chapter 7: Sorting Algorithms
Introduction to Algorithms Chapter 7: Quick Sort.
1 Today’s Material Divide & Conquer (Recursive) Sorting Algorithms –QuickSort External Sorting.
CS 201 Data Structures and Algorithms Text: Read Weiss, § 7.7
Quicksort, Mergesort, and Heapsort. Quicksort Fastest known sorting algorithm in practice  Caveats: not stable  Vulnerable to certain attacks Average.
Lecture 7COMPSCI.220.FS.T Algorithm MergeSort John von Neumann ( 1945 ! ): a recursive divide- and-conquer approach Three basic steps: –If the.
Fundamentals of Algorithms MCS - 2 Lecture # 16. Quick Sort.
CS 206 Introduction to Computer Science II 12 / 09 / 2009 Instructor: Michael Eckmann.
Quicksort CSC 172 SPRING 2002 LECTURE 13. Quicksort The basic quicksort algorithm is recursive Chosing the pivot Deciding how to partition Dealing with.
Probabilistic (Average-Case) Analysis and Randomized Algorithms Two different but similar analyses –Probabilistic analysis of a deterministic algorithm.
Chapter 7: Sorting Algorithms
CSC 2300 Data Structures & Algorithms March 27, 2007 Chapter 7. Sorting.
CS2420: Lecture 10 Vladimir Kulyukin Computer Science Department Utah State University.
Quicksort CSC 172 SPRING 2004 LECTURE 10. Reminders  Project 2 (polynomial linked-list) is due, tomorrow  Wed, 18 th 5PM  Computer Science Office –
Sorting. Introduction Assumptions –Sorting an array of integers –Entire sort can be done in main memory Straightforward algorithms are O(N 2 ) More complex.
Unit 061 Quick Sort csc326 Information Structures Spring 2009.
Median, order statistics. Problem Find the i-th smallest of n elements.  i=1: minimum  i=n: maximum  i= or i= : median Sol: sort and index the i-th.
CSC 2300 Data Structures & Algorithms January 30, 2007 Chapter 2. Algorithm Analysis.
CSC 2300 Data Structures & Algorithms March 20, 2007 Chapter 7. Sorting.
Selection1. 2 The Selection Problem Given an integer k and n elements x 1, x 2, …, x n, taken from a total order, find the k-th smallest element in this.
Chapter 7 (Part 2) Sorting Algorithms Merge Sort.
Design and Analysis of Algorithms – Chapter 51 Divide and Conquer (I) Dr. Ying Lu RAIK 283: Data Structures & Algorithms.
CS 206 Introduction to Computer Science II 12 / 08 / 2008 Instructor: Michael Eckmann.
CS2420: Lecture 11 Vladimir Kulyukin Computer Science Department Utah State University.
Sorting What makes it hard? Chapter 7 in DS&AA Chapter 8 in DS&PS.
CS 146: Data Structures and Algorithms July 14 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
Sorting (Part II: Divide and Conquer) CSE 373 Data Structures Lecture 14.
DIVIDE & CONQUR ALGORITHMS Often written as first as a recursive algorithm Master’s Theorem: T(n) = aT(n/b) + cn i, for some constant integer i, and constants.
Divide-And-Conquer Sorting Small instance.  n
Quick Sort By: HMA. RECAP: Divide and Conquer Algorithms This term refers to recursive problem-solving strategies in which 2 cases are identified: A case.
Nattee Niparnan. Recall  Complexity Analysis  Comparison of Two Algos  Big O  Simplification  From source code  Recursive.
Order Statistics The ith order statistic in a set of n elements is the ith smallest element The minimum is thus the 1st order statistic The maximum is.
Computer Science 101 Fast Searching and Sorting. Improving Efficiency We got a better best case by tweaking the selection sort and the bubble sort We.
Quicksort, Mergesort, and Heapsort. Quicksort Fastest known sorting algorithm in practice  Caveats: not stable  Vulnerable to certain attacks Average.
1 Sorting Algorithms Sections 7.1 to Comparison-Based Sorting Input – 2,3,1,15,11,23,1 Output – 1,1,2,3,11,15,23 Class ‘Animals’ – Sort Objects.
Sorting What makes it hard? Chapter 7 in DS&AA Chapter 8 in DS&PS.
Sorting: Implementation Fundamental Data Structures and Algorithms Klaus Sutner February 24, 2004.
CS 146: Data Structures and Algorithms July 14 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
Divide And Conquer A large instance is solved as follows:  Divide the large instance into smaller instances.  Solve the smaller instances somehow. 
1 Heapsort, Mergesort, and Quicksort Sections 7.5 to 7.7.
Young CS 331 D&A of Algo. Topic: Divide and Conquer1 Divide-and-Conquer General idea: Divide a problem into subprograms of the same kind; solve subprograms.
CSC317 1 Quicksort on average run time We’ll prove that average run time with random pivots for any input array is O(n log n) Randomness is in choosing.
Quicksort Quicksort is a well-known sorting algorithm that, in the worst case, it makes Θ(n 2 ) comparisons. Typically, quicksort is significantly faster.
Quicksort CSC 172. Quicksort The basic quicksort algorithm is recursive Chosing the pivot Deciding how to partition Dealing with duplicates Wrong decisions.
1 Chapter 7 Quicksort. 2 About this lecture Introduce Quicksort Running time of Quicksort – Worst-Case – Average-Case.
Algorithm Design Techniques, Greedy Method – Knapsack Problem, Job Sequencing, Divide and Conquer Method – Quick Sort, Finding Maximum and Minimum, Dynamic.
Quicksort The basic quicksort algorithm is recursive Chosing the pivot
Selection Selection 1 Quick-Sort Quick-Sort 10/30/16 13:52
Chapter 4: Divide and Conquer
Quick Sort (11.2) CSE 2011 Winter November 2018.
CS 1114: Sorting and selection (part two)
Topic: Divide and Conquer
Data Structures & Algorithms
Design and Analysis of Algorithms
Presentation transcript:

CSC 2300 Data Structures & Algorithms March 23, 2007 Chapter 7. Sorting

Today – Sorting Quicksort –  Algorithm  Pivot  Analysis Worst Case Best Case Average Case

Quicksort – Algorithm 1. If the number of elements in S is 0 or 1, then return. 2. Pick any element v in S. This is called the pivot. 3. Partition S – {v} into two disjoint groups: S 1 = { x ε S – {v} | x ≤ v} and S 2 = { x ε S – {v} | x ≥ v}. 4. Return { quicksort(S 1 ) followed by v followed by quicksort(S 2 )}.

Quicksort – Example

Quicksort – Partition Strategy Example. Input: 8, 1, 4, 9, 6, 3, 5, 2, 7, 0. Say 6 is chosen as pivot ijpivot ij ij ij ij ij ij ij ij jipivot pivot

Choices of Pivot Four suggestions:  First element of array;  Larger of first two distinct elements of array;  Middle element of array;  Randomly. What do you think about these choices? All bad choices. Why?

Good Choice of Pivot Best choice: median of array. Disadvantage? Practical choice: Median of Three. What is it? Median of left, right, and center elements. Example: 8, 1, 4, 9, 6, 3, 5, 2, 7, 0. Median of 8, 6, and 0.

Example Example: 8, 1, 4, 9, 6, 3, 5, 2, 7, 0. Pivot = Median of 8, 6, and 0. What should new array look like? Recall what we have done: ij pivot Can we do better? i pivotj Where should we move pivot? ij pivot

Median-of-Three Code

Quicksort – Analysis Quicksort is recursive. We thus get a recurrence formula: T(0) = T(1) = 1, T(N) = T(i) + T(N – i – 1) + cN, where i denotes the number of elements in S 1. What value of i gives worst case? What value of i gives best case?

Worst Case Analysis We have i = 0, always. What does that say about the pivot? Always the smallest element. Recurrence becomes T(N) = T(0) + T(N – 1) + cN. Ignore T(0), and get T(N) = T(N – 1) + cN. Hence T(N – 1) = T(N – 2) + c(N – 1), T(N – 2) = T(N – 3) + c(N – 2), … T(2) = T(1) + c(2). We get T(N) = T(1) + c ∑ i = 1 + c [ N(N+1)/2 – 1] = O(N 2 ).

Best Case Analysis We have i = N/2, always. What does that say about the pivot? Always the median. Recurrence becomes T(N) = T(N/2) + T(N/2) + cN = 2 T(N/2) + cN. Do you remember how to solve this recurrence? Divide by N to get T(N)/N = T(N/2)/(N/2) + c. Thus, T(N/2)/(N/2) = T(N/4)/(N/4) + c, T(N/4)/(N/4) = T(N/8)/(N/8) + c, … T(2)/2= T(1)/1 + c. We get T(N)/N= T(1)/1 + c logN, and so T(N)= N + c N logN = O(N log N).

Average Case Analysis Always much harder than worst and best cases. What can we assume about the pivot? Assume that each of the sizes for S 1 is equally likely and thus has probability 1/N. The average value of T(i) is thus (1/N) ∑ T(j). What can we say about the value of T(N – i – 1)? Recurrence becomes T(N) = (2/N) ∑ T(j) + cN. Does this recurrence look familiar? When we did an internal path length analysis in Chapter 4 (Trees).

Average Case Analysis Recurrence: T(N) = (2/N) ∑ T(j) + cN. How can we solve this recurrence? Divide by N? No, multiply by N! We get this recurrence: N T(N) = 2 ∑ T(j) + cN 2. How do we get rid of the ∑ T(j) ? We use this recurrence: (N – 1)T(N – 1) = 2 ∑ T(j) + c(N – 1) 2. Subtracting one recurrence from the other, we get NT(N) – (N – 1)T(N – 1) = 2 T(N – 1) + c(2N – 1). Simplifying and dropping the c term, we get NT(N) = (N+1) T(N – 1) + 2cN.

Recurrence Recurrence: NT(N) = (N+1) T(N – 1) + 2cN. How can we solve this recurrence? Divide by N? Divide by N+1? No, divide by N(N+1)! We get this recurrence: T(N)/(N+1)= T(N – 1)/N + 2c/(N+1). What to do now? We can telescope: T(N – 1)/N= T(N – 2)/(N – 1) + 2c/N, T(N – 2)/(N – 1)= T(N – 3)/(N – 2) + 2c/(N – 1), … T(2)/3= T(1)/2 + 2c/3. We get this solution: T(N)/(N+1)= T(1)/2 + 2c ∑ (1/i). What does ∑ (1/i) equal? We get T(N) = O(N log N).