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. Given the following Data Strutures
    class Edge
    {
        public int neighbor;
        public Edge next;
    }
    
    class AdjListGraph
    {
        pubic int numVertex;
        public Edge[] edges;
    }
    
    class AdjMatrixGraph
    {
       public int numVertex;
       public boolean[][] adjMatrix;
    }
    
    Write a function that takes as input an adjacency list representation of a graph, and returns the adjacancy matrix representaion of the same graph.
    AdjMatrixGraph adjMatrixFromAdjList(AdjListGraph G) {
    
    
  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?