Questions 15-19, Page 1.
CS140 Final Exam: May 10, 1999

These questions concern hashing. Our hash table is simply an array of integers. Keys are positive integers, and the hash table is initialized to consist of all zeros. hash_find is a routine that takes a hash table, the table size, and a key. It returns the index of the table containing the key, or -1 if the key is not in the hash table.

Assume that keys are inserted using linear probing and that keys are never deleted. The hash function is hash(int key, int tsize), which returns a hash value between zero and (tsize-1).

Below are four implementations of hash_find():

(a)

int hash_find(int *table, 
              int tsize, int key)
{
  int h;

  h = hash(key, tsize);
  while (table[h] != key) h++;
  return h;
}
(b)

int hash_find(int *table, 
              int tsize, int key)
{
  int h, i, j;

  i = 0;
  h = hash(key, tsize);
  while(i < tsize) {
    j = table[(h+i)%tsize];
    if (j == key) return (h+i)%tsize;
    if (j == 0) return -1;
    i++;
  }
}
(c)

int hash_find(int *table, 
              int tsize, int key)
{
  int h, j, i;

  i = 0;
  h = hash(key, tsize);
  while(i < tsize) {
    j = (h+i)%tsize;
    if (j == key) return table[j];
    if (j == 0) return table[-1];
    i++;
  }
}
(d)

int hash_find(int *table, 
              int tsize, int key)
{
  int h, j, i;

  i = 0;
  h = hash(key, tsize);
  j = h;
  while(i < tsize) {
    if (j > tsize) j -= tsize;
    if (hash(table[j], tsize) == h) return j;
    if (table[j] == 0) return -1;
    i++;
  }
}