Lecture 4 Sort(2) Merge sort Quick sort ACKNOWLEDGEMENTS: Some contents in this lecture source from...

23
Lecture 4 Sort(2) Merge sort Quick sort ACKNOWLEDGEMENTS: Some contents in this lecture source from COS226 of Princeton University by Kevin Wayne and Bob Sedgewick

Transcript of Lecture 4 Sort(2) Merge sort Quick sort ACKNOWLEDGEMENTS: Some contents in this lecture source from...

Lecture 4 Sort(2)

• Merge sort

• Quick sort

ACKNOWLEDGEMENTS: Some contents in this lecture source from COS226 of Princeton University by Kevin Wayne and Bob Sedgewick

Roadmap

• Merge sort• Quick sort

Merge sort

Merge sort

•Divide array into two halves.

•Recursively sort each half.

•Merge two halves.

2 613 24 1 51 9 1043

2 13 6 24

2 6 13 24

1 43 9 10

9 10 51

1 9 10 43 51

1 62 9 13 24 43 5110

Merging

Assume we need to merge two sorted arrays, with M, N elements respectively. Then

• How many comparisons in best case?• How many comparisons in worst case?

A. M+N B. max{M,N} C. min{M,N} D. M+N-1

2, 13, 43, 45, 89 6, 24, 51, 90, 93

2, 6, 13, 24, 43, 45, 51, 89, 90, 93

Complexity

insertion sort (N2) mergesort (N log N)computer thousand million billion thousand million billion

home instant2.8

hours317

yearsinstant

1 secon

d18 min

super instant1

second

1 week instantinstan

tinstant

•Laptop executes 108 compares/second.

•Supercomputer executes 1012 compares/second.

Good algorithms are better than supercomputers.

Discussion

• Mergesort is• stable?• in place?

• Recursions cost spaces.

Bottom-up merge sort

2 13 6 24 43 1 51 9

2 13 6 24 1 43 9 51

2 136 24 1 9 43 51

1 62 9 13 24 43 51

10

10

10

10

1 62 9 13 24 43 5110

QuizGive the array that results immediately after the 7th call to merge() by using:a) top-down mergesortb) bottom-up merge sort

M B X V Z Y H U N S I K

A. B M V X Y Z H N U S I K B. B M V X Y Z H N U S K IC. B M V X Y Z H U N S I K D. B M V X Y Z H N U S K I

QuizMergesort, BottomupMergesort, which one is more efficient?which one is easier for understanding and debugging?which one you prefer?

A. Mergesort B. BottomupMergesort

Where are we?

• Merge sort• Quick sort

Quick sortQuicksort honored as one of top 10 algorithms of 20th century in science and engineering.

Mergesort.

•Java sort for objects.

•Perl, C++ stable sort, Python stable sort, Firefox JavaScript, ...

Quicksort.

•Java sort for primitive types.

•C qsort, Unix, Visual C++, Python, Matlab, Chrome JavaScript, ...

Quick sort

Sir Charles Antony Richard Hoare

1980 Turing Award

Quick sort

•Shuffle the array.

•Partition so that, for some j -entry a[j] is in place-no larger entry to the left of j-no smaller entry to the right of j

•Sort each piece recursively.

Partition#1

Partition#1

10 13 6 24 43 1 51 9

i j

Partition #2- by Sedgewick

Complexity

Best case: array always split into two equal-sized subarrays.

similar to mergesort, O(NlogN)

Worst case: array always split into a 0-sized and an N-1-sized subarrays

similar to selection sort, O(N2)

Average case:

Complexity

insertion sort (N2) mergesort (N log N) quicksort (N log N)

computer thousand million billion thousand million billion thousand million billion

homeinstan

t2.8

hours317

yearsinstant

1 second

18 min

instant0.6 sec

12 min

superinstan

t1

second1 week instant instant

instant

instantinstan

tinstant

•Laptop executes 108 compares/second.

•Supercomputer executes 1012 compares/second.

Good algorithms are better than supercomputers. Great algorithms are better than good algorithms.

Duplicate keys

B A A B A B B B C C C

A A A A A A A A A A A

Dijkstra’s 3-way partition

QuizGive the array that results after applying quicksort partitioning to the following array by using:a) partition #1b) partition #2c) Dijkstra’s 3-way partition

H J B R H R H B C V S

A. C B H H B H R J R V SB. H C B B H H R R J V SC. C B B H H H R R V S JD. none of above

Sort summaryinplace? stable? worst average best remarks

selection x N 2 / 2 N 2 / 2 N 2 / 2 N exchanges

insertion x x N 2 / 2 N 2 / 4 N use for small N or partially ordered

shell x ? ? N tight code, subquadratic

quick x N 2 / 2 2 N ln N N lg NN log N probabilistic guarantee

fastest in practice

3-way quick x N 2 / 2 2 N ln N Nimproves quicksort in presence

of duplicate keys

merge x N lg N N lg N N lg N N log N guarantee, stable

Exercise

• Download hw4.pdf from our course homepage

• Due on Oct. 11

•Watch the week#4 video of

Algorithm(Part I) Coursera.

• Optional: Programming Assignment 4

04/18/23 Xiaojuan Cai 29