Advertisement
Guest User

Untitled

a guest
Nov 11th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.28 KB | None | 0 0
  1. /*
  2.  
  3. -Hash tables are used to efficiently store data.
  4. -hashing is a process that uses a hash function to get the key for the hash table and transform it into an index that will point to the place
  5. where the information is stored.
  6. -here we used 2 functions, hashInsert and hashSearch and a procedure that creates an empty hash table
  7. -the first one is for inserting data into the hash table, and the second one is for searching if a specific element is in the table or not
  8.  
  9. */
  10. #include <iostream>
  11. #include "Profiler.h"
  12. #define totPos 9973
  13.  
  14. Profiler profiler("hash");
  15.  
  16. double fillingFactor[] = { 0.8, 0.85, 0.9, 0.95, 0.99 };
  17. int maxEffortFound[5], maxEffortNotFound[5];
  18. float avgEffortFound[5], avgEffortNotFound[5];
  19. int search_effort = 0;
  20.  
  21. using namespace std;
  22.  
  23. typedef struct
  24. {
  25. int id;
  26. char name[30];
  27. } Entry;
  28.  
  29. void createEmptyHash(Entry t[], int size)
  30. {
  31. for (int i = 0; i < size; i++)
  32. t[i].id = NULL;
  33. }
  34.  
  35. int hash_function(int val, int i, int size)
  36. {
  37. int h = val + i * i;
  38. int f = h % size;
  39. return f;
  40. }
  41. int hashInsert(Entry t[], int key, int size)
  42. {
  43. int i = 0;
  44. while (i < size)
  45. {
  46. int j = hash_function(key, i, size);
  47. if (t[j].id == NULL)
  48. {
  49. t[j].id = key;
  50. return j;
  51. }
  52. else
  53. i++;
  54. }
  55. cout << "hash table overflow" << "\n";
  56. return 0;
  57. }
  58. int hashSearch(Entry t[], int key, int size)
  59. {
  60. int i = 0, j=0;
  61. do
  62. {
  63. j = hash_function(key, i, size);
  64. search_effort++;
  65. if (t[j].id == key)
  66. {
  67. return j;
  68. }
  69. i++;
  70. }while (i < size && t[j].id != NULL);
  71. return NULL;
  72. }
  73.  
  74. void hashInsertDemo()
  75. {
  76. cout << "Demo Insertion" << "\n";
  77. int a[6] = { 2,8,5,7,9,10 };
  78. Entry t[11];
  79. createEmptyHash(t, 11);
  80. cout << "We add in the table: ";
  81. for (int i = 0; i < 6; i++)
  82. {
  83. cout << a[i] << " ";
  84. }
  85. cout << "\n";
  86. for (int i = 0; i < 6; i++)
  87. {
  88. hashInsert(t, a[i], 11);
  89. }
  90. cout << "After performig insertion, we have: ";
  91. cout << "\n";
  92. for (int i = 0; i < 11; i++)
  93. cout << "On the position " << i << ": " << t[i].id << "\n";
  94. cout << "\n";
  95. }
  96. void hashSearchDemo()
  97. {
  98. cout << "Demo Searching" << "\n";
  99. int a[6] = { 2,8,5,7,9,10 };
  100. int test[4] = { 2,1,6,10 };
  101. Entry t[11];
  102. createEmptyHash(t, 11);
  103. for (int i = 0; i < 6; i++)
  104. {
  105. hashInsert(t, a[i], 10);
  106. }
  107. cout << "We search for: ";
  108. for (int i = 0; i < 4; i++)
  109. {
  110. cout << test[i] << " ";
  111. }
  112. cout << "\n";
  113. cout << "The hash table is: ";
  114. for (int i = 0; i < 11; i++)
  115. {
  116. cout << t[i].id << " ";
  117. }
  118. cout << "\n";
  119. for (int i = 0; i < 6; i++)
  120. {
  121. int ok;
  122. ok = hashSearch(t, test[i], 11);
  123. if (ok == NULL) cout << "The el " << test[i] << " is not in the table\n";
  124. else if (ok!=NULL) cout << "The el " << test[i] << " is in the table\n";
  125. }
  126. }
  127. void demo95FillFactor()
  128. {
  129. Entry t[totPos];
  130. createEmptyHash(t, totPos);
  131. int a[totPos], notFound[1500];
  132. FillRandomArray(notFound, 1500, 11000, 15000, true, 0);
  133. FillRandomArray(a, int(0.95 * totPos), 0, 10500, true, 0);
  134. for (int i = 0; i < int(0.95 * totPos); i++)
  135. hashInsert(t, a[i], totPos);
  136. for (int i = 0; i < 1500; i++)
  137. {
  138. search_effort = 0;
  139. int x = hashSearch(t, t[i].id, totPos);
  140. if (x != NULL)
  141. {
  142. if (search_effort > maxEffortFound[3])
  143. maxEffortFound[3] = search_effort;
  144. avgEffortFound[3] += search_effort;
  145. }
  146. search_effort = 0;
  147. x = hashSearch(t, notFound[i], totPos);
  148. if (search_effort > maxEffortNotFound[3])
  149. maxEffortNotFound[3] = search_effort;
  150. avgEffortNotFound[3] += search_effort;
  151. }
  152. avgEffortFound[3] = avgEffortFound[3] / 1500;
  153. avgEffortNotFound[3] = avgEffortNotFound[3] / 1500;
  154. cout << "\nFor the fill factor .95, with non-uniform selection of elements, we have: \n";
  155. cout << "Avg effort found: " << avgEffortFound[3];
  156. cout << "\nMax effort found: " << maxEffortFound[3];
  157. cout << "\nAvg effort not found: " << avgEffortNotFound[3];
  158. cout << "\nMax Effort not found: " << maxEffortNotFound[3] << "\n\n\n";
  159. }
  160. /*void demo95FillFactor()
  161. {
  162. Entry table[totPos];
  163. createEmptyHash(table, totPos);
  164. int insVector[totPos], nfVector[1505];
  165. FillRandomArray(nfVector, 1500, 11000, 15000, true, 0);
  166. FillRandomArray(insVector, int(0.95 * totPos), 0, 10500, true, 0);
  167. /* We choose .95 as the fill factor for this case
  168. for (int i = 0; i < int(0.95 * totPos); i++)
  169. hashInsert(table, insVector[i], totPos);
  170. for (int i = 0; i < 1500; i++) {
  171. search_effort = 0;
  172. int aux;
  173. aux = hashSearch(table, nfVector[i], totPos);
  174. if (aux != NULL) cout << "x";
  175. if (search_effort > maxEffortNotFound[3])
  176. maxEffortNotFound[3] = search_effort;
  177. avgEffortNotFound[3] += search_effort;
  178.  
  179. search_effort = 0;
  180. aux = hashSearch(table, table[i].id, totPos);
  181. if (aux != NULL) {
  182. if (search_effort > maxEffortFound[3])
  183. maxEffortFound[3] = search_effort;
  184. avgEffortFound[3] += search_effort;
  185. }
  186. }
  187. avgEffortFound[3] = avgEffortFound[3] / 1500;
  188. avgEffortNotFound[3] = avgEffortNotFound[3] / 1500;
  189. cout << "\nFor the fill factor .95, with non-uniform selection of elements, we have: \n";
  190. cout << "Avg effort found: " << avgEffortFound[3];
  191. cout << "\nMax effort found: " << maxEffortFound[3];
  192. cout << "\nAvg effort not found: " << avgEffortNotFound[3];
  193. cout << "\nMax Effort not found: " << maxEffortNotFound[3] << "\n\n\n";
  194. }*/
  195. int main()
  196. {
  197. hashInsertDemo();
  198. hashSearchDemo();
  199. demo95FillFactor();
  200. return 0;
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement