End of Term Review 2.0

End of Term Review 2.0 - Merge Sort Merge two sorted lists...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the 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 Merge-Sort The height h of the merge-sort 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 merge-sort 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 Heap-Sort Algorithm Build an array-based (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 Heap-Sort Running Time The heap can be built bottom-up 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 Quick-Sort Quick-sort is a divide-andconquer 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: Quick-sort 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

Page1 / 5

End of Term Review 2.0 - Merge Sort Merge two sorted lists...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online