Computer Science 245: Data Structures and Algorithms

Topics For Midterm 2

Types of Questions

The second midterm will be similar in composition to the first midterm, in terms of question types. The topics will, of course, be a little different.

Sample Questions

  1. The following array is to be sorted using radix sort (base-10).  Show the result after sorting each digit:

    218 326 654 182 335 318 125 322 911 251 807 302 224 731 435 152

  2. You are about to partition the following  list, using the last element as a pivot:

    10 41 4 16 17 8 30 2 11 29 22 20

    Show the list after the partition step.

  3. The following elements are inserted into a hash table of size 11 (with a hash function h(x) = x % 11) (in this order):  25, 3, 14, 4, 36

    1. Show the resulting hash table, assuming open hashing (separate chaining, closed addressing)
    2. Show the resulting hash table, assuming closed hashing (open addressing) and linear probling
    3. Show the resulting hash table, assuming closed hashing (open addressing) and quadratic probling
    4. Show the resulting hash table, aassiming closed hashing (open addressing) and double hashing (second hash function: hash2(x) = (x % 5) + 1)

  4. Consider a hash table of size 10 (with a hash funtion h(x) = x % 10) that uses linear probing, with f(i) = i. The elements 13, 12, 22, 52 are inserted into an empty table

    1. Show the contents of the table after these 4 elements are inserted
    2. Assume that the 5th element to be inserted has a 1/10 change of hasing to 0, a 1/10 chance of hahsing to 1, a 1/10 change of hashing to 2, and so on. For each of the 10 table locations, give the probability that the 5th element will be placed at that location.
    3. Repeat parts a and b, assuming that the table uses quadradic probing, with f(i) = i * i.

  5. Consider the following (very silly) function that takes a graph as input:
    class Edge
    {
        public int neighbor;
        public int cost;
        public Edge next;
    }
    
    class Graph
    {
        public int numVertex;
        public Edge edges
    }
    
    int Strange(Graph G)
    {
        int total = 0;
        for (v = 0; v < G.numVertex;v++)
        {
            for (Edge tmp = G.edges[v]; tmp != null; tmp = tmp.next)
            {
                for (int i = 0; i < G.numVertex; i++)
                {
                    if (i == tmp.neighbor)
                        total += tmp.cost;
                }
            }
    
        }
        return total;
    }
    
    1. (5 points) What does this function compute?
    2. (5 points) What is the running time of this function, as a function of the number of vertices V and edges E in the graph?