This preview shows pages 1–2. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.View Full Document
Unformatted text preview: 600.363/463 Solution to Homework 2 Due: Wednesday, 9/24 6.4-3 We want the output of heap sort to be in increasing order. Even if the input array A is sorted in increasing order, each call to MAX HEAPIFY( A, 1) in the procedure HEAPSORT( A ) still takes O (log n ) time. So the running time of heap sort is dominated by this and hence is O ( n log n ). The cost of MAX HEAPIFY does not change for the case where the input is sorted in decreasing order and heap sort still runs in O ( n log n ) time. 6-2 Analysis of d-ary heaps 1. A d-ary heap can be represented in a 1-dimensional array as follows. The root is keep in A , its d children are kept in order in A  through A [ d +1], their children are kept in order in A [ d + 2] through A [ d 2 + d + 1], and so on. The two procedures that map a node with index i to its parent and to its j th child (for 1 j d ), respectively, are: D-ARY-PARENT( i ) return b ( i- 2) /d + 1 c D-ARY-CHILD( i, j ) return d ( i- 1) + j + 1 (To convenience yourself that these procedures really work, verify that: D-ARY-PARENT(D-ARY-CHILD( i, j ))= i , for any 1 j d . Notice that the binary eap procedures are a special case of the above procedures when d =2.) 2. Since each node has d children, the height of a d-ary heap with n nodes is (log d n )=(log n /lg d ). 3. The procedure HEAP-EXTRACT-MAX given in the text for binary heaps works fine for d-ary heaps too. The change needed to support d-ary heaps is in HEAPIFY, which must compare the argument node to all d children instead of just 2 children. The running time of HEAP-EXTRACT-MAX is still the running time for HEAPIFY, but that now takes worst-case...
View Full Document
This note was uploaded on 10/13/2009 for the course CS 5807 taught by Professor A during the Spring '09 term at Arkansas State.
- Spring '09