FinalReview-solns

# FinalReview-solns - CMPS 101 Final Review Problems...

This preview shows pages 1–3. Sign up to view the full content.

CMPS 101 Final Review Problems Solutions to Selected Problems 2. Trace HeapSort on the following arrays a. (9, 3, 5, 4, 8, 2, 5, 10, 12, 2, 7, 4) b. (5, 3, 7, 1, 10, 12, 19, 24, 5, 7, 2, 6) c. (9, 8, 7, 6, 5, 4, 3, 2, 1) Solution to (a): After performing BuildHeap on the array (9, 3, 5, 4, 8, 2, 5, 10, 12, 2, 7, 4) we get the array (12, 10, 5, 9, 8, 4, 5, 3, 4, 2, 7, 2). Drawn as an ACBT this looks like: 12 10 5 9 8 4 5 3 4 2 7 2 We repeatedly swap the root with the rightmost leaf, decrement HeapSize, and call Heapify() on the root. We show the array and the corresponding tree after each iteration of this process, placing a double vertical bar || within the array at the end of the heap. (10, 9, 5, 4, 8, 4, 5, 3, 2, 2, 7 || 12) 10 9 5 4 8 4 5 3 2 2 7 (9, 8, 5, 4, 7, 4, 5, 3, 2, 2 || 10, 12) 9 8 5 4 7 4 5 3 2 2 (8, 7, 5, 4, 2, 4, 5, 3, 2 || 9, 10, 12) 8 7 5 4 2 4 5 3 2

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

View Full Document
(7, 4, 5, 3, 2, 4, 5, 2 || 8, 9, 10, 12) 7 4 5 3 2 4 5 2 (5, 4, 5, 3, 2, 4, 2 || 7, 8, 9, 10, 12) 5 4 5 3 2 4 2 (5, 4, 4, 3, 2, 2
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 3

FinalReview-solns - CMPS 101 Final Review Problems...

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

View Full Document
Ask a homework question - tutors are online