Sorting1.8 - Heap-Sort Algorithm Build an array-based (max)...

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: 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 - 41 - Last Updated: 4/1/10 11:16 AM Heap-Sort Algorithm Algorithm HeapSort(S) Input: S, an unsorted array of comparable elements Output: S, a sorted array of comparable elements T = MakeMaxHeap (S) for i = n-1 downto 1 S[i] = T.removeMax() CSE 2011 Prof. J. Elder - 42 - Last Updated: 4/1/10 11:16 AM Heap Sort Example CSE 2011 Prof. J. Elder - 43 - Last Updated: 4/1/10 11:16 AM 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 - 44 - Last Updated: 4/1/10 11:16 AM Quick-Sort 88 52 14 31 25 98 30 23 62 79 Divide and Conquer CSE 2011 Prof. J. Elder - 45 - Last Updated: 4/1/10 11:16 AM ...
View Full Document

Page1 / 5

Sorting1.8 - Heap-Sort Algorithm Build an array-based (max)...

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