Download presentation

Presentation is loading. Please wait.

Published byReginald Munt Modified about 1 year ago

1
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

2
Roadmap Merge sort Quick sort

3
Merge sort

4
Divide array into two halves. Recursively sort each half. Merge two halves

5
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, 896, 24, 51, 90, 93 2, 6, 13, 24, 43, 45, 51, 89, 90, 93

6
MERGE(A,p,q,r)..., 2, 13, 43, 45, 89, 6, 24, 51, 90, 93,... A pqr

7
Complexity Compute by picture. D (N)D (N) D ( N / 2) D ( N / 4) D (2) N D ( N / 2 k ) 2 ( N /2) 2 k ( N /2 k ) N /2 (2)... lg N N lg N = N... D (2) 4 ( N /4) = N D ( N ) = 2 D ( N /2) + N

8
Complexity Compute by expansion. D ( N ) = 2 D ( N /2) + N D ( N ) / N = 2 D ( N /2) / N + 1 = D ( N /2) / ( N /2) + 1 = D ( N /4) / ( N /4) = D ( N /8) / ( N /8) = D ( N / N ) / ( N / N ) = lg N D ( N ) = 2 D ( N /2) + N D ( N ) / N = 2 D ( N /2) / N + 1 = D ( N /2) / ( N /2) + 1 = D ( N /4) / ( N /4) = D ( N /8) / ( N /8) = D ( N / N ) / ( N / N ) = lg N D ( N ) = 2 D ( N /2) + N

9
Complexity insertion sort (N 2 )mergesort (N log N) comput er thousan d millionbillion thousan d millionbillion homeinstant 2.8 hours 317 years instant 1 secon d 18 min superinstant 1 secon d 1 weekinstant Laptop executes 10 8 compares/second. Supercomputer executes compares/second. Good algorithms are better than supercomputers.

10
Discussion Mergesort is stable? in place? Recursions cost spaces.

11
Bottom-up merge sort

12
Bottom-up merge sort

13
Quiz Give the array that results immediately after the 7th call to merge() by using: a) top-down mergesort b) 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 I C. 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

14
Quiz Mergesort, BottomupMergesort, which one is more efficient? which one is easier for understanding and debugging? which one you prefer? A. Mergesort B. BottomupMergesort

15
O(nlogn) sorting algorithms n=2 k Best caseWorst case Bottom-up-merge Merge Space Θ(n) Quicksort: worst O(n 2 ), average O(nlogn), space O(logn) Heapsort: worst O(nlogn), space O(1)

16
Sorting

17
Where are we? Merge sort Quick sort

18
Quicksort honored as one of top 10 algorithms of 20 th 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,...

19
Quick sort Sir Charles Antony Richard Hoare 1980 Turing Award

20
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.

21
Partition#1

22
i j

23
Partition #2 - by Sedgewick

24
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(N 2 ) Average case:

25
Complexity insertion sort (N 2 )mergesort (N log N)quicksort (N log N) compute r thous and millionbillion thousan d millionbillion thousan d millio n billion home instan t 2.8 hours 317 years instant 1 second 18 min instant 0.6 sec 12 min super instan t 1 second 1 weekinstant Laptop executes 10 8 compares/second. Supercomputer executes compares/second. Good algorithms are better than supercomputers. Great algorithms are better than good algorithms.

26
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

27
Quiz Give the array that results after applying quicksort partitioning to the following array by using: a) partition #1 b) partition #2 c) 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 S B. H C B B H H R R J V S C. C B B H H H R R V S J D. none of above

28
Sort summary inplace?stable?worstaveragebestremarks selection xN 2 / 2 N exchanges insertion xxN 2 / 2N 2 / 4Nuse for small N or partially ordered shell x??Ntight code, subquadratic quick xN 2 / 22 N ln NN lg N N log N probabilistic guarantee fastest in practice 3-way quick xN 2 / 22 N ln NN improves quicksort in presence of duplicate keys merge xN lg N N log N guarantee, stable

29
Exercise Download hw4.pdf from our course homepage Due on Oct. 11 Watch the week#4 video of Algorithm(Part I) Coursera.the week#4 video Optional: Programming Assignment 4Programming Assignment 4 5/3/2015Xiaojuan Cai29

Similar presentations

© 2016 SlidePlayer.com Inc.

All rights reserved.

Ads by Google