CS 361: Assignments |
Give pseudocode for sift-up, sift-down, insert, and delete to verify that all four operations still run correctly and in logarithmic time.
Problem 2.
Consider the following variation on double hashing for keys that have a total
ordering.
When attempting to insert an item, x, into the table, we begin by hashing
x with our first hash function h1; if location h1(x) is empty, we insert
x there. If h1(x) contains some key y, however, instead of automatically
bouncing to the next location in the probe sequence for x (which would
be location h1(x)+h2(x) as used in double hashing), we first compare keys
x and y; if x is the larger key, we bounce x, compute h2(x), and probe
location h1(x)+h2(x); on the other hand, if x is the smaller key,
we dislodge y, i.e., we insert x in location h1(x) and send y bouncing
along its probe sequence, i.e, we next examine location h1(x)+h2(y).
At every collision, we repeat the procedure, sending the larger key bouncing.
Let the size of the table be n, the number of items in the table be k, and the loading factor be alpha=k/n.
Problem 2. Solve the Dutch national flag problem.
You are given an array filled with elements that can take one of only 3 values:
red, white, and blue; the goal is to return the array with all red elements
on the left, all blue elements on the right, and all white elements in the
middle. Your algorithm must run in linear time,
can only exchange elements, and has just one additional variable to hold
one of the elements -- in particular, it cannot use counters to count
the number of elements of each color.
Solutions are available.
| Back to CS 361 home page |