This repository has been archived by the owner on Apr 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HashTable.java
268 lines (135 loc) · 5.18 KB
/
HashTable.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
import java.util.ArrayList;
import java.util.Arrays;
public class HashTable {
// Offers fast searching and insertion, but they are hard to order and resizing poses a problem, as they are based on arrays
// Hash function decides the index for the element to go in the Hash table, hence faster searching, i.e: calculation not searching helps you find the element in the HasTable
// Often primes are used for array size in HashTable, as this minimize the collisions
String[] array;
int size;
int itemsInArray = 0;
public HashTable(int size) {
this.size = size;
array = new String[size];
Arrays.fill(array, " ");
}
public boolean isPrime(int num) {
if(num==1)
return false;
if(num==2 || num==3 )
return true;
for(int i=3;i<num/2;i+=2)
if(num%i==0)
return false;
return true;
}
public int getNextPrime(int minNumToCheck) {
for(int i=minNumToCheck; true;i++) {
if(isPrime(i))
return i;
}
}
public void increaseArraySize(int minArraySize) {
int newArraySize = getNextPrime(minArraySize);
moveOldArray(newArraySize);
}
public void moveOldArray(int newArraySize) {
String[] cleanArray = removeEmptySpaces(array);
array = new String[newArraySize];
size = newArraySize;
Arrays.fill(array, " ");
HashFunction2(cleanArray,array);
}
public String[] removeEmptySpaces(String[] arrayToClean) {
ArrayList<String> stringlist = new ArrayList<String>();
for(String myString : arrayToClean) {
if(!myString.equals(" "))
stringlist.add(myString);
}
return stringlist.toArray(new String[stringlist.size()]);
}
public void HashFunction1(String[] stringsForArray, String[] theArray) {
for(int i=0;i<stringsForArray.length;i++) {
String newElement = stringsForArray[i];
theArray[Integer.parseInt(newElement)] = newElement; // this marks the same value element to same index
}
}
public void HashFunction2(String[] stringsForArray, String[] theArray) {
for(int i=0;i<stringsForArray.length;i++) {
String newElement = stringsForArray[i];
int arrIndex = Integer.parseInt(newElement)%29; // this marks the same value element to same index
System.out.println("Modulus Index "+arrIndex+" for value "+newElement);
while(!theArray[arrIndex].equals(" ")) {
++arrIndex;
System.out.println("Collision occurred, trying "+arrIndex+" instead");
arrIndex%=size;
}
theArray[arrIndex]=newElement;
}
}
public void doubleHashFunction(String[] stringsForArray, String[] theArray) {
// This is made to avoid clustering which is bad for finding the element
for(int i=0;i<stringsForArray.length;i++) {
String newElement = stringsForArray[i];
int arrIndex = Integer.parseInt(newElement)%size; // this marks the same value element to same index
int stepDistance = 7 - (Integer.parseInt(newElement)%7); // 7 because, prime and why not?
System.out.println("Modulus Index "+arrIndex+" for value "+newElement);
while(!theArray[arrIndex].equals(" ")) {
arrIndex+=stepDistance;
System.out.println("Collision occurred, trying "+arrIndex+" instead");
arrIndex%=size;
}
theArray[arrIndex]=newElement;
}
}
public String findKey(String key) {
int arrayIndexHash = Integer.parseInt(key)%29; // Same as in has function
while(!array[arrayIndexHash].equals(" ")) {
if(array[arrayIndexHash]==key) {
System.out.println(key+" found at the index "+arrayIndexHash);
return array[arrayIndexHash];
}
++arrayIndexHash;
arrayIndexHash%=size;
}
return null;
}
public String findKeyDoubleHased(String key) {
int arrayIndexHash = Integer.parseInt(key)%size; // Same as in hash function
int stepDistance = 7 - (Integer.parseInt(key)%7);
while(!array[arrayIndexHash].equals(" ")) {
if(array[arrayIndexHash]==key) {
System.out.println(key+" found at the index "+arrayIndexHash);
return array[arrayIndexHash];
}
arrayIndexHash+=stepDistance;
arrayIndexHash%=size;
}
return null;
}
public void displayHashTable() {
System.out.println();
for(int i=0;i<10;i++) {
System.out.println(i+" | "+array[i]);
System.out.println("____|________");
}
for(int i=10;i<size;i++) {
System.out.println(i+" | "+array[i]);
System.out.println("____|________");
}
System.out.println();
}
public static void main(String[] args) {
// hashing strings means turning strings to a particular number than can help find string easily
HashTable hashtable = new HashTable(30);
String[] elementsToAdd = {"10", "51", "17", "21", "26", "39","24","80"};
hashtable.HashFunction2(elementsToAdd, hashtable.array);
hashtable.displayHashTable();
System.out.println();
hashtable.findKey("21");
hashtable.increaseArraySize(40);
hashtable.displayHashTable();
hashtable.doubleHashFunction(elementsToAdd, hashtable.array);
hashtable.displayHashTable();
hashtable.findKeyDoubleHased("17");
}
}