Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.lang.Math;
- public class Hash {
- public static long djb2(String str, int hashtableSize) {
- long hash = 5381;
- for (int i = 0; i < str.length(); i++) {
- hash = ((hash << 5) + hash) + str.charAt(i);
- }
- return Math.abs(hash) % hashtableSize;
- }
- public static long sdbm(String str, int hashtableSize) {
- long hash = 0;
- for (int i = 0; i < str.length(); i++) {
- hash = str.charAt(i) + (hash << 6) + (hash << 16) - hash;
- }
- return Math.abs(hash) % (hashtableSize - 1) + 1;
- }
- public static long h(String str, int hashtableSize, int i) {
- return (djb2(str, hashtableSize) + i * sdbm(str, hashtableSize)) % hashtableSize;
- }
- public static void main(String[] args) {
- String[][] h_size5 = new String[5][5];
- // First index represents attempt number, second attempt represents index on the array.
- h_size5[0][0] = "KapilSingh";
- h_size5[0][4] = "ChunkySingh";
- h_size5[0][0] = "ShyamSingh";
- h_size5[1][4] = "ShyamSingh";
- h_size5[2][3] = "ShyamSingh";
- h_size5[0][3] = "ShyaamSingh";
- System.out.println(djb2("JanaardanBhartiya", 5));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement