# Topics For Midterm 2

• Sorting Algorithms
• O(n2) sorting algorithms: Insertion Sort, Selection Sort
• Shell Sort
• Divide and Conquer sorting algorithms: Mergesort and Quicksort
• Non-Comparison Sorting algorithms
• Bin/Bucket sort
• Counting Sort
• Hash Tables
• Hash functions
• Collision Strategies
• Open Hashing
• Closed Hashing
• Linear 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
• Topoligical Sort
• Graph Traversals
• Breadth-First Search (BFS)
• Depth-First 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

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

{
pubic int numVertex;
public Edge[] edges;
}

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