38
LECTURE 25: QUICK SORT CSC 213 – Large Scale Programming or

Lecture 25: Quick Sort

  • Upload
    mikaia

  • View
    46

  • Download
    0

Embed Size (px)

DESCRIPTION

or. CSC 213 – Large Scale Programming. Lecture 25: Quick Sort. Today’s Goals. Begin by discussing basic approach of quick sort Divide-and-conquer used , but how does this help? Merge sort also divides-and-conquers, what differs? Show execution tree & discuss meaning for quick sort - PowerPoint PPT Presentation

Citation preview

Page 1: Lecture 25: Quick Sort

LECTURE 25:QUICK SORT

CSC 213 – Large Scale Programming

or

Page 2: Lecture 25: Quick Sort

Today’s Goals

Begin by discussing basic approach of quick sort Divide-and-conquer used, but how does

this help? Merge sort also divides-and-conquers, what

differs? Show execution tree & discuss meaning for

quick sort Consider selection and important of the

pivot Quick sort rocks for which pivots and why? If we get really unlucky, when would quick

sort suck? How can we affect choice to insure we get

lucky Given all these facts: is quick sort

worth the risk?

Page 3: Lecture 25: Quick Sort

Quick (ungraded) Quiz

For what cases does quick sort suck?

When would quick sort execute fastest?

How does quick sort use divide-and-conquer?

Page 4: Lecture 25: Quick Sort

Divide-and-Conquer

Like all recursive algorithms, need base case Has immediate solution to a simple

problem Work is not easy and sorting 2+ items takes

work Already sorted 1 item since it cannot be out

of order Sorting a list with 0 items even easer

Recursive step simplifies problem & combines it Begins by splitting data into two Sequences Combine subSequences once they are

sorted

Page 5: Lecture 25: Quick Sort

Quick Sort

Divide: Partition by pivot L has values <= p G uses values >= p

Recur: Sort L and G Conquer: Merge L, p, G

p

Page 6: Lecture 25: Quick Sort

Quick Sort

Divide: Partition by pivot L has values <= p G uses values >= p

Recur: Sort L and G Conquer: Merge L, p, G

p

Page 7: Lecture 25: Quick Sort

Quick Sort

Divide: Partition by pivot L has values <= p G uses values >= p

Recur: Sort L and G Conquer: Merge L, p, G

p

L G

p

Page 8: Lecture 25: Quick Sort

Quick Sort

Divide: Partition by pivot L has values <= p G uses values >= p

Recur: Sort L and G Conquer: Merge L, p, G

p

p

L G

p

Page 9: Lecture 25: Quick Sort

Quick Sort v. Merge Sort

Quick Sort Merge Sort Work mostly splitting

data Cannot guarantee even

split Should skip some

comparisons

Does not need extra space Less work allocating

arrays

Blindly merges all the data Data already in sorted

order!

Blindly splits data in half Always gets even split

Needs* to use other arrays Wastes time in allocation Complex workaround

exists

Work mostly in merge step Combines two (sorted)

halves Always skips some

comparisons

Page 10: Lecture 25: Quick Sort

Execution Tree

Depicts divide-and-conquer execution Recursive call represented by each oval

node Original Sequence shown at start At the end of the oval, sorted Sequence

shown Initial call at root of the (binary) tree

Bottom of the tree has leaves for base cases

Page 11: Lecture 25: Quick Sort

Execution Example

Each call starts by selecting the pivot7 2 9 4 3 7 6 1 1 2 3 4 6 7 7

9

Page 12: Lecture 25: Quick Sort

Execution Example

Each call starts by selecting the pivot7 2 9 4 3 7 6 1 1 2 3 4 6 7 7

9

Page 13: Lecture 25: Quick Sort

Execution Example

Split into L & G partitions around pivot7 2 9 4 3 7 6 1 1 2 3 4 6 7 7

9

Page 14: Lecture 25: Quick Sort

Execution Example

Split into L & G partitions around pivot7 2 9 4 3 7 6 1 1 2 3 4 6 7 7

9

Page 15: Lecture 25: Quick Sort

Execution Example

Recursively solve L partition first7 2 9 4 3 7 6 1 1 2 3 4 6 7 7

9

Page 16: Lecture 25: Quick Sort

Execution Example

Recursively solve L partition first

2 4 3 1 1 2 3 4

7 2 9 4 3 7 6 1 1 2 3 4 6 7 7 9

Page 17: Lecture 25: Quick Sort

Execution Example

Select new pivot and split into L & G partitions

2 4 3 1 1 2 3 4

7 2 9 4 3 7 6 1 1 2 3 4 6 7 7 9

Page 18: Lecture 25: Quick Sort

Execution Example

Recursively solve for L

1 1

2 4 3 1 1 2 3 4

7 2 9 4 3 7 6 1 1 2 3 4 6 7 7 9

Page 19: Lecture 25: Quick Sort

Execution Example

Recursively solve for L & enjoy base case

1 1

2 4 3 1 1 2 3 4

7 2 9 4 3 7 6 1 1 2 3 4 6 7 7 9

Page 20: Lecture 25: Quick Sort

Execution Example

Now solve for G partition and select pivot

4 3 3 41 1

2 4 3 1 1 2 3 4

7 2 9 4 3 7 6 1 1 2 3 4 6 7 7 9

Page 21: Lecture 25: Quick Sort

Execution Example

Recursively solve for L

