lecture-16

last node of level h 1 ie element n2 cpsc 223 fall

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: ons … –  We can improve efficiency by: •  Treating the input array as a heap •  Incrementally rebuild the heap “bottom up” •  Starting at the (approx.) last node of level h – 1 (i.e., element n/2) CPSC 223  ­ ­ Fall 2010 5 Heapsort •  Building the Heap more efficiently –  Recall that a “Semiheap” is a tree whose subtrees are heaps but the root node may be out of place –  We can rebuild a semiheap via “trickle down” –  Here we assume a variant to heapRebuild: // rebuild semiheap stored in array at index void HeapRebuild(int theArray, int size, int index); CPSC 223  ­ ­ Fall 2010 6 3 10/26/10 Heapsort •  Building the Heap more efficiently 6 array = 6 3 5 9 2 10 0 1 2 3 4 3 5 5 9 2 10 HeapRebuild(array, 6, 2); a semiheap 6 array = 6 3 10 9 2 5 0 1 4 5 2 3 3 9 10 2 5 CPSC 223  ­ ­ Fall 2010 7 Heapsort •  Building the Heap more efficiently 6 array = 6 3 10 9 2 5 0 1 4 5 2 3 3 9 HeapRebuild(array, 6, 1); 10 2 5 a semiheap 6 array = 6 9 10 3 2 5 0 1 4 5 2 3 9 3 CPSC 223  ­ ­ Fall 2010 10 2 5 8 4 10/26/10 Heapsort •  Building the Heap more efficiently 6 array = 6 9 10 3 2 5 0 1 4 5 2 3 9 3 HeapRebuild(array, 6, 0); 10 2 5 a semiheap 10 array = 10 9 0 1 6 3 2 5 2 3 4 5 9 3 CPSC 223 ...
View Full Document

Ask a homework question - tutors are online