Topics For Midterm 2
 Sorting Algorithms
 O(n^{2}) sorting algorithms: Insertion Sort, Selection Sort
 Shell Sort
 Divide and Conquer sorting algorithms: Mergesort and Quicksort
 NonComparison Sorting algorithms
 Bin/Bucket sort
 Counting Sort
 Radix Sort
 Hash Tables
 Hash functions
 Collision Strategies
 Open Hashing
 Closed Hashing
 Linear Probing
 Quadratic Probing
 Double Hashing
 Disjoint Sets
 Representing sets as trees
 Representing trees using parent array
 Union by Rank and Path Compression
 Graphs
 Definitions and Terminology
 Vertices (nodes), edges (arcs), Graph, Subgraph,
Directed/Undirected, Connected/Strongly Connected, Cycle / Acyclic / DAG
 Graph Representation
 Adjacency Graph
 Adjacency Matrix
 Topoligical Sort
 Graph Traversals
 BreadthFirst Search (BFS)
 DepthFirst Search (DFS)
 Shortest Path algorithms: Dijkstra
 How Algorithin works
 Known / Distance / Cost table
 Running time
 Spanning Tree Algorithms: Kruskal
 Analysis of Graph Algorithms
 Running time as a function of two parameters: Vertices and Edges
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.
 Trace through an algorithm, to show how it works on a particular
data set
 Check against visualizations
 These should be easy point questions!
 If we were to change an algorithm in a given way, would it
still work? Would the running time be the same?
 Small amount of coding (probably with graphs!)
 At least one "thinking question"
Sample Questions
 The following array is to be sorted using radix sort
(base10). Show the result after sorting each digit:
218 
326 
654 
182 
335 
318 
125 
322 
911 
251 
807 
302 
224 
731 
435 
152 
 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.
 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
 Show the resulting hash table, assuming open hashing
(separate chaining, closed addressing)
 Show the resulting hash table, assuming closed hashing (open addressing) and linear probling
 Show the resulting hash table, assuming closed hashing (open addressing) and quadratic probling
 Show the resulting hash table, aassiming closed hashing (open
addressing) and double hashing (second hash function: hash2(x) = (x %
5) + 1)
 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) {

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;
}

(5 points) What does this function compute?

(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?