4 3 3 4

1 1

2 4 3 1 1 2 3 4

7 2 9 4 3 7 6 1 1 2 3 4 6 7 7 9

Page 22: Lecture 25: Quick Sort

Execution Example

Recursively solve for L & enjoy base case

4 3 3 4

1 1

2 4 3 1 1 2 3 4

7 2 9 4 3 7 6 1 1 2 3 4 6 7 7 9

Page 23: Lecture 25: Quick Sort

Execution Example

Now solve for G & enjoy base case

4 4

4 3 3 4

1 1

2 4 3 1 1 2 3 4

7 2 9 4 3 7 6 1 1 2 3 4 6 7 7 9

Page 24: Lecture 25: Quick Sort

Execution Example

Add L, pivot, & G to complete previous call

4 3 3 4

1 1

2 4 3 1 1 2 3 4

7 2 9 4 3 7 6 1 1 2 3 4 6 7 7 9

4 4

Page 25: Lecture 25: Quick Sort

Execution Example

Add L, pivot, & G to complete previous call

4 3 3 4

1 1

2 4 3 1 1 2 3 4

7 2 9 4 3 7 6 1 1 2 3 4 6 7 7 9

4 4

Page 26: Lecture 25: Quick Sort

Execution Example

Recursively sort G from original call

4 3 3 4

1 1

2 4 3 1 1 2 3 4

7 2 9 4 3 7 6 1 1 2 3 4 6 7 7 9

4 4

Page 27: Lecture 25: Quick Sort

Execution Example

Recursively sort G from original call

4 3 3 4

1 1

2 4 3 1 1 2 3 4

7 2 9 4 3 7 6 1 1 2 3 4 6 7 7 9

4 4

7 9 7 7 7 9

Page 28: Lecture 25: Quick Sort

Execution Example

Select pivot & partition into L & G

4 3 3 4

1 1

2 4 3 1 1 2 3 4

7 2 9 4 3 7 6 1 1 2 3 4 6 7 7 9

4 4

7 9 7 7 7 9

Page 29: Lecture 25: Quick Sort

Execution Example

Solve L recursively via base case & move to G

4 3 3 4

1 1

2 4 3 1 1 2 3 4

7 2 9 4 3 7 6 1 1 2 3 4 6 7 7 9

4 4

7 9 7 7 7 9

7 9 7 9

Page 30: Lecture 25: Quick Sort

Execution Example

Select pivot & partition into L & G

4 3 3 4

1 1

2 4 3 1 1 2 3 4

7 2 9 4 3 7 6 1 1 2 3 4 6 7 7 9

4 4

7 9 7 7 7 9

7 9 7 9

Page 31: Lecture 25: Quick Sort

Execution Example

Solve through two base cases in L & G

9 9

4 3 3 4

1 1

2 4 3 1 1 2 3 4

7 2 9 4 3 7 6 1 1 2 3 4 6 7 7 9

4 4

7 9 7 7 7 9

7 9 7 9

Page 32: Lecture 25: Quick Sort

Execution Example

Add L, pivot, & G to complete the call

9 9

4 3 3 4

1 1

2 4 3 1 1 2 3 4

7 2 9 4 3 7 6 1 1 2 3 4 6 7 7 9

4 4

7 9 7 7 7 9

7 9 7 9

Page 33: Lecture 25: Quick Sort

Execution Example

Add L, pivot, & G to complete the call

9 9

4 3 3 4

1 1

2 4 3 1 1 2 3 4

7 2 9 4 3 7 6 1 1 2 3 4 6 7 7 9

4 4

7 9 7 7 7 9

7 9 7 9

Page 34: Lecture 25: Quick Sort

Execution Example

Add L, pivot, & G to complete final call7 2 9 4 3 7 6 1 1 2 3 4 6 7 7

9

9 9

4 3 3 4

1 1

2 4 3 1 1 2 3 4

4 4

7 9 7 7 7 9

7 9 7 9

Page 35: Lecture 25: Quick Sort

Execution Example

Sorts data, but only as good as pivot choice7 2 9 4 3 7 6 1 1 2 3 4 6 7 7

9

4 3 3 4

1 1

2 4 3 1 1 2 3 4

4 4

7 9 7 7 7 9

7 9 7 9

9 9

Page 36: Lecture 25: Quick Sort

QUICK-SORT’S Pivot

Can select any element as pivot Pivot's existence required only, not where

or what it is QUICK-SORT'S ideal pivot is median

element Equal-sized L & G when median element

selected Makes complexity equal to MERGE-SORT Must sort data first to find the median,

however Smallest (or largest element) worst

pivot G (or L) holds all elements & other partition

empty Complexity becomes equal to INSERTION-

SORT

Page 37: Lecture 25: Quick Sort

Simple Pivot Selection

Could use first (or last) element as pivot It’s easy to code… …but also makes sorted data the worst

case… …which is common so this is a really bad

idea Could choose random element as pivot

Little harder to code, but expected to work well

Best is median of first, mid, & last items in list Nearly eliminates worst case, but much

harder to code

Page 38: Lecture 25: Quick Sort

For Next Lecture

Week #9 assignment posted so get working

Keep reviewing requirements for program #2 2nd preliminary deadline coming up so get

started Time developing good tests saves coding

time later Reading on radix sort for Monday

Is it possible to sort without comparisons? How is radix sort faster than O(n log n) lower

bound?