CS302 Midterm, Fall, 2015

James S. Plank

Answers and Grading


Question 1: 15 points

This was the question on which the class did the worst. Everything comes from the enumeration lecture notes. Grading:


Question 2: 15 points

Grading

This was one of the few times that crowdsourcing didn't get every answer correct. Parts F and L tripped y'all up, as they required some precise thought and understanding of big-O:

abcdefghijklmnopqrstuvwxyz
A010300611200000000000001000
B0150500104700100000000000000
C17003146000100000000100000000
D0305300001300000000000000000
E180480110900001000000000000
F020102002258200000000000000
G2612167611001001011050000000
H001132000116000000000000000
I020000630100100000000020000
J00252512000002010000300000100
K150550050300000000000000000
L001257000214010000900000100
M011011000106400000000000000
N011024000006000000100000000
O050900520000101100000000000


Question 3: 15 points

A-1: No:

A-2: Yes:

A-3: Yes:

A-4: No:

Part B:

Part C:

Grading


Question 4: 14 points

Selection sort:

Pass Action Result Letter
1 Swap 10 with 10 (do nothing) 10, 47, 99, 64, 77, 35, 56, 21 w
2 Swap 21 with 47 10, 21, 99, 64, 77, 35, 56, 47 j
3 Swap 35 with 99 10, 21, 35, 64, 77, 99, 56, 47 g
4 Swap 47 with 64 10, 21, 35, 47, 77, 99, 56, 64 e
5 Swap 56 with 77 10, 21, 35, 47, 56, 99, 77, 64 d
6 Swap 64 with 99 10, 21, 35, 47, 56, 64, 77, 99 a
7 Swap 77 with 77 (do nothing) 10, 21, 35, 47, 56, 64, 77, 99 a
8 Done 10, 21, 35, 47, 56, 64, 77, 99 a

Insertion Sort

Pass Action Result Letter
1 Insert 47 (do nothing) 10, 47, 99, 64, 77, 35, 56, 21 w
2 Insert 99 (do nothing) 10, 47, 99, 64, 77, 35, 56, 21 w
3 Insert 64 10, 47, 64, 99, 77, 35, 56, 21 v
4 Insert 77 10, 47, 64, 77, 99, 35, 56, 21 u
5 Insert 35 10, 35, 47, 64, 77, 99, 56, 21 o
6 Insert 56 10, 35, 47, 56, 64, 77, 99, 21 n
7 Insert 21 10, 21, 35, 47, 56, 64, 77, 99 a

Grading:


Question 5: 15 points

The easiest thing to do, of course, is Union by Size. The constructor is O(n), Union() is O(1), and Find() is O(log n). The other two implementations are in the lecture notes (with this one).

Disjoint::Disjoint(int nelements)
{
  links.resize(nelements, -1);
  ranks.resize(nelements, 1);
}

int Disjoint::Union(int s1, int s2)
{
  int p, c;

  if (ranks[s1] >= ranks[s2]) {
    p = s1;
    c = s2;
  } else {
    p = s2;
    c = s1;
  }

  links[c] = p;
  ranks[p] += ranks[c];
  return p;
}

int Disjoint::Find(int element)
{
  while(links[element] != -1) element = links[element];
  return element;
}

Grading:


Question 6: 15 points

Here it is with a main() stuck on it so you can test it. This was identical to a program that I gave you in a monday lab.

#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;

long long two_ks(int K, long long S)
{
  long long rv;

  if (K == 0) return 1 % S;

  if (K%2 == 1) return (2 * two_ks(K-1, S)) % S;

  rv = two_ks(K/2, S);
  return (rv * rv) %S;
}

main()
{
  int K;
  long long S;

  cin >> K >> S;

  cout << two_ks(K, S) << endl;
  exit(0);
}

Grading:

You started off with 15 points if your general structure was ok, and then you lost points for getting things wrong. Here were the major ones: