Advertisement
Guest User

Hash

a guest
Nov 14th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.45 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <new>
  4.  
  5. #include "Profiler.h"
  6.  
  7. Profiler profiler("HashTables");
  8. using namespace std;
  9. /*
  10. typedef struct Node
  11. {
  12. int value;
  13. struct Node* next;
  14. };
  15.  
  16. void Push(Node* head, int value)
  17. {
  18. Node* current = head;
  19. while (current->next != NULL)
  20. {
  21. current = current->next;
  22. }
  23. current->next = (Node*)malloc(sizeof(Node));
  24. current->next->value = value;
  25. current->next->next = NULL;
  26. }
  27.  
  28. int Search(Node* Table[], int key, int value)
  29. {
  30. Node* temp = Table[value / key];
  31. int k = 1;
  32. while (temp->value != value)
  33. {
  34. temp = temp->next;
  35. }
  36. return k;
  37. }
  38.  
  39. void Insert(Node* Table[], int key, int value)
  40. {
  41. if (Table[value / key] != NULL)
  42. {
  43. Push(Table[value / key], value);
  44. }
  45. else
  46. {
  47. Table[value / key]->value = value;
  48. Table[value / key]->next = NULL;
  49. }
  50. }
  51. */
  52.  
  53. void InsertArr(int Table[], int key, int value)
  54. {
  55. int temp = value % key;
  56. if (Table[temp] == 0)
  57. {
  58. Table[temp] = value;
  59. }
  60. else
  61. {
  62. int i = 1;
  63. while (Table[(value + i*i) % key] != 0)
  64. {
  65. i++;
  66. }
  67. Table[(value + i * i) % key] = value;
  68. }
  69. }
  70. int SearchArr(int Table[], int key, int value, int* counter , int* max)
  71. {
  72. int i = 0;
  73. if(Table[value % key] != value)
  74. {
  75. i++;
  76. while (Table[(value + i * i) % key] != value && (value + i * i) % key != value % key)
  77. {
  78. i++;
  79. }
  80. }
  81. if (i > *max)
  82. {
  83. *max = i;
  84. }
  85. counter = counter + i;
  86. if ((value + i * i) % key == value % key && i != 0)
  87. return -1;
  88. return (value + i * i) % key;
  89. }
  90.  
  91. int main()
  92. {
  93. int counterGasite = 0;
  94. int counterNegasite = 0;
  95. int max = 0;
  96.  
  97.  
  98. //Demo
  99. {
  100. //int hashTable[10] = { 0 };
  101. //int X[10] = { 1,2,3,4,5,6,7,8,10,15 };
  102. //for (int i = 0; i < 10; i++)
  103. //{
  104. // InsertArr(hashTable, 10, X[i]);
  105. //}
  106. //cout << SearchArr(hashTable, 10, 8, &counterGasite, &max) << '\n';
  107. //cout << SearchArr(hashTable, 10, 15, &counterGasite, &max) << '\n';
  108. //cout << SearchArr(hashTable, 10, 9, &counterGasite, &max) << '\n';
  109. //cout << SearchArr(hashTable, 10, 16, &counterGasite, &max) << '\n';
  110. }
  111.  
  112. int* X = new int[10000];
  113. int* hashTable = new int[10000];
  114.  
  115. // 0.99
  116. {
  117. FillRandomArray(X, 9973, 10, 10000, true);
  118. for (int i = 0; i < 9973; i++)
  119. {
  120. InsertArr(hashTable, 9973, X[i]);
  121. }
  122. for (int i = 1; i <= 1500; i++)
  123. {
  124. SearchArr(hashTable, 10000, X[i], &counterGasite, &max);
  125. }
  126. for(int i = 10000 ; i <= 11500 ; i++)
  127. {
  128. SearchArr(hashTable, 10000, i, &counterNegasite, &max);
  129. }
  130. cout << counterGasite << ' ' << counterNegasite;
  131. cout << '\n' << max;
  132. }
  133.  
  134. //0.95
  135. {
  136. //FillRandomArray(X, 9497, 10, 10000, true);
  137. //for (int i = 0; i < 9497; i++)
  138. //{
  139. // InsertArr(hashTable, 9497, X[i]);
  140. //}
  141. //for (int i = 1; i <= 1500; i++)
  142. //{
  143. // SearchArr(hashTable, 10000, X[i], &counterGasite, &max);
  144. //}
  145. //for (int i = 10000; i <= 11500; i++)
  146. //{
  147. // SearchArr(hashTable, 10000, i, &counterNegasite, &max);
  148. //}
  149. //cout << counterGasite << ' ' << counterNegasite;
  150. //cout << '\n' << max;
  151. }
  152.  
  153. //0.9
  154. {
  155. //FillRandomArray(X, 8999, 10, 10000, true);
  156. //for (int i = 0; i < 8999; i++)
  157. //{
  158. // InsertArr(hashTable, 8999, X[i]);
  159. //}
  160. //for (int i = 1; i <= 1500; i++)
  161. //{
  162. // SearchArr(hashTable, 10000, X[i], &counterGasite, &max);
  163. //}
  164. //for (int i = 10000; i <= 11500; i++)
  165. //{
  166. // SearchArr(hashTable, 10000, i, &counterNegasite, &max);
  167. //}
  168. //cout << counterGasite << ' ' << counterNegasite;
  169. //cout << '\n' << max;
  170. }
  171.  
  172. //0.85
  173. {
  174. //FillRandomArray(X, 8501, 10, 10000, true);
  175. //for (int i = 0; i < 8501; i++)
  176. //{
  177. // InsertArr(hashTable, 8501, X[i]);
  178. //}
  179. //for (int i = 1; i <= 1500; i++)
  180. //{
  181. // SearchArr(hashTable, 10000, X[i], &counterGasite, &max);
  182. //}
  183. //for (int i = 10000; i <= 11500; i++)
  184. //{
  185. // SearchArr(hashTable, 10000, i, &counterNegasite, &max);
  186. //}
  187. //cout << counterGasite << ' ' << counterNegasite;
  188. //cout << '\n' << max;
  189. }
  190.  
  191. //0.8
  192. {
  193. //FillRandomArray(X, 7993, 10, 10000, true);
  194. //for (int i = 0; i < 7993; i++)
  195. //{
  196. // InsertArr(hashTable, 7993, X[i]);
  197. //}
  198. //for (int i = 1; i <= 1500; i++)
  199. //{
  200. // SearchArr(hashTable, 10000, X[i], &counterGasite, &max);
  201. //}
  202. //for (int i = 10000; i <= 11500; i++)
  203. //{
  204. // SearchArr(hashTable, 10000, i, &counterNegasite, &max);
  205. //}
  206. //cout << counterGasite << ' ' << counterNegasite;
  207. //cout << '\n' << max;
  208. }
  209.  
  210. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement