Advertisement
dasza

Untitled

Dec 4th, 2017
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.64 KB | None | 0 0
  1. package tabliceRozproszone;
  2.  
  3.  
  4. import java.util.Arrays;
  5.  
  6. public class LinearProbingFirstFunction {
  7. private int tableSize = 20000;
  8. private String[] words = new String[tableSize];
  9.  
  10. public LinearProbingFirstFunction() {
  11. tableSize = 20000;
  12. words = new String[20000];
  13. }
  14.  
  15. public String getWords() {
  16. return Arrays.toString(words);
  17. }
  18.  
  19. public int calculateASCIIcodeSum(String word) {
  20. int sum = 0;
  21. for (int i = 0; i < word.length(); i++) {
  22. sum += (int)word.charAt(i);
  23. }
  24. return sum;
  25. }
  26.  
  27. private int firstHashFunctionForLinearProbing (String word, int i) {
  28. return (calculateASCIIcodeSum(word) + i)%tableSize;
  29. }
  30.  
  31. public void linearProbingForFirstFunction (String word) {
  32. int i = 0;
  33. int hash = 0;
  34. while (i<tableSize) {
  35. hash = firstHashFunctionForLinearProbing(word, i);
  36. if (words[hash].equals("null")) {
  37. words[hash] = word;
  38. break;
  39. }
  40. else
  41. i+=1;
  42. }
  43. if (i == tableSize)
  44. System.out.println("No space available");
  45. }
  46.  
  47. public int findKey_linearProbing_firstFunction (String word) {
  48. int i = 0 ;
  49. int j;
  50. do {
  51. j = firstHashFunctionForLinearProbing(word, i);
  52. if (words[j].equals(word)) {
  53. return j;
  54. }
  55. else {
  56. i = i+1;
  57. }
  58. } while (i < tableSize && !words[j].equals("null"));
  59. System.out.println("Record not found");
  60. return -1;
  61. }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement