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

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

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

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

More on Sorting: Quick Sort and Heap Sort

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.

Design and Analysis of Algorithms

Problem Set 3 Solutions

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

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

CS240: Programming in C. Lecture 12: Polymorphic Sorting

CE 221 Data Structures and Algorithms

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

Sorting and Algorithm Analysis

CS1100 Introduction to Programming

CHAPTER 10: ALGORITHM DESIGN TECHNIQUES

Esc101 Lecture 1 st April, 2008 Generating Permutation

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

Sorting. Sorted Original. index. index

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

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

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

Programming in Fortran 90 : 2017/2018

Brave New World Pseudocode Reference

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.

A Binarization Algorithm specialized on Document Images and Photos

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

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

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

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

Load Balancing for Hex-Cell Interconnection Network

Report on On-line Graph Coloring

Hierarchical clustering for gene expression data analysis

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

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

An Optimal Algorithm for Prufer Codes *

Intro. Iterators. 1. Access

CMPS 10 Introduction to Computer Science Lecture Notes

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

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

Parallelism for Nested Loops with Non-uniform and Flow Dependences

Fast Computation of Shortest Path for Visiting Segments in the Plane

5 The Primal-Dual Method

Biostatistics 615/815

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

Support Vector Machines

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

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

Learning the Kernel Parameters in Kernel Minimum Distance Classifier

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

Performance Evaluation of Information Retrieval Systems

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

ELEC 377 Operating Systems. Week 6 Class 3

Mathematics 256 a course in differential equations for engineering students

Hermite Splines in Lie Groups as Products of Geodesics

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

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

Loop Transformations, Dependences, and Parallelization

NUMERICAL SOLVING OPTIMAL CONTROL PROBLEMS BY THE METHOD OF VARIATIONS

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

Problem Definitions and Evaluation Criteria for Computational Expensive Optimization

Comparison Based Sorting Algorithms. Algorithms and Data Structures: Lower Bounds for Sorting. Comparison Based Sorting Algorithms

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

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

CHAPTER 2 DECOMPOSITION OF GRAPHS

Algorithms and Data Structures: Lower Bounds for Sorting. ADS: lect 7 slide 1

Private Information Retrieval (PIR)

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

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)

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

LOOP ANALYSIS. The second systematic technique to determine all currents and voltages in a circuit

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

Setup and Use. Version 3.7 2/1/2014

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

K-means and Hierarchical Clustering

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

TN348: Openlab Module - Colocalization

A Fast Content-Based Multimedia Retrieval Technique Using Compressed Data

Machine Learning. Topic 6: Clustering

Active Contours/Snakes

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

1 Dynamic Connectivity

Life Tables (Times) Summary. Sample StatFolio: lifetable times.sgp

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

AP PHYSICS B 2008 SCORING GUIDELINES

Smoothing Spline ANOVA for variable screening

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

Real-Time Guarantees. Traffic Characteristics. Flow Control

A fault tree analysis strategy using binary decision diagrams

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

Greedy Technique - Definition

Notes on Organizing Java Code: Packages, Visibility, and Scope

Efficient Multidimensional Searching Routines for Music Information Retrieval

Setup and Use. For events not using AuctionMaestro Pro. Version /7/2013

Transcription:

CSE 326: Data Structures Qucksort Comparson Sortng Bound Steve Setz Wnter 2009 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 length 0 or 1. 2 The steps of Qucksort Qucksort Example 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 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 Conquer 1 2 3 4 6 8 9 Conquer 1 2 3 4 5 6 8 9 [Wess] 3 4 3 4

Pvot Pckng and Parttonng Pckng the Pvot 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. 5 6 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. Place the pvot and the largest at the rght and the smallest at the left. 7 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. 8

Partonng In-place Setup: = start and = end of un-partoned elements: 0 1 4 9 7 3 5 2 6 8 Advance : Advance : Partonng In-place 0 1 4 2 7 3 5 9 6 8 0 1 4 2 7 3 5 9 6 8 Advance untl element pvot: 0 1 4 9 7 3 5 2 6 8 Swap : Advance : 0 1 4 2 5 3 7 9 6 8 0 1 4 2 5 3 7 9 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 9 Advance : >, swap n pvot, partton done! 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 10 Partton Pseudocode Partton(A[], left, rght) { v = A[rght]; // Assumes pvot value currently at rght = left; // Intalze left sde, rght sde ponters = rght-1; Qucksort Pseudocode Puttng the peces together: // 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 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); 11 12 Complexty for nput sze n?

QuckSort: Best case complexty 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, 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(A, left, pvotindex 1); Qucksort(A, pvotindex + 1, rght); 13 14 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. 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( ) 15 16

Parttonng wth Duplcates Setup: = start and = end of un-partoned elements: Advance untl element pvot: Parttonng wth Duplcates Advance,: Swap: Advance,: Advance untl element pvot: If >, then swap: 17 Swap: Advance,: Fnsh: 18 Parttonng wth Duplcates:Take Two Start = start and = end of un-partoned elements: Advance untl element > pvot (and n bounds): Advance untl element < pvot (and n bounds): Fnsh: 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? Can we do better? Is ths better? 19 20

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); else { InsertonSort(A, left, rght); CUTOFF = 10 s reasonable. 21 Propertes of Qucksort 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. 22 How fast can we sort? 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. Permutatons Suppose you are gven N elements Assume no duplcates How many possble orderngs can you get? Example: a, b, c (N = 3) Can we do any better? 23 24

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)L(2)(1)= N! possble orderngs Sortng Model Recall our basc sortng assumpton: We can only compare two elements at a tme. These comparsons prune the space of possble orderngs. We can represent these concepts n a 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 Every sortng algorthm corresponds to a decson tree Fnds correct leaf by choosng edges to follow e, by makng comparsons 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. 29 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

An Alternatve Explanaton Lower Bound on log(n!) At each decson pont, one branch has ½ of the optons remanng, the other has ½ remanng. Worst case: we always end up wth ½ remanng. Best algorthm, n the worst case: we always end up wth exactly ½ remanng. Thus, n the worst case, the best we can hope for s halvng the space d tmes (wth d comparsons), untl we have an answer,.e., untl the space s reduced to sze = 1. The space starts at N! n sze, and halvng d tmes means multplyng by 1/2 d, gvng us a lower bound on the worst case: N! d = 1 N! = 2 d= log 2( N!) d 2 33 34 Ω(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?) 35