{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

# sort2 - Mergesort and Quicksort Mergesort and Quicksort Two...

This preview shows pages 1–4. Sign up to view the full content.

Robert Sedgewick and Kevin Wayne • Copyright © 2005 • http://www.Princeton.EDU/~cos226 Mergesort and Quicksort Reference: Chapters 7-8, Algorithms in Java, 3 rd Edition, Robert Sedgewick. 2 Mergesort and Quicksort Two great sorting algorithms. ! Full scientific understanding of their properties has enabled us to hammer them into practical system sorts. ! Occupies a prominent place in world's computational infrastructure. ! Quicksort honored as one of top 10 algorithms of 20 th century in science and engineering. Mergesort. ! Java sort for objects. ! Perl stable, Python stable. Quicksort. ! Java sort for primitive types. ! C qsort, Unix, g++, Visual C++, Perl, Python. Robert Sedgewick and Kevin Wayne • Copyright © 2005 • http://www.Princeton.EDU/~cos226 Mergesort 4 Mergesort Mergesort. ! Divide array into two halves. ! Recursively sort each half. ! Merge two halves to make sorted whole.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
5 Mergesort: Example 6 Merging Merging. Combine two pre-sorted lists into a sorted whole. How to merge efficiently? Use an auxiliary array. A G L O R H I M S T A G H I L M for ( int k = l ; k < r ; k ++) aux [ k ] = a [ k ]; int i = l , j = m ; for ( int k = l ; k < r ; k ++) { if ( i >= m ) a [ k ] = aux [ j ++]; else if ( j >= r ) a [ k ] = aux [ i ++]; else if ( less ( aux [ j ], aux [ i ])) a [ k ] = aux [ j ++]; else if (less(aux[j], aux[i])) a [ k ] = aux [ i ++]; } i j k l r m aux[] a[] 7 Mergesort: Java Implementation public class Merge { private static void sort ( Comparable [] a , Comparable [] aux , int l , int r ) { if ( r <= l + 1 ) return ; int m = l + ( r - l ) / 2 ; sort ( a , aux , l , m ); sort ( a , aux , m , r ); merge ( a , aux , l , m , r ); } public static void sort ( Comparable [] a ) { Comparable [] aux = new Comparable [ a . length ]; sort ( a , aux , 0 , a . length ); } } l m r 10 11 12 13 14 15 16 17 18 19 8 Mergesort Analysis: Memory Q. How much memory does mergesort require? ! Original input array = N. ! Auxiliary array for merging = N. ! Local variables: constant. ! Function call stack: log 2 N. ! Total = 2N + O(log N). Q. How much memory do other sorting algorithms require? ! N + O(1) for insertion sort and selection sort. ! In-place = N + O(log N). Challenge for the bored. In-place merge. [Kronrud, 1969]
9 Mergesort Analysis: Running Time Def. T(N) = number of comparisons to mergesort an input of size N. Mergesort recurrence. Solution. T(N) = O(N log 2 N). ! Note: same number of comparisons for any input of size N. ! We prove T(N) = N log 2 N when N is a power of 2, and = instead of " . T ( N ) " 0 if N = 1 T N /2 # \$ ( ) solve left half 1 2 4 3 4 4 + T N /2 % ( ) solve right half 1 2 4 3 4 4 + N merging { otherwise ( ) * ) including already sorted 10 Proof by Recursion Tree T ( N ) = 0 if N = 1 2 T ( N /2) sorting both halves 1 2 4 3 4 + N merging { otherwise " # \$ % \$ T(N) T(N/2) T(N/2) T(N/4) T(N/4) T(N/4) T(N/4) T(2) T(2)

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 9

sort2 - Mergesort and Quicksort Mergesort and Quicksort Two...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online