CSE 326: Data Structures Quicksort Comparison Sorting Bound

Similar documents
CSE 326: Data Structures Quicksort Comparison Sorting Bound

Insertion Sort. Divide and Conquer Sorting. Divide and Conquer. Mergesort. Mergesort Example. Auxiliary Array

Today s Outline. Sorting: The Big Picture. Why Sort? Selection Sort: Idea. Insertion Sort: Idea. Sorting Chapter 7 in Weiss.

Sorting: The Big Picture. The steps of QuickSort. QuickSort Example. QuickSort Example. QuickSort Example. Recursive Quicksort

Sorting. Sorting. Why Sort? Consistent Ordering

Sorting Review. Sorting. Comparison Sorting. CSE 680 Prof. Roger Crawfis. Assumptions

CSCI 104 Sorting Algorithms. Mark Redekopp David Kempe

Quicksort. Part 1: Understanding Quicksort

Sequential search. Building Java Programs Chapter 13. Sequential search. Sequential search

Searching & Sorting. Definitions of Search and Sort. Linear Search in C++ Linear Search. Week 11. index to the item, or -1 if not found.

Course Introduction. Algorithm 8/31/2017. COSC 320 Advanced Data Structures and Algorithms. COSC 320 Advanced Data Structures and Algorithms

More on Sorting: Quick Sort and Heap Sort

Problem Set 3 Solutions

Design and Analysis of Algorithms

CE 221 Data Structures and Algorithms

CS240: Programming in C. Lecture 12: Polymorphic Sorting

Kent State University CS 4/ Design and Analysis of Algorithms. Dept. of Math & Computer Science LECT-16. Dynamic Programming

Sorting and Algorithm Analysis

CS221: Algorithms and Data Structures. Priority Queues and Heaps. Alan J. Hu (Borrowing slides from Steve Wolfman)

Esc101 Lecture 1 st April, 2008 Generating Permutation

CS1100 Introduction to Programming

Assignment # 2. Farrukh Jabeen Algorithms 510 Assignment #2 Due Date: June 15, 2009.

The Greedy Method. Outline and Reading. Change Money Problem. Greedy Algorithms. Applications of the Greedy Strategy. The Greedy Method Technique

CHAPTER 10: ALGORITHM DESIGN TECHNIQUES

Sorting. Sorted Original. index. index

Priority queues and heaps Professors Clark F. Olson and Carol Zander

Exercises (Part 4) Introduction to R UCLA/CCPR. John Fox, February 2005

Compiler Design. Spring Register Allocation. Sample Exercises and Solutions. Prof. Pedro C. Diniz

Programming in Fortran 90 : 2017/2018

6.854 Advanced Algorithms Petar Maymounkov Problem Set 11 (November 23, 2005) With: Benjamin Rossman, Oren Weimann, and Pouya Kheradpour

Report on On-line Graph Coloring

Brave New World Pseudocode Reference

CMPS 10 Introduction to Computer Science Lecture Notes

Complex Numbers. Now we also saw that if a and b were both positive then ab = a b. For a second let s forget that restriction and do the following.

News. Recap: While Loop Example. Reading. Recap: Do Loop Example. Recap: For Loop Example

Loop Transformations, Dependences, and Parallelization

The AVL Balance Condition. CSE 326: Data Structures. AVL Trees. The AVL Tree Data Structure. Is this an AVL Tree? Height of an AVL Tree

Load Balancing for Hex-Cell Interconnection Network

ON SOME ENTERTAINING APPLICATIONS OF THE CONCEPT OF SET IN COMPUTER SCIENCE COURSE

On Some Entertaining Applications of the Concept of Set in Computer Science Course

Intro. Iterators. 1. Access

Biostatistics 615/815

AMath 483/583 Lecture 21 May 13, Notes: Notes: Jacobi iteration. Notes: Jacobi with OpenMP coarse grain

An Optimal Algorithm for Prufer Codes *

A Binarization Algorithm specialized on Document Images and Photos

5 The Primal-Dual Method

Hierarchical clustering for gene expression data analysis

High level vs Low Level. What is a Computer Program? What does gcc do for you? Program = Instructions + Data. Basic Computer Organization

Dynamic Programming. Example - multi-stage graph. sink. source. Data Structures &Algorithms II

Parallelism for Nested Loops with Non-uniform and Flow Dependences

R s s f. m y s. SPH3UW Unit 7.3 Spherical Concave Mirrors Page 1 of 12. Notes

Fast Computation of Shortest Path for Visiting Segments in the Plane

Outline. Self-Organizing Maps (SOM) US Hebbian Learning, Cntd. The learning rule is Hebbian like:

Introduction to Programming. Lecture 13: Container data structures. Container data structures. Topics for this lecture. A basic issue with containers

LLVM passes and Intro to Loop Transformation Frameworks

Non-Split Restrained Dominating Set of an Interval Graph Using an Algorithm

Parallel Numerics. 1 Preconditioning & Iterative Solvers (From 2016)

For instance, ; the five basic number-sets are increasingly more n A B & B A A = B (1)

$OJRULWKPV. (Feodor F. Dragan) Department of Computer Science Kent State University

Q.1 Q.20 Carry One Mark Each. is differentiable for all real values of x

Problem Definitions and Evaluation Criteria for Computational Expensive Optimization

ELEC 377 Operating Systems. Week 6 Class 3

Support Vector Machines

Private Information Retrieval (PIR)

A Comparison of Top-k Temporal Keyword Querying over Versioned Text Collections

Harvard University CS 101 Fall 2005, Shimon Schocken. Assembler. Elements of Computing Systems 1 Assembler (Ch. 6)

CSE373: Data Structure & Algorithms Lecture 21: More Comparison Sorting. Aaron Bauer Winter 2014

CHAPTER 2 DECOMPOSITION OF GRAPHS

Mathematics 256 a course in differential equations for engineering students

Hermite Splines in Lie Groups as Products of Geodesics

08 A: Sorting III. CS1102S: Data Structures and Algorithms. Martin Henz. March 10, Generated on Tuesday 9 th March, 2010, 09:58

1 Dynamic Connectivity

Learning the Kernel Parameters in Kernel Minimum Distance Classifier

A New Exact Algorithm for Traveling Salesman Problem with Time Complexity Interval (O(n^4), O(n^3 2^n))

CSE 373 NOVEMBER 8 TH COMPARISON SORTS

Greedy Technique - Definition

Virtual Memory. Background. No. 10. Virtual Memory: concept. Logical Memory Space (review) Demand Paging(1) Virtual Memory

Inverse Kinematics (part 2) CSE169: Computer Animation Instructor: Steve Rotenberg UCSD, Spring 2016

Performance Evaluation of Information Retrieval Systems

Lecture 4: Principal components

CSE 326: Data Structures Sorting Conclusion

LU Decomposition Method Jamie Trahan, Autar Kaw, Kevin Martin University of South Florida United States of America

Lecture 3: Computer Arithmetic: Multiplication and Division

CSE 373: Data Structures and Algorithms

Real-Time Guarantees. Traffic Characteristics. Flow Control

CSE 373: Data Structures & Algorithms More Sor9ng and Beyond Comparison Sor9ng

Assembler. Shimon Schocken. Spring Elements of Computing Systems 1 Assembler (Ch. 6) Compiler. abstract interface.

Setup and Use. Version 3.7 2/1/2014

Assembler. Building a Modern Computer From First Principles.

Outline. Midterm Review. Declaring Variables. Main Variable Data Types. Symbolic Constants. Arithmetic Operators. Midterm Review March 24, 2014

cos(a, b) = at b a b. To get a distance measure, subtract the cosine similarity from one. dist(a, b) =1 cos(a, b)

A Fast Content-Based Multimedia Retrieval Technique Using Compressed Data

Lecture 5: Multilayer Perceptrons

AADL : about scheduling analysis

Discrete Applied Mathematics. Shortest paths in linear time on minor-closed graph classes, with an application to Steiner tree approximation

Subspace clustering. Clustering. Fundamental to all clustering techniques is the choice of distance measure between data points;

Simulation: Solving Dynamic Models ABE 5646 Week 11 Chapter 2, Spring 2010

Transaction-Consistent Global Checkpoints in a Distributed Database System

A SYSTOLIC APPROACH TO LOOP PARTITIONING AND MAPPING INTO FIXED SIZE DISTRIBUTED MEMORY ARCHITECTURES

Smoothing Spline ANOVA for variable screening

Transcription:

CSE 326: Data Structures Qucksort Comparson Sortng Bound Bran Curless Sprng 2008 Announcements (5/14/08) Homework due at begnnng of class on Frday. Secton tomorrow: Graded homeworks returned More dscusson of proect, Java and hash tables Readng for ths lecture: Chapter 7. 2 Qucksort Qucksort uses a dvde and conquer strategy, but does not requre the O(N) extra space that MergeSort does. Here s the dea for sortng array S: 1. Pck an element v n S. Ths s the pvot value. 2. Partton S-{v nto two dsont subsets, S 1 and S 2 such that: elements n S 1 are all v elements n S 2 are all v 3. Return concatenaton of QuckSort(S 1 ), v, QuckSort(S 2 ) Recurson ends when Qucksort( ) receves an array of The steps of Qucksort length 0 or 1. 3 [Wess] 4 S 13 13 81 92 43 65 31 57 0 43 31 65 26 57 26 92 75 75 0 81 select pvot value S 1 S 2 partton S S 1 S 2 0 13 26 31 43 57 65 75 81 92 S 0 13 26 31 43 57 65 75 81 92 QuckSort(S 1 ) and QuckSort(S 2 ) Presto! S s sorted

Qucksort Example Pvot Pckng and Parttonng 4 6 3 8 1 9 2 5 Dvde 5 4 2 3 1 6 9 8 Dvde 2 1 3 4 6 8 9 Dvde 1 element 3 4 Conquer 3 4 Conquer 1 2 3 4 6 8 9 Conquer 1 2 3 4 5 6 8 9 5 The trcky peces are: Pckng the pvot Goal: pck a pvot value that wll cause S 1 and S 2 to be roughly equal n sze. Parttonng Preferably n-place Dealng wth duplcates. 6 Pckng the Pvot Medan of Three Pvot 0 1 2 3 4 5 6 7 8 9 8 1 4 9 6 3 5 2 7 0 medanof3pvot( ) 0 1 4 9 7 3 5 2 6 8 Choose the pvot as the medan of three. 7 Place the pvot and the largest at the rght and the smallest at the left. 8

Qucksort Parttonng Need to partton the array nto left and rght sub-arrays such that: elements n left sub-array are pvot elements n rght sub-array are pvot Can be done n-place wth another two ponter method Sounds lke mergesort, but here we are parttonng, not sortng and we can do t n-place. 9 Partonng In-place Setup: = start and = end of un-partoned elements: 0 1 4 9 7 3 5 2 6 8 Advance untl element pvot: 0 1 4 9 7 3 5 2 6 8 Advance untl element pvot: 0 1 4 9 7 3 5 2 6 8 If >, then swap: 0 1 4 2 7 3 5 9 6 8 10 Advance : Advance : Swap : Advance : Advance : >, swap n pvot, partton done! Partonng In-place 0 1 4 2 7 3 5 9 6 8 0 1 4 2 7 3 5 9 6 8 0 1 4 2 5 3 7 9 6 8 0 1 4 2 5 3 7 9 6 8 0 1 4 2 5 3 7 9 6 8 0 1 4 2 5 3 6 9 7 8 S 1 pvot pvot S 2 pvot 11 Partton Pseudocode Partton(A[], left, rght) { v = A[rght]; // Assumes pvot value currently at rght = left; // Intalze left sde, rght sde ponters = rght-1; // Do ++, -- untl they cross, swappng values as needed whle (1) { whle (A[] < v) ++; whle (A[] > v) --; f ( < ) { Swap(A[], A[]); ++; --; else break; Swap(A[], A[rght]); // Swap pvot value nto poston return ; // Return the fnal pvot poston Complexty for nput sze n? 12

Qucksort Pseudocode Puttng the peces together: Qucksort(A[], left, rght) { f (left < rght) { medanof3pvot(a, left, rght); pvotindex = Partton(A, left+1, rght-1); Qucksort(A, left, pvotindex 1); Qucksort(A, pvotindex + 1, rght); QuckSort: Best case complexty Qucksort(A[], left, rght) { f (left < rght) { medanof3pvot(a, left, rght); pvotindex = Partton(A, left+1, rght-1); Qucksort(A, left, pvotindex 1); Qucksort(A, pvotindex + 1, rght); 13 14 Qucksort(A[], left, rght) { f (left < rght) { medanof3pvot(a, left, rght); pvotindex = Partton(A, left+1, rght-1); QuckSort: Worst case complexty Qucksort(A, left, pvotindex 1); Qucksort(A, pvotindex + 1, rght); QuckSort: Average case complexty Turns out to be O(n log n). See Secton 7.7.5 for an dea of the proof. Don t need to know proof detals for ths course. 15 16

Many Duplcates? An mportant case to consder s when an array has many duplcates. 0 1 2 3 4 5 6 7 8 9 8 6 6 6 6 6 6 6 6 0 medanof3pvot( ) Parttonng wth Duplcates Setup: = start and = end of un-partoned elements: Advance untl element pvot: Advance untl element pvot: 17 If >, then swap: 18 Parttonng wth Duplcates Advance,: Swap: Advance,: Parttonng wth Duplcates:Take Two Start = start and = end of un-partoned elements: Advance untl element > pvot (and n bounds): Swap: Advance,: Fnsh: 19 Advance untl element < pvot (and n bounds): Fnsh: Is ths better? 20

Parttonng wth Duplcates: Upshot It s better to stop advancng ponters when elements are equal to pvot, and then ust do swaps. Complexty of qucksort on an array of dentcal values? Important Tweak Inserton sort s actually better than qucksort on small arrays. Thus, a better verson of qucksort: Qucksort(A[], left, rght) { f (rght left CUTOFF) { medanof3pvot(a, left, rght); pvotindex = Partton(A, left+1, rght-1); Qucksort(A, left, pvotindex 1); Qucksort(A, pvotindex + 1, rght); Can we do better? 21 else { InsertonSort(A, left, rght); CUTOFF = 10 s reasonable. 22 Propertes of Qucksort How fast can we sort? O(N 2 ) worst case performance, but O(N log N) average case performance. Pure qucksort not good for small arrays. No teratve verson (wthout usng a stack). In-place, but uses auxlary storage because of recursve calls. Stable? Used by Java for sortng arrays of prmtve types. 23 Heapsort, Mergesort, and Bnary Tree Sort all have O(N log N) worst case runnng tme. These algorthms, along wth Qucksort, also have O(N log N) average case runnng tme. Can we do any better? 24

Sortng Model Recall our basc assumpton: we can only compare two elements at a tme we can only reduce the possble soluton space by half each tme we make a comparson Suppose you are gven N elements Assume no duplcates How many possble orderngs can you get? Example: a, b, c (N = 3) Permutatons How many possble orderngs can you get? Example: a, b, c (N = 3) (a b c), (a c b), (b a c), (b c a), (c a b), (c b a) 6 orderngs = 3 2 1 = 3! (.e., 3 factoral ) All the possble permutatons of a set of 3 elements For N elements N choces for the frst poston, (N-1) choces for the second poston,, (2) choces, 1 choce N(N-1)(N-2) (2)(1)= N! possble orderngs 25 26 Decson Tree Decson Trees b < c a < c c < a < b b > c a > c c < a < b a < b,, c < a < b,,, c < b < a a > b c < a b < c c < b < a c > a b > c c < b < a A Decson Tree s a Bnary Tree such that: Each node = a set of orderngs.e., the remanng soluton space Each edge = 1 comparson Each leaf = 1 unque orderng How many leaves for N dstnct elements? Only 1 leaf has the orderng that s the desred correctly sorted arrangement The leaves contan all the possble orderngs of a, b, c. 27 28

Decson Tree Example Decson Trees and Sortng b < c a < c c < a < b b > c a > c c < a < b a < b,, c < a < b,,, c < b < a actual order a > b c < a possble orders b < c c < b < a c > a b > c c < b < a 29 Every sortng algorthm corresponds to a decson tree Fnds correct leaf by choosng edges to follow e, by makng comparsons Each decson reduces the possble soluton space by one half We wll focus on worst case run tme. Observatons: Worst case run tme s maxmum number of comparsons. Maxmum number of comparsons s the length of the longest path n the decson tree,.e. the heght of the tree. 30 How many leaves on a tree? Suppose you have a bnary tree of heght h. How many leaves n a perfect tree? Lower bound on Heght A bnary tree of heght h has at most 2 h leaves Can prove formally by nducton A decson tree has N! leaves. What s ts mnmum heght of that tree? We can prune a perfect tree to make any bnary tree of same heght. Can # of leaves ncrease? 31 32

Lower Bound on log(n!) Ω(N log N) Worst case run tme of any comparsonbased sortng algorthm s Ω(N log N). Can also show that average case run tme s also Ω(N log N). Can we do better f we don t use comparsons? (Huh?) 33 34