Skip to content

Commit 0f3620c

Browse files
committed
Including hashing functions
1 parent da06d5e commit 0f3620c

File tree

5 files changed

+91
-0
lines changed

5 files changed

+91
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.packt.datastructuresandalg.lesson3.hashtable;
2+
3+
public interface HashProvider<K> {
4+
int hashKey(K key, int tableSize);
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.packt.datastructuresandalg.lesson3.hashtable;
2+
3+
import java.util.Optional;
4+
5+
public interface HashTable<K,V> {
6+
void put(K key,V value);
7+
8+
Optional<V> get(K key);
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.packt.datastructuresandalg.lesson3.hashtable;
2+
3+
public class MultiplicationHashing implements HashProvider<Integer> {
4+
private double k;
5+
6+
public MultiplicationHashing(double k) {
7+
this.k = k;
8+
}
9+
10+
public int hashKey(Integer key, int tableSize) {
11+
return (int) (tableSize * (k * key % 1));
12+
}
13+
14+
public static void main(String[] args) {
15+
MultiplicationHashing multiplicationHashing = new MultiplicationHashing((Math.sqrt(5) - 1) / 2);
16+
System.out.println(multiplicationHashing.hashKey(337481990, 1000));
17+
System.out.println(multiplicationHashing.hashKey(116241990, 1000));
18+
System.out.println(multiplicationHashing.hashKey(983611990, 1000));
19+
System.out.println(multiplicationHashing.hashKey(201031990, 1000));
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.packt.datastructuresandalg.lesson3.hashtable;
2+
3+
public class RemainderHashing implements HashProvider<Integer>{
4+
5+
public int hashKey(Integer key, int tableSize) {
6+
return key % tableSize;
7+
}
8+
9+
public static void main(String[] args) {
10+
RemainderHashing remainderHashing = new RemainderHashing();
11+
System.out.println(remainderHashing.hashKey(337481990, 1000));
12+
System.out.println(remainderHashing.hashKey(116241990, 1000));
13+
System.out.println(remainderHashing.hashKey(983611990, 1000));
14+
System.out.println(remainderHashing.hashKey(201031990, 1000));
15+
16+
System.out.println(remainderHashing.hashKey(337481990, 1447));
17+
System.out.println(remainderHashing.hashKey(116241990, 1447));
18+
System.out.println(remainderHashing.hashKey(983611990, 1447));
19+
System.out.println(remainderHashing.hashKey(201031990, 1447));
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.packt.datastructuresandalg.lesson3.hashtable;
2+
3+
import java.math.BigInteger;
4+
5+
public class UniversalHashing implements HashProvider<Integer> {
6+
7+
private final BigInteger i, j;
8+
private final long p = 47055833459L;
9+
10+
public UniversalHashing() {
11+
j = BigInteger.valueOf((long) (Math.random() * p));
12+
i = BigInteger.valueOf(1 + (long) (Math.random() * (p - 1L)));
13+
}
14+
15+
public int hashKey(Integer key, int tableSize) {
16+
return i.multiply(BigInteger.valueOf(key)).add(j)
17+
.mod(BigInteger.valueOf(p))
18+
.mod(BigInteger.valueOf(tableSize))
19+
.intValue();
20+
}
21+
22+
public static void main(String[] args) {
23+
UniversalHashing universalHashing = new UniversalHashing();
24+
System.out.println(universalHashing.hashKey(337481990, 1000));
25+
System.out.println(universalHashing.hashKey(116241990, 1000));
26+
System.out.println(universalHashing.hashKey(983611990, 1000));
27+
System.out.println(universalHashing.hashKey(201031990, 1000));
28+
29+
universalHashing = new UniversalHashing();
30+
System.out.println(universalHashing.hashKey(337481990, 1000));
31+
System.out.println(universalHashing.hashKey(116241990, 1000));
32+
System.out.println(universalHashing.hashKey(983611990, 1000));
33+
System.out.println(universalHashing.hashKey(201031990, 1000));
34+
}
35+
}

0 commit comments

Comments
 (0)