Advertisement
lashrone1

hash

Dec 1st, 2019
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.46 KB | None | 0 0
  1.  
  2. import com.sun.net.httpserver.Authenticator;
  3.  
  4. import java.util.Arrays;
  5. import java.util.Scanner;
  6. class LinkedHashEntry {
  7.  
  8. private int key;
  9. private int value;
  10. private LinkedHashEntry next;
  11.  
  12. LinkedHashEntry(int key, int value) {
  13. this.key = key;
  14. this.value = value;
  15. this.next = null;
  16. }
  17.  
  18. public int getValue() {
  19. return value;
  20. }
  21. public void setValue(int value) {
  22. this.value = value;
  23. }
  24. public int getKey() {
  25. return key;
  26. }
  27. public LinkedHashEntry getNext() {
  28. return next;
  29. }
  30. public void setNext(LinkedHashEntry next) {
  31. this.next = next;
  32. }
  33. }
  34.  
  35.  
  36. public class Main {
  37.  
  38. static Scanner sc = new Scanner(System.in);
  39.  
  40. static int[] arr;
  41. static int[] hash;
  42. static int[] arrforTable;
  43. static int arraySize = 100;
  44.  
  45.  
  46. static LinkedHashEntry[] table;
  47.  
  48.  
  49. static void hash_div_10() {
  50. hash = new int[arr.length];
  51. for(int i = 0;i < arr.length; i++){
  52. hash[i] = arr[i]%10;
  53. System.out.println(hash[i]);
  54. }
  55.  
  56.  
  57.  
  58. for(int i = 0;i < arr.length; i++){
  59. put(i,hash[i]);
  60. System.out.println("Index " + i%10+ " Value " + get(i));
  61. }
  62. System.out.println();
  63. System.out.println("Number of collisions: " + coll(hash));
  64. System.out.println();
  65.  
  66. }
  67.  
  68. static void hash_div_100() {
  69. hash = new int[arr.length];
  70. for(int i = 0;i < arr.length; i++){
  71. hash[i] = arr[i]%100;
  72. }
  73. System.out.println("Number of collisions: " + coll(hash));
  74. }
  75.  
  76. static void hash_div_101() {
  77. hash = new int[arr.length];
  78. for(int i = 0;i < arr.length; i++){
  79. hash[i] = arr[i]%101;
  80. }
  81. System.out.println("Number of collisions: " + coll(hash));
  82.  
  83. }
  84.  
  85.  
  86. static void hash_mult_10() {
  87.  
  88. hash = new int[arr.length];
  89. double A=0.618033;
  90. for(int i = 0;i < arr.length; i++) {
  91. double tmp = (arr[i] * A) * arr.length;
  92. hash[i] = (int) Math.abs(tmp)% 10;
  93. }
  94. for(int n=0;n<arrforTable.length; n++){
  95.  
  96. int element = hash[n];
  97. int arrIndex = element % 10;
  98. //System.out.println(arrIndex);
  99. System.out.println("Index= " + arrIndex + " for value " + element);
  100.  
  101. while (arrforTable[arrIndex] != -1) {
  102. ++arrIndex;
  103. System.out.println("Collision Try " + arrIndex + " Instead");
  104. // If we get to the end of the array go back to index 0
  105. arrIndex %= arraySize;
  106. }
  107. arrforTable[arrIndex] = element;
  108. System.out.println("Success!");
  109. }
  110. System.out.println("Number of collisions: " + coll(hash));
  111. }
  112.  
  113. static void hash_mult_100() {
  114. hash = new int[arr.length];
  115. double A=0.618033;
  116. for(int i = 0;i < arr.length; i++){
  117. double tmp = (arr[i]*A)*arr.length;
  118. //System.out.println(tmp%100);
  119. hash[i] = (int)Math.abs(tmp%100);
  120. }
  121. System.out.println("Number of collisions: " + coll(hash));
  122. }
  123.  
  124. static int coll(int[] arr) {
  125. int counter = 0;
  126. for (int i = 0; i < arr.length; i++ ){
  127. for (int j = i+1; j < arr.length; j++){
  128. if(arr[i] == arr[j])
  129. counter++;
  130. }
  131. }
  132. return counter;
  133. }
  134.  
  135. static void print(int[] arr){
  136. for(int i = 0;i < arr.length; i++){
  137. if(i % 10 == 0)
  138. System.out.println();
  139. System.out.print(arr[i]+" ");
  140. }
  141. }
  142.  
  143. static int get(int key) {
  144. int hash = (key % 10);
  145. if (table[hash] == null)
  146. return -1;
  147. else {
  148. LinkedHashEntry entry = table[hash];
  149. while (entry != null && entry.getKey() != key)
  150. entry = entry.getNext();
  151. if (entry == null)
  152. return -1;
  153. else
  154. return entry.getValue();
  155. }
  156. }
  157.  
  158. static void put(int key, int value) {
  159.  
  160. int hash = (key % 10);
  161. if (table[hash] == null)
  162. table[hash] = new LinkedHashEntry(key, value);
  163. else {
  164. LinkedHashEntry entry = table[hash];
  165. while (entry.getNext() != null && entry.getKey() != key)
  166. entry = entry.getNext();
  167. if (entry.getKey() == key)
  168. entry.setValue(value);
  169. else
  170. entry.setNext(new LinkedHashEntry(key, value));
  171. }
  172. }
  173.  
  174.  
  175. public static void main(String[] args){
  176.  
  177. arr = new int[100];
  178.  
  179. for(int i = 0;i < arr.length; i++){
  180. arr[i] = 1 + (int)(Math.random()*999);
  181. if(i%10==0)
  182. System.out.println();
  183. System.out.print(arr[i]+" ");
  184.  
  185. }
  186. System.out.println();
  187. System.out.println();
  188.  
  189.  
  190. boolean tmp = true;
  191. while(tmp) {
  192. System.out.println();
  193. System.out.println("1.Division 10 + Table\n2.Division 100\n3.Division 101\n4.Multiply 10 + Table\n5.Multiply 100\n6.Exit");
  194. int answ = sc.nextInt();
  195. switch (answ){
  196.  
  197. case 1:{
  198. table = new LinkedHashEntry[100];
  199. System.out.println();
  200. for (int i = 0; i < 100; i++)
  201. table[i] = null;
  202. hash_div_10();
  203. break;
  204. }
  205. case 2:{
  206. hash_div_100();
  207. print(hash);
  208. System.out.println();
  209. break;
  210. }
  211. case 3:{
  212. hash_div_101();
  213. print(hash);
  214. System.out.println();
  215. break;
  216. }
  217. case 4:{
  218. arrforTable = new int[100];
  219. Arrays.fill(arrforTable, -1);
  220. hash_mult_10();
  221. break;
  222. }
  223. case 5:{
  224. hash_mult_100();
  225. print(hash);
  226. System.out.println();
  227. break;
  228. }
  229. case 6:{
  230. tmp = false;
  231. break;
  232. }
  233. default:{
  234. System.out.println("REenter!");
  235. break;
  236. }
  237.  
  238. }
  239.  
  240.  
  241. // arrforTable = new int[100];
  242. // Arrays.fill(arrforTable, -1);
  243. //
  244. // hash_mult_10();
  245.  
  246.  
  247.  
  248.  
  249. // hash_div_10();
  250. // print(hash);
  251. // System.out.println();
  252. // System.out.println("Number of collisions: " + coll(hash));
  253. //
  254. // hash_div_100();
  255. // print(hash);
  256. // System.out.println();
  257. // System.out.println("Number of collisions: " + coll(hash));
  258. //
  259. // hash_div_101();
  260. // print(hash);
  261. // System.out.println();
  262. // System.out.println("Number of collisions: " + coll(hash));
  263. //
  264. // hash_mult_10();
  265. // print(hash);
  266. // System.out.println();
  267. // System.out.println("Number of collisions: " + coll(hash));
  268.  
  269. // hash_mult_100();
  270. // print(hash);
  271. // System.out.println();
  272. // System.out.println("Number of collisions: " + coll(hash));
  273.  
  274.  
  275. }
  276.  
  277. }
  278. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement