Skip to content

Instantly share code, notes, and snippets.

@maurobaraldi
Last active October 11, 2018 00:15
Show Gist options
  • Save maurobaraldi/b1182d5e60a18ac80bc2b1216aa5cf6e to your computer and use it in GitHub Desktop.
Save maurobaraldi/b1182d5e60a18ac80bc2b1216aa5cf6e to your computer and use it in GitHub Desktop.
Hashing Function Java
package hashtable;
public class HashTable {
public static int hashFunction(int value) {
return value % 10;
}
public static boolean hashInsert(int hashTable[], int value) {
int pos = hashFunction(value);
if (hashTable[pos] == 0) {
hashTable[pos] = value;
} else {
while(hashTable[pos]!=0){
pos++;
}
hashTable[pos] = value;
}
return true;
}
public static void main(String[] args) {
int hashTable[] = new int[20];
hashInsert(hashTable,222);
hashInsert(hashTable,322);
hashInsert(hashTable,719);
for(int i=0;i<10;i++){
System.out.println(hashTable[i]);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment