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
| package TP8;
public class HashLinearProbing {
public static char Keys[] = new char[11];
public static int Values[] = new int[11];
// Fonction de hachage par modulo sur le code ascii d'un caractère en entrée
public static int hash(char key, int M) {
int hash;
int ascii;
ascii = (int) (key); // récupère le code ASCII d'un caractère
hash = ascii % M; // hachage par modulo
return hash;
}
public static void put(char key, int value) {
int M = 9;
int index;
index = hash(key, M);
Keys[value] = key;
Values[value] = index;
}
public static void main(String[] args) {
// Déclaration de ma table d'entrée R (Exemple du cours
// "Hash Table Linear Probing exemple")
char R[] ={'B','O','E','P','C','L','C','X','N','D','M'};
System.out.println("La table de données en entrée est la suivante :");
System.out.println(R);
System.out
.println("***************************************************");
int i;
for (i = 0; i < R.length; i++) {
put(R[i], i);
}
System.out.println(Keys);
for (int j = 0; j < Values.length; j++) {
System.out.print(Values[j]+";");
}
System.out.println();
for (int j = 0; j < Values.length; j++) {
for (int k = 0; k < Values.length; k++) {
if(j== Values [k]){
System.out.print(Keys[k]+"-");
}
}
}
}
} |
Partager