File size: 864 Bytes
c574d3a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
public class Quad{
static int max= 10;
static int hash(int key){
return key%max;
}
static int prob(int H[], int key){
int i=0;
int index = hash(key);
while(H[(index+(i*i))%max]!=0){
i++;
}
return (index+(i*i))%max;
}
static void insert(int H[], int key){
int index = hash(key);
while(H[index]!=0){
index = prob(H,key);
}
H[index] = key;
}
static int search(int H[], int key){
int index = hash(key);
int i=0;
while(H[(index+(i*i))%max]!=key){
i++;
}
return (index+(i*i))%max;
}
public static void main(String[] args){
int H[] = new int[max];
for(int i=0;i<max; ++i){
H[i] = 0;
}
insert(H,3);
insert(H,5);
insert(H,6);
insert(H,12);
insert(H,13);
int x = 3;
System.out.println(search(H,x));
}
}
|