Docstoc

Homework2

Document Sample
Homework2 Powered By Docstoc
					Module 3

3.9 The following refer to iterators.

a. Why does a ListIterator object store a pointer to a List object?

b. What are the advantages of the iterator-based List class over the original pointer-based List from
chapter 4? Are there any disadvantages?

Module 5

5.1 Based on the O notation, approximately how many swaps and comparisons occur when SelectionSort
is called on a worst-case array of length 8?

a. 16
b. 64
c. 100
d. 800

5.2 Based on the O notation, approximately how many swaps and comparisons occur when MergeSort is
called on a worst-case array of length 8?

a. 8
b. 16
c. 24
d. 64

5.3 Both MergeSort and QuickSort are O(N log N) algorithms in the average case. However, MergeSort
has an advantage over QuickSort, which is:

a.   MergeSort requires less code to write
b.   MergeSort has a faster worst-case time
c.   MergeSort requires less memory
d.   MergeSort can work with larger arrays

5.4 Both MergeSort and QuickSort are O(N log N) algorithms in the average case. However, QuickSort
has an advantage over MergeSort, which is:

a. QuickSort requires less code to write
b. QuickSort has a faster worst-case time
c. QuickSort requires less memory
d. QuickSort can work with larger arrays

5.5 Trace the Partition function on the following array:

[0] [1] [2] [3] [4] [5] [6]
„F‟ „K‟ „A‟ „R‟ „C‟ „Y‟ „G‟

Assume that ChoosePivot returns without doing anything (the purpose of the function it to move the pivot
to the leftmost position in the subarray, so by doing nothing the leftmost item in the original array is the
pivot be default). What will be the state of the array after the call (elements shown left to right from [0] to
[6])?
a. „F‟ „A‟ „C‟ „R‟ „K‟ „Y‟ „G‟
b. „C‟ „A‟ „F‟ „R‟ „K‟ „Y‟ „G‟
c. „A‟ „C‟ „F‟ „K‟ „R‟ „Y‟ „G‟
d. „A‟ „C‟ „F‟ „G‟ „K‟ „R‟ „Y‟

5.6 Complete the implementation of the hybrid QuickSort/InsertionSort. Write a program to test this using
an array of about 200,000 integers; use an array large enough that time differences can be seen. Use the
clock() function before and after the call to QuickSort and subtract to calculate the CPU time spent
sorting. Write your program so that it runs this time trial 10 times. Run the program with various values for
Cutoff (including 0, in which QuickSort never calls InsertionSort ). Make a table describing your results.

Module 7

7.6 Find the minimum spanning tree rooted at e for the graph shown in figure 13-36, using Prim‟s
algorithm from Chapter 13. Show (or describe, like "edge from e to f") the edge that is added at each step.




Directions: Answer the following questions. Keep a copy of your work and include your name and
student number on every page.

1. Consider a variation of the ADT queue called a deque, or double-ended queue, in which items can be
inserted or deleted from either end. Construct a C++ class that implements this ADT. Write a short
program to test your ADT. Then create stack and single-ended queue classes as descendants of your
deque class.

2. Add recursive functions to BinaryTree that do the following (the data type of the nodes is int):

         Compute the height of a tree
         Find the maximum element
         Find the sum of the elements

Note that this question is about general binary trees, not binary search trees; you should not make any
assumptions about the ordering of the elements. Write a program that tests your procedures.

3. The fictitious company Global Tech Industries needs a system that tracks employees and clients.

All employees have a 9-digit employee identification number, first name and last name. Employees who
work in the high-security laboratory are classified as secure employees. A secure employee has a
clearance level of 1-9. Other employees may belong to an internal tech-support group and are classified
as support employees. A support employee is on 24-hour call and thus the employee‟s pager number (a
10-digit number which includes an area code) must be stored. Note that not all employees are classified
as support or secure.

All clients have a 5 character client identification code that may include letters and digits, and a first name
and last name.
For this assignment, use C++ classes object-oriented design to create a program to help Global Tech
Industries track employees and clients. You should create an abstract base class for a person that
encompasses the common data of employees and clients, then create classes for employees and clients
as descendants. Then, create classes for secure and support employees as descendants of employee.
Finally, create structures to store and retrieve employees and clients, and a program that allows the user
to interact with these structures. You should be able to:

        Enter an employee into the system. If the employee is classified as “secure” or “support,” then
         additional information must be requested and stored.
        Enter a client into the system.
        Retrieve an employee or client by entering the identification number or code.
        List all employees or clients (not in any particular order).

For this assignment, you are not required to store the employee or client information in a file. You can
assume the maximum number of employees and clients is fixed at 100 each.

4. Write a program to find the minimum spanning tree for a weighted undirected graph. Read triplets of
integers. The integers will represent two vertices and a weight. For example

3 6 12

means there is an edge between 3 and 6 (going both ways since this is an undirected graph) of cost 12.
Continue reading triplets until you read 0 0 0. Implement the minimum spanning tree algorithm for
weighted undirected graphs, as presented in Chapter 13. You can assume that all vertices are numbered
in the range 0-19 and that the graph represented by the input data is connected. The output should
consist of a list of edges of a minimum spanning tree, along with the cost of that tree.

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:4
posted:10/29/2011
language:English
pages:3