|
| 1 | +//Closed hashing techniques using linear (with and without replacement) and quadratic probing and double hashing techniques |
| 2 | +package closedHashing; |
| 3 | +import java.util.*; |
| 4 | + |
| 5 | +public class ClosedHashing |
| 6 | +{ |
| 7 | + public static void main(String args[]) |
| 8 | + { |
| 9 | + int n = 5; //number of elements to be inserted in hash table |
| 10 | + Hash h = new Hash(); |
| 11 | + h.acceptElements(n); |
| 12 | + } |
| 13 | +} |
| 14 | + |
| 15 | +class Hash |
| 16 | +{ |
| 17 | + int hashtable1[] = new int[10]; //hash table of size 50, technique for probing is linear probing |
| 18 | + int hashtable2[] = new int[10]; //hash table of size 50, technique for probing is linear probing with replacement |
| 19 | + int hashtable3[] = new int[10]; //hash table of size 50, technique for probing is quadratic probing |
| 20 | + int hashtable4[] = new int[10]; //hash table of size 50, technique is double hashing |
| 21 | + |
| 22 | + //good load factor is <=0.5 |
| 23 | + //default constructor |
| 24 | + hash() |
| 25 | + { |
| 26 | + for (int i = 0; i < 10; i++) |
| 27 | + { |
| 28 | + hashtable1[i] = -1; //initialized to indicate no element currently |
| 29 | + hashtable2[i] = -1; |
| 30 | + hashtable3[i] = -1; |
| 31 | + hashtable4[i] = -1; |
| 32 | + } |
| 33 | + } |
| 34 | + Scanner sc=new Scanner(System.in); |
| 35 | + |
| 36 | + //function to accept elements |
| 37 | + void acceptElements(int n) |
| 38 | + { |
| 39 | + for (int i = 0; i < n; i++) |
| 40 | + { |
| 41 | + int element = sc.nextInt(); |
| 42 | + insertLP(element); |
| 43 | + insertLPWR(element); |
| 44 | + insertQP(element); |
| 45 | + insertDH(element); |
| 46 | + } |
| 47 | + |
| 48 | + //display the different hash tables after hashing performed |
| 49 | + System.out.println("\nLinear Probing without replacement:"); |
| 50 | + display(n, hashtable1); |
| 51 | + System.out.println("\nLinear Probing with replacement:"); |
| 52 | + display(n, hashtable2); |
| 53 | + System.out.println("\nQuadratic Probing:"); |
| 54 | + display(n, hashtable3); |
| 55 | + System.out.println("\nDouble Hashing:"); |
| 56 | + display(n, hashtable4); |
| 57 | + } |
| 58 | + |
| 59 | + //pass every element to the hash function to find its hash address |
| 60 | + int hashfunction(int n) |
| 61 | + { |
| 62 | + int hno = n % 10; |
| 63 | + return hno; |
| 64 | + } |
| 65 | + |
| 66 | + //linear probing without replacement |
| 67 | + void insertLP(int element) |
| 68 | + { |
| 69 | + int flag = 0; |
| 70 | + int hashaddress = hashfunction(element); //found hash address for element |
| 71 | + if (hashtable1[hashaddress] == -1) //i.e. position currently empty |
| 72 | + { |
| 73 | + hashtable1[hashaddress] = element; //element inserted at appropriate position |
| 74 | + } |
| 75 | + else |
| 76 | + { |
| 77 | + int lp = (hashaddress + 1) % 10; //linear probing applied to look for empty space |
| 78 | + while(flag != 1) |
| 79 | + { |
| 80 | + //found empty position? |
| 81 | + if (hashtable1[lp] == -1) |
| 82 | + { |
| 83 | + hashtable1[lp] = element; |
| 84 | + flag = 1; |
| 85 | + } |
| 86 | + else |
| 87 | + lp = (lp + 1) % 10; //else, keep looking |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + //linear probing with replacement |
| 93 | + void insertLPWR(int element) |
| 94 | + { |
| 95 | + int flag = 0; |
| 96 | + int temp = element; |
| 97 | + int hashaddress = hashfunction(element); //found hash address for element |
| 98 | + if (hashtable2[hashaddress] == -1) //i.e. position currently empty |
| 99 | + { |
| 100 | + hashtable2[hashaddress] = element; //element inserted at appropriate position |
| 101 | + } |
| 102 | + else |
| 103 | + { |
| 104 | + int lp = (hashaddress + 1) % 10; //linear probing applied to look for empty space for element |
| 105 | + int hold = hashfunction(hashtable2[hashaddress]); //found hash address of key already present at the collided index |
| 106 | + if (hold != hashaddress) //i.e. not its original address |
| 107 | + { |
| 108 | + //then, find new hash address for element originally present |
| 109 | + temp = hashtable2[hashaddress]; |
| 110 | + hashtable2[hashaddress] = element; |
| 111 | + } |
| 112 | + while (flag != 1) |
| 113 | + { |
| 114 | + //found empty position? |
| 115 | + if (hashtable2[lp] == -1) |
| 116 | + { |
| 117 | + hashtable2[lp] = temp; |
| 118 | + flag = 1; |
| 119 | + } |
| 120 | + else |
| 121 | + lp = (lp + 1) % 10; //else, keep looking |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + //quadratic probing without replacement |
| 127 | + void insertQP(int element) |
| 128 | + { |
| 129 | + int flag = 0; |
| 130 | + int hashaddress = hashfunction(element); //found hash address for element |
| 131 | + if (hashtable3[hashaddress] == -1) //i.e. position currently empty |
| 132 | + { |
| 133 | + hashtable3[hashaddress] = element; //element inserted at appropriate position |
| 134 | + } |
| 135 | + else |
| 136 | + { |
| 137 | + int lp = (hashaddress + 1) % 10; //probing applied to look for empty space |
| 138 | + int i = 2; |
| 139 | + while (flag != 1) |
| 140 | + { |
| 141 | + //found empty position? |
| 142 | + if (hashtable3[lp] == -1) |
| 143 | + { |
| 144 | + hashtable3[lp] = element; |
| 145 | + flag = 1; |
| 146 | + } |
| 147 | + else |
| 148 | + { |
| 149 | + lp = (lp + (i ^ 2)) % 10; //else, keep looking quadratically incremented |
| 150 | + i++; |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | + //double hashing |
| 156 | + void insertDH(int element) |
| 157 | + { |
| 158 | + int offset = 7 - (element % 7); //second hash function |
| 159 | + int flag = 0; |
| 160 | + int hashaddress = hashfunction(element); //found hash address for element |
| 161 | + if (hashtable4[hashaddress] == -1) //i.e. position currently empty |
| 162 | + { |
| 163 | + hashtable4[hashaddress] = element; //element inserted at appropriate position |
| 164 | + } |
| 165 | + else |
| 166 | + { |
| 167 | + while (flag != 1) |
| 168 | + { |
| 169 | + hashaddress = (hashaddress + offset) % 10; //hash address changed |
| 170 | + if (hashtable4[hashaddress] == -1) //i.e. position currently empty |
| 171 | + { |
| 172 | + hashtable4[hashaddress] = element; //element inserted at appropriate position |
| 173 | + flag = 1; |
| 174 | + } |
| 175 | + } |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + //display elements in hashtable |
| 180 | + void display(int n, int hashtable[]) |
| 181 | + { |
| 182 | + System.out.println("\nIndex\tElement"); |
| 183 | + for (int i = 0; i< 10; i++) |
| 184 | + { |
| 185 | + if (hashtable[i] != -1) |
| 186 | + { |
| 187 | + System.out.println(i + "\t" + hashtable[i]); |
| 188 | + } |
| 189 | + } |
| 190 | + } |
| 191 | +} |
| 192 | +/* |
| 193 | +Input: |
| 194 | +44 |
| 195 | +3 |
| 196 | +34 |
| 197 | +77 |
| 198 | +23 |
| 199 | +
|
| 200 | +Output: |
| 201 | +Linear Probing without replacement: |
| 202 | +
|
| 203 | +Index Element |
| 204 | +3 3 |
| 205 | +4 44 |
| 206 | +5 34 |
| 207 | +6 23 |
| 208 | +7 77 |
| 209 | +
|
| 210 | +Linear Probing with replacement: |
| 211 | +
|
| 212 | +Index Element |
| 213 | +3 3 |
| 214 | +4 44 |
| 215 | +5 34 |
| 216 | +6 23 |
| 217 | +7 77 |
| 218 | +
|
| 219 | +Quadratic Probing: |
| 220 | +
|
| 221 | +Index Element |
| 222 | +1 23 |
| 223 | +3 3 |
| 224 | +4 44 |
| 225 | +5 34 |
| 226 | +7 77 |
| 227 | +
|
| 228 | +Double Hashing: |
| 229 | +
|
| 230 | +Index Element |
| 231 | +3 3 |
| 232 | +4 44 |
| 233 | +5 34 |
| 234 | +7 77 |
| 235 | +8 23 |
| 236 | +*/ |
0 commit comments