2 Solved Questions on Sorting Algorithms - Assignment | CS 3343, Assignments of Algorithms and Programming

Material Type: Assignment; Professor: Ruan; Class: Analysis Of Algorithms; Subject: Computer Science; University: University of Texas - San Antonio; Term: Spring 2008;

Typology: Assignments

Pre 2010

Uploaded on 07/30/2009

koofers-user-a9k
koofers-user-a9k 🇺🇸

9 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS 3343 (Spring 2008) Assignment 6
Solution
1. (40 points) Heap and heap sort.
a. (15 points) Study the pseudocode of BuildHeap and Heapify on Slide #11 and #23 in Lecture 13.
Use Slides #24 36 as a model, illustrate the operation of BuildHeap on array A = [5 3 17 10 15 19
6 22 9 28]. To save space and time, you only need to show the content of the tree after each call to
heapify(). (That is, slides 24, 25, 27, 29, 32, 36). The following partially filled trees are provided for
your convenience. Make sure your final result is indeed a heap.
5
3 17
10 15 19 6
22 9 28
5
3 17
10 28 19 6
22 9 15
5
3 17
22 28 19 6
10 9 15
5
3 19
22 28 17 6
10 9 15
5
28 19
22 15 17 6
10 9 3
28
22 19
10 15 17 6
5 9 3
1
pf3

Partial preview of the text

Download 2 Solved Questions on Sorting Algorithms - Assignment | CS 3343 and more Assignments Algorithms and Programming in PDF only on Docsity!

CS 3343 (Spring 2008) Assignment 6

Solution

  1. (40 points) Heap and heap sort.

a. (15 points) Study the pseudocode of BuildHeap and Heapify on Slide #11 and #23 in Lecture 13. Use Slides #24 – 36 as a model, illustrate the operation of BuildHeap on array A = [5 3 17 10 15 19 6 22 9 28]. To save space and time, you only need to show the content of the tree after each call to heapify(). (That is, slides 24, 25, 27, 29, 32, 36). The following partially filled trees are provided for your convenience. Make sure your final result is indeed a heap.

5

b. (5 points) Exercise # 6.3-2 in textbook page 135: Why do we want the loop index i in algorithm BuildHeap to decrease from blength(A)/ 2 c to 1 rather than increase from 1 to blength(A)/ 2 c?

To call Heapify(A, i), we need to make the assumption that the subtrees rooted at the left and right children of i are both heaps. This assumption is invalid if we start building the heap from A[1].

c. (10 points) Exercise # 6.4-3 in textbook page 136: What is the running time of heapsort on an array A of length n that is already sorted in increasing order? What about decreasing order? (Hint: first think about the cost for building heap in these two cases, then think about the cost for the actual sorting part. You can use examples [0 1 2 3 4 5 6 7 8 9] or [9 8 7 6 5 4 3 2 1 0] to help you think.)

In either case, buildHeap will take Θ(n) time: to build a heap using an array that is sorted in increasing order, we have to sift every elements down (worst-case seneraio), which is in Θ(n); to build a heap using an array that is sorted in decreasing order also takes Θ(n) time because we have to call Heapify n/2 times, even though each call to Heapify will return immediately and takes constant time. After the heap is constructed, each call to heapify takes Θ(log n) time. Therefore the total cost for heap sort is Θ(n log n), regardless of the initial order of the array.

d. (10 points) Starting from the heap below, show the content of the new heap after each heap operation.

20

Insert 19

ExtractMax

ExtractMax

  1. (30 points) Sorting algorithms. You are given a list of short words. Each word has length exactly five, and uses only the following four letters: a, c, g, and t. For example, aaact, acgtc, gctac, etc. (In fact, those are called “DNA words” that control the genes in our cells.) You want to sort the words alphabetically. However, you do not have a library function to sort strings, and you are too busy to write one by yourself. Fortunately the library developers have implemented a collection of popular algorithms for sorting integer arrays, including merge sort, quicksort, couting sort, heap sort, insertion sort, selection sort, etc.