This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: Merge Sort
Merge two sorted lists into one
25,31,52,88,98
14,23,25,30,31,52,62,79,88,98
14,23,30,62,79 CSE 2011
Prof. J. Elder  46  Last Updated: 4/1/10 2:37 PM Analysis of MergeSort
The height h of the mergesort tree is O(log n)
at each recursive call we divide in half the sequence, The overall amount or work done at the nodes of depth i is O(n)
we partition and merge 2i sequences of size n/2i
we make 2i+1 recursive calls Thus, the total running time of mergesort is O(n log n)
depth #seqs size
0 1 n 1 2 n/2 i 2i n/2i … … T (n) = 2T (n / 2) + O(n) … CSE 2011
Prof. J. Elder  47  Last Updated: 4/1/10 2:37 PM HeapSort Algorithm
Build an arraybased (max) heap
Iteratively call removeMax() to extract the keys in
descending order
Store the keys as they are extracted in the unused tail
portion of the array CSE 2011
Prof. J. Elder  48  Last Updated: 4/1/10 2:37 PM HeapSort Running Time
The heap can be built bottomup in O(n) time
Extraction of the ith element takes O(log(n  i+1)) time
(for downheaping)
Thus total run time is
T (n) = O(n) + n log(n i + 1) i =1 = O(n) + n log i
i =1 O(n) + n log n
i =1 = O(n log n)
CSE 2011
Prof. J. Elder  49  Last Updated: 4/1/10 2:37 PM QuickSort
Quicksort is a divideandconquer algorithm: x Divide: pick a random
element x (called a pivot)
and partition S into
L elements less than x x E elements equal to x
G elements greater than x L E G Recur: Quicksort L and G
Conquer: join L, E and G
x CSE 2011
Prof. J. Elder  50  Last Updated: 4/1/10 2:37 PM ...
View
Full
Document
This note was uploaded on 02/14/2012 for the course CSE 2011Z taught by Professor Elder during the Fall '11 term at York University.
 Fall '11
 Elder
 Data Structures, Sort

Click to edit the document details