Advertisement
Guest User

Untitled

a guest
Nov 11th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include "Profiler.h"
  6. using namespace std;
  7. Profiler profiler("HashTables");
  8.  
  9. typedef struct {
  10. int id;
  11. char name[30];
  12. }Entry;
  13.  
  14.  
  15. int effort, flag;
  16.  
  17. /*
  18. int Linear(int key, int i, int size)
  19. {
  20. return key % size;
  21. }
  22. */
  23. int HashFunction(int key, int i, int size)
  24. {
  25. return (key + i * i) % size;
  26. }
  27.  
  28.  
  29. void initializeTable(Entry* hash, int size)
  30. {
  31. for (int i = 0; i < size; i++)
  32. {
  33. hash[i].id = NULL;
  34. strcpy_s (hash[i].name, "empty");
  35. }
  36. }
  37.  
  38. int HashInsert(Entry T[], int key, int size)
  39. {
  40. int i = 0;
  41. do
  42. {
  43. int j = HashFunction(key, i, size);
  44. if (T[j].id == NULL)
  45. {
  46. T[j].id = key;
  47. strcpy_s(T[j].name, "Name");
  48. return j;
  49. }
  50. else i++;
  51. } while (i != size);
  52.  
  53. return 0;
  54. }
  55.  
  56. int HashSearch(Entry T[], int key, int size)
  57. {
  58. int i = 0, j;
  59. do
  60. {
  61. j = HashFunction(key, i, size);
  62. effort++;
  63. if (T[j].id == key)
  64. {
  65. if (flag == 0) cout << "\nAt position " << j << "we found the element " << key;
  66. return j;
  67. }
  68. else i++;
  69. } while (i!=size || T[j].id != NULL);
  70. if (flag == 0) cout << "\ERR: element " << key << " not found\n";
  71. return 0;
  72. }
  73.  
  74. int* generateAvgData(int n)
  75. {
  76. int* a = (int*)malloc(n * sizeof(int));
  77. FillRandomArray(a, n, 1, 1000, false, 0);
  78. return a;
  79. }
  80.  
  81. int FillingFactor(int factor, int size)
  82. {
  83. return (size * factor / 100);
  84. }
  85.  
  86. void HashPrint(Entry hash[], int size)
  87. {
  88. for (int i = 0; i < size; i++)
  89. cout << hash[i].id << " -> " << hash[i].name;
  90. }
  91.  
  92. void DemoInsert ()
  93. {
  94. cout << "\nDemoInsert\n";
  95. int *a = generateAvgData(7);
  96. cout << "\nThe array inserted in the hash table: \n";
  97. for (int i = 0; i < 7; i++)
  98. cout << a[i] << " ";
  99. cout << endl;
  100. Entry insert[10];
  101. initializeTable(insert, 10);
  102.  
  103. for (int i = 0; i < 10; i++)
  104. HashInsert(insert, a[i], 10);
  105. for (int i = 0; i < 10; i++)
  106. cout << "insert[" << i << "]= " << insert[i].id << endl;
  107. }
  108.  
  109. void DemoFillingFactorInsert()
  110. {
  111. cout << "\nDemo Filling Factor Insert\n";
  112. int factor = FillingFactor(50, 10);
  113. cout << "the filling factor is 50% and we'll insert "<<factor<<" elements in a table with 10 positions"<<endl;
  114. int* a = generateAvgData(factor);
  115. cout << "\nThe array inserted in the hash table: \n";
  116. for (int i = 0; i < factor; i++)
  117. cout << a[i] << " ";
  118. cout << endl;
  119. Entry insert[10];
  120. initializeTable(insert, 10);
  121. for (int i = 0; i < 10; i++)
  122. cout << insert[i].id << " ";
  123. cout << endl;
  124.  
  125. for (int i = 0; i < 10; i++)
  126. HashInsert(insert, a[i], 10);
  127. for (int i = 0; i < 10; i++)
  128. cout << "insert[" << i << "]= " << insert[i].id << endl;
  129.  
  130. }
  131. int main()
  132. {
  133. DemoInsert();
  134. DemoFillingFactorInsert();
  135.  
  136.  
  137. return 0;
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement