Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package tabliceRozproszone;
- import java.util.Arrays;
- public class LinearProbingFirstFunction {
- private int tableSize = 20000;
- private String[] words = new String[tableSize];
- public LinearProbingFirstFunction() {
- tableSize = 20000;
- words = new String[20000];
- }
- public String getWords() {
- return Arrays.toString(words);
- }
- public int calculateASCIIcodeSum(String word) {
- int sum = 0;
- for (int i = 0; i < word.length(); i++) {
- sum += (int)word.charAt(i);
- }
- return sum;
- }
- private int firstHashFunctionForLinearProbing (String word, int i) {
- return (calculateASCIIcodeSum(word) + i)%tableSize;
- }
- public void linearProbingForFirstFunction (String word) {
- int i = 0;
- int hash = 0;
- while (i<tableSize) {
- hash = firstHashFunctionForLinearProbing(word, i);
- if (words[hash].equals("null")) {
- words[hash] = word;
- break;
- }
- else
- i+=1;
- }
- if (i == tableSize)
- System.out.println("No space available");
- }
- public int findKey_linearProbing_firstFunction (String word) {
- int i = 0 ;
- int j;
- do {
- j = firstHashFunctionForLinearProbing(word, i);
- if (words[j].equals(word)) {
- return j;
- }
- else {
- i = i+1;
- }
- } while (i < tableSize && !words[j].equals("null"));
- System.out.println("Record not found");
- return -1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement