Advertisement
Guest User

Untitled

a guest
Sep 19th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1. import java.lang.Math;
  2.  
  3. public class Hash {
  4.  
  5. public static long djb2(String str, int hashtableSize) {
  6. long hash = 5381;
  7. for (int i = 0; i < str.length(); i++) {
  8. hash = ((hash << 5) + hash) + str.charAt(i);
  9. }
  10. return Math.abs(hash) % hashtableSize;
  11. }
  12.  
  13. public static long sdbm(String str, int hashtableSize) {
  14. long hash = 0;
  15. for (int i = 0; i < str.length(); i++) {
  16. hash = str.charAt(i) + (hash << 6) + (hash << 16) - hash;
  17. }
  18. return Math.abs(hash) % (hashtableSize - 1) + 1;
  19. }
  20.  
  21. public static long h(String str, int hashtableSize, int i) {
  22. return (djb2(str, hashtableSize) + i * sdbm(str, hashtableSize)) % hashtableSize;
  23. }
  24.  
  25. public static void main(String[] args) {
  26. String[][] h_size5 = new String[5][5];
  27.  
  28. // First index represents attempt number, second attempt represents index on the array.
  29. h_size5[0][0] = "KapilSingh";
  30. h_size5[0][4] = "ChunkySingh";
  31. h_size5[0][0] = "ShyamSingh";
  32. h_size5[1][4] = "ShyamSingh";
  33. h_size5[2][3] = "ShyamSingh";
  34. h_size5[0][3] = "ShyaamSingh";
  35.  
  36.  
  37. System.out.println(djb2("JanaardanBhartiya", 5));
  38. }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement