You are on page 1of 4

EXTENDIBLE HASHING ALGORITHM

#define MAX_BUCKETS 512


#define SIZE 10

int makesub(char *in, int depth) {


char key[21];
int i;
int hash, sub = 0;
int remainder;
strncpy(key, in, 20);
key[20] = ' ';
hash = hasher(key);
for(i=0; i < depth; i++) {
sub = sub << 1;
remainder = hash % 2;
sub = sub + remainder;
hash = hash >> 1;
}
return sub;
}

struct rec_tag {
char key_field[21];
};

typedef struct buck_tag {


long number_of_records;
int bucket_bits;
struct rec_tag record[SIZE];
} BUCKET;

struct head_tag {
int bits_used_in_index;
long buckets_used;
};

int doublidx(int bits, long *addresses) {


int i;
int size;
long old_table[MAX_BUCKETS];
size = pow(2, bits);
if((size * 2) == MAX_BUCKETS) puts("WARNING: You are about to run out of index
space!");
memcpy(old_table, addresses, (size * sizeof(long)));
for(i=0; i < size; i++) {
addresses[i*2] = old_table[i];
addresses[i*2 +1] = old_table[i];
}

return (bits + 1);


}

int moverecs(BUCKET * old, BUCKET * neu) {


char spaces[21] = " ";
int i, j, sub, n_sub;
int odd, flag, space, check;
old->bucket_bits += 1;
n_sub = 0;
for(i=0; i < SIZE; i++) {
sub = makesub(old->record[i].key_field, old->bucket_bits);
odd = sub%2;
if(odd) {
memcpy(&neu->record[n_sub], &old->record[i], sizeof(struct rec_tag));
strcpy(old->record[i].key_field, spaces);
n_sub++;
}
}
neu->number_of_records = n_sub;
neu->bucket_bits = old->bucket_bits;

for(i=0, flag=0; i < SIZE && flag==0; i++) {


space = strcmp(old->record[i].key_field, spaces);
if(space == 0) {
for(j=i+1, check=1; j < SIZE && check != 0; j++)
check = strcmp(old->record[j].key_field, spaces);
if(j < SIZE) {
memcpy(&old->record[i], &old->record[j-1], sizeof(struct rec_tag));
strcpy(old->record[j-1].key_field, spaces);
}
else flag = 1;
}
}
old->number_of_records -= n_sub;
return 0;
}

SEPARATE CHAINING ALGORITHM


import java.util.Scanner;

class Link {
public int data;
public Link next;
public Link(int d) {
data = d;
}
}
class SortedList {
private Link first;
public SortedList() {
first = null;
}
public void insert(Link theLink){
int key = theLink.data;
Link previous = null;
Link current = first;
while (current != null && key > current.data) {
previous = current;
current = current.next;
}
if (previous == null)
first = theLink;
else
previous.next = theLink;
theLink.next = current;
}
public void displayList() {
System.out.print("List: ");
Link current = first;
while (current != null){
System.out.print(current.data + " ");
current = current.next;
}
System.out.println("");
}
}
public class SeperateChaining {
private SortedList[] hashArray;
private int arraySize;
public SeperateChaining(int size) {
arraySize = size;
hashArray = new SortedList[arraySize];
for (int i = 0; i < arraySize; i++)
hashArray[i] = new SortedList();
}
public void displayTable() {
for (int j = 0; j < arraySize; j++) {
System.out.print(j + ". ");
hashArray[j].displayList();
}
}
public void insert(Link theLink) {
int key=theLink.data;
int hashVal =key % arraySize;
hashArray[hashVal].insert(theLink);
}
public static void main(String[] args) {
Link dataItem;
int size, initSize,ele;
size = 10;
initSize = 10;
SeperateChaining hashTable = new SeperateChaining(size);
Scanner in = new Scanner(System.in);
for (int i = 0; i < initSize; i++){
System.out.println("Enter the elements");
ele = in.nextInt();
dataItem = new Link(ele);
hashTable.insert(dataItem);
}
hashTable.displayTable();
}
}