October 16th Foundation Exam

CS I Solutions


(1, 15%) The following (INCORRECT) algorithm is intended to be the iterative bubble sort algorithm. Assume that swap is a global procedure that exchanges the contents of its two parameters.

algorithm Bsort(n)

endalgorithm

It SHOULD take this first list…

8
3
10
7
5
4

…and produce this list.

3
4
5
7
8
10

1a) But, it doesn't. What does it produce?

3
5
7
8
10
4

1b) There is one error in the algorithm. In which line does it occur?

1c) Write the correct pseudo-code for the line identified in 1b).

There are three possible ways to fix the problem in this algorithm. Therefore, there are three correct answers to parts 1b) and 1c).


(2, 20%) The following are Postfix strings. All values are single decimal digits and the operations are binary add "+", binary subtract "-",and binary multiplication "*".

Indicate which are invalid, if any. For those that are valid, compute the result.

a)    9 4 + 2 3 * - 6 3 - 5 * + 7 -   = 15

b)    8 3 2 4 * + - 7 8 2 3 * - +   is invalid

There are not enough operators. 8 3 2 4 * + - gives the answer -3 and 7 8 2 3 * - + gives the answer 9, but there is no operator remaining to finish the calculation.

c)    3 2 + 4 2 * 7 5 - 2 3 * + - +   = 5

d) Given the following Postfix expression, show in the boxes below what the stack would contain immediately before the - operation is processed. Do not find the final result. Assume that the stack is initially empty.

The following operations are done before the - is reached:

The stack then contains the following items just before the - is processed:

(top of stack)

 

 

 

6

5

8


(3, 15%) The following insertion algorithm, Insert, must be invoked from the algorithm you are to write below. Assume that the variables j and x are local to the procedure and that k is an integer parameter.

Procedure Insert(k)

end

Write a recursive "InsertionSort" algorithm using Insert. You may assume a globally defined array a[1..n] of integer values that is already initialized.

procedure InsertionSort(n)

endprocedure


(4, 20%) Find the closed form or exact value for each of the following: ( n is an arbitrary positive integer):

4a)   å (3i+2) =    (i ranges from 0 to n.) = 3n2/2 + 7n/2 + 2

4b)   å (2i-3) =    (i ranges from 50 to 200.)   = 37297

4c)  S = 5 + 6 + 7 + 8 + … + (n-2) + (n-1)    = (n2 - n)/2 - 10

4d)   t(n) = t(n-1) + 2, where t(0) = 1    = 2n + 1


(5, 15%) Answer each of the following "timing" questions concerning an algorithm of a particular order and an instance of a particular size.

a) For an O(n) algorithm, an instance with n = 50 takes 4 seconds.

   How long will it take with n = 250?

(50/4) = (250/x)           thus, x = 20

b) For an O(n3) algorithm, an instance with n = 30 takes 20 seconds.

   How long will it take when n = 60?

(303/4) = (603/x)           thus, x = 160

c) For an O(2n) algorithm, an instance with n = 2 takes 12 seconds.

   How long will it take when n = 3?

(22/12) = (23/x)           thus, x = 24

Or, 3 = 2 + 1 and anytime you increase the power of 2 by 1, it doubles the amount.
So, increasing n by 1 doubles the time.


(6, 5%) Given the following procedure, fill in the blanks below to show the order in which the values of x will be output if the procedure is called with Print_it(5).

Show, in order, the output from executing the procedure:

If n = 1 is false, the procedure makes a recursive call to Print_it with the parameter (n - 1). It must then wait on the completion of that call before it can print its own number (n). If n = 1 is true, that "clone" of the procedure can print its number (which is 1) and does not make a recursive call to Print_it. Thus the recursion ends and Print_it(2) can print its number (which is 2) and end also. Then Print_it(3) gets to print, etc., until Print_it(5) prints and ends. So, the numbers are printed in the following order.

     1      2       3      4       5  


(7, 10%) P is the class of problems known to be solvable in polynomial time, NP is the class that is verifiable in polynomial time (i.e., given the correct solution to an instance, we can verify its validity in polynomial time), and EXP are problems we know require exponential time to solve and to verify.

Which is the most appropriate set for each of the following?


Back to the Oct 16 Foundation Exam page.