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.43 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. 62 Analysis of dary heaps 1. A dary heap can be represented in a 1dimensional array as follows. The root is keep in A [1], its d children are kept in order in A [2] 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: DARYPARENT( i ) return b ( i 2) /d + 1 c DARYCHILD( i, j ) return d ( i 1) + j + 1 (To convenience yourself that these procedures really work, verify that: DARYPARENT(DARYCHILD( 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 dary heap with n nodes is (log d n )=(log n /lg d ). 3. The procedure HEAPEXTRACTMAX given in the text for binary heaps works fine for dary heaps too. The change needed to support dary heaps is in HEAPIFY, which must compare the argument node to all d children instead of just 2 children. The running time of HEAPEXTRACTMAX is still the running time for HEAPIFY, but that now takes worstcase...
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
 a
 Sort

Click to edit the document details