hw2-solns - Illinois Institute of Technology Department of...

Info iconThis preview shows pages 1–3. 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
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Illinois Institute of Technology Department of Computer Science Solutions to Homework Assignment 2 CS 430 Introduction to Algorithms Spring Semester, 2009 Due: Wednesday, February 11 1. Problem 6.4-3 on page 136 The problem is to get the running time of heapsort on an array A of length n that is already sorted in increasing order or decreasing order. We need to define the heapsort and refer HEAPSORT( A ) on page 136 to get its running time. Heapsort is to sort elements in the input array in increasing order or decreasing order in place. The im- plementation will probably be different per each person, but we will consider the running time through HEAPSORT( A ) on page 136. As the book mentions on page 136, HEAPSORT( A ) consists of two different procedures: BULID-MAX- HEAP and MAX-HEAPIFY. To get the running time, we need to figure out the running time of both BULID-MAX-HEAP and MAX-HEAPIFY. In the former, BULID-MAX-HEAP is to construct max-heap from the original input array, termed as A , and we can get the modified array, termed A . We can easily figure out the BULID-MAX-HEAP procedure makes the same A regardless of the original array A . In the latter, MAX-HEAPIFY is to reorder the array A to sort in increasing order. For both cases, MAX-HEAPIFY takes the same time, O ( nlgn ). In the case A is sorted in increasing order, ∑ b lgn c h =0 d n/ 2 h +1 e O ( h ) = O ( n ∑ b lgn c h =0 h/ 2 h ). ∑ b lgn c h =0 h/ 2 h = 1 / 2 + 2 / 2 2 + 3 / 2 3 + 4 / 2 4 + ... ≤ 1 / 2 + 2 / 2 2 + 2 / 2 2 + 4 / 2 3 + 4 / 2 4 + 8 / 2 5 + 8 / 2 6 + 8 / 2 7 + ... = 1 / 2 + 1 / 2 2 + (1 / 2 + 1 / 2 2 + 1 / 2 3 + 1 / 2 4 + ... ) = 1 / 2 + 1 / 4 + 1 = 1 . 75 (As n → ∞ ) In Big-Oh notation, we can ignore any coefficient which is constant, and O ( n ∑ b lgn c h =0 h/ 2 h ) = O ( n ). In the case A is sorted in decreasing order, we do not need to move since Parent( i ) ≥ Left( i ) and Parent( i ) ≥ Right( i ). However, we have to check all nodes in the input array A , we need to visit each node exactly once. Then, we do not care about the hight of each node, we only need to consider how many nodes are in the array, O ( n ). In conclusion, in either case, the running time depends on MAX-HEAPIFY, O ( nlgn ). CS 430—Spring, 2009 2 Solutions to Homework Assignment 2 2. Problem 7.4-5 on page 159 This problem is composed of two parts: argue that this sorting algorithm runs in...
View Full Document

This note was uploaded on 04/07/2009 for the course CS 430 taught by Professor Kapoor during the Spring '08 term at Illinois Tech.

Page1 / 4

hw2-solns - Illinois Institute of Technology Department of...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online