Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class LHash{
- static void printA(int a[][], int count[], int rFilled){
- int j, i;
- for(i=0; i <= rFilled; i++){
- System.out.print("Bucket "+i+ ": ");
- for(j=0; j < count[i]; j++)
- System.out.print("\t"+a[i][j]+" ");
- System.out.println();
- }
- System.out.println();
- }
- public static void main(String args[]){
- int[][] a = new int[6][12];
- int[] count = new int[6];
- int[] data = {1, 7, 3, 8, 12, 4, 11, 2, 10, 13, 14, 9};
- int s = 0, bSize = 2, n = 3;
- int i, j, temp, c, b, rFilled=0;
- for(i = 0; i < 12; i++){
- if(s>n){
- s=0;
- n*=2;
- System.out.println("The hash function is now h(k)=k mod "+n);
- }
- if(data[i]%n>=s){
- System.out.println("Inserting "+data[i]+" ("+data[i]+" mod "+n+" = "+data[i]%n+")");
- a[data[i]%n][count[data[i]%n]++]=data[i];
- if(data[i]%n > rFilled) rFilled=data[i]%n;
- }
- else{
- System.out.println("Inserting "+data[i]+" ("+data[i]+" mod "+2*n+" = "+data[i]%(2*n)+")");
- a[data[i]%(2*n)][count[data[i]%(2*n)]++]=data[i];
- if(data[i]%(2*n) > rFilled) rFilled=data[i]%(2*n);
- }
- printA(a, count, rFilled);
- if(count[data[i]%n] > bSize){
- System.out.println("Overflow! at Bucket "+data[i]%n);
- int counter=count[s];
- count[s] = 0;
- for(j = 0; j < counter; j++){
- temp = a[s][j];
- a[s][j]=0;
- a[temp%(2*n)][count[temp%(2*n)]++] = temp;
- if(temp%(2*n) > rFilled) rFilled=temp%(2*n);
- }
- s++;
- printA(a, count, rFilled);
- }
- }
- System.out.println("All insertions complete.");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement