## Question 1

The following is an array representation of a priority queue.

Part 1: Assume that the queue is as pictured above. Draw the array representation of the priority queue when the maximum value is removed from the queue.

Part 2: Assume that the queue is as pictured above. Draw the array representation of the priority queue when the value 66 is inserted into the queue.

## Question 2

Use the following multiple choice answers to this question:

 a. O(1) b. O(log2(n)) c. O(n) d. O(n log2(n)) e. O(n2)) f. O(n3))

For each activity, what is the running time complexity of the activity.

• A: Adding an element to a heap with n elements.
• B: Creating a heap from an array of n elements.
• C: Converting a random number generated with drand48() to a uniformly distributed random number between 0 and n.
• D: Sorting a randomly generated array of n elements using merge sort.
• E: Sorting a randomly generated array of n elements using quicksort.
• F: Sorting a randomly generated array of n elements using insertion sort.
• G: Sorting a randomly generated array of n elements using selection sort.
• H: Sorting an array of n elements using merge sort, when the original array is in sorted order with the exception of three elements, which are out of order.
• I: Sorting an array of n elements using insertion sort, when the original array is in sorted order with the exception of three elements, which are out of order.
• J: Performing cycle detection on a graph with n nodes, with the restriction that each node has no more than 10 edges.

```
```

## Question 3

Suppose you are provided with the function merge() with the following prototype:

 ```void merge(double *a1, int size1, double *a2, int size2, double *a3); ```

merge() assumes that a1 is an array of size1 doubles, that a2 is an array of size2 doubles, and that a3 is an array of size1+size2 doubles. It also assumes that a1 and a2 are sorted. When it completes, a3 will contain all the values of a1 and a2 in sorted order.

Write the procedure merge_sort(), with the following prototype:

 ```void merge_sort(double *a, int size); ```

You may write a helper procedure or two if you need it.

Obviously, merge_sort() should use merge() to sort the size doubles in array a using the Merge Sort algorithm. Make sure that your procedure does not have a memory leak.

## Question 4

Behold the following undirected graph:

Which of the following are valid pre-order printings of the nodes given a depth-first traversal starting with the node Set. Answer all that are valid (there may be more than one).

 A: Set You Free Why Babe Me Don't D: Set Me Free Why Don't You Babe B: Set You Me Don't Free Babe Why E: Set You Don't Free Babe Why Me C: Set Me Why Babe You Don't Free F: Set Me Why Babe Free Don't You

Extra Credit: What's the name of the song and who sang it?

## Question 5

 ```#include #include #include #include using namespace std; class LNames { public: LNames(string ln, string init_fn); string lname; int n; set fns; }; typedef map LNMap; LNames::LNames(string ln, string init_fn) { lname = ln; n = 1; fns.insert(init_fn); cout << "Created " << ln << " with " << init_fn << endl; } ``` ```main() { LNMap lnames; LNames *l; string ln, fn; LNMap::iterator lnit; while (!cin.fail()) { cin >> fn; if (!cin.fail()) { cin >> ln; lnit = lnames.find(ln); if (lnit == lnames.end()) { l = new LNames(ln, fn); lnames.insert(make_pair(ln, l)); } else { l = lnit->second; l->fns.insert(fn); l->n++; cout << "Added " << fn << " to " << ln << endl; } } } // HERE } ```

What is the output of the above program when the following is given as standard input:

 ```James Woods Ickey Woods Woody Austin Tiger Woods Woody Woodpecker Steve Austin Woody Herman ```

## Question 6

Suppose in the program above, the line "// Here" was replaced with:
```  print_all_names(&lnames);
```
Write print_all_names(), which should print all the names, one per line, in the format:
```last, first
```
The output should be sorted by last name, and if two people share the same last name, then they should be printed in order of first name.

## Question 7

Suppose the cumulative distribution function of a probability distribution is:

CDF(x) = (x2)/9

for 0 ≤ x ≤ 3.

Part A: Why do we restrict x to be ≤ 3?

Part B: In English, tell me what it means that CDF(1) = 1/9.

Part C: When we choose a random number according to this probability distribution, which is more likely:

• a. A number less than zero.
• b. A number between 0 and 1.
• c. A number between 1 and 2.
• d. A number between 2 and 3.
• e. A number greater than 3.

Part D: Which of the following will set a to be a random number according to the probability distribution given by the above CDF. Assume that all variables used are doubles.

 a. { a = drand48() / sqrt(3.0); } b. { a = drand48() / 3.0; } c. { a = drand48() / 9.0; } d. { a = drand48() * sqrt(3.0); } e. { a = drand48() * 3.0; } f. { a = drand48() * 9.0; } g. { d = drand48(); a = d*d / sqrt(3.0); } h. { d = drand48(); a = d*d / 3.0; } i. { d = drand48(); a = d*d / 9.0; } j. { d = drand48(); a = d*d * sqrt(3.0); } k. { d = drand48(); a = d*d * 3.0; } l. { d = drand48(); a = d*d * 9.0; } m. { a = sqrt(drand48()) / sqrt(3.0); } n. { a = sqrt(drand48()) / 3.0; } o. { a = sqrt(drand48()) / 9.0; } p. { a = sqrt(drand48()) * sqrt(3.0); } q. { a = sqrt(drand48()) * 3.0; } r. { a = sqrt(drand48()) * 9.0; }