Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import com.sun.net.httpserver.Authenticator;
- import java.util.Arrays;
- import java.util.Scanner;
- class LinkedHashEntry {
- private int key;
- private int value;
- private LinkedHashEntry next;
- LinkedHashEntry(int key, int value) {
- this.key = key;
- this.value = value;
- this.next = null;
- }
- public int getValue() {
- return value;
- }
- public void setValue(int value) {
- this.value = value;
- }
- public int getKey() {
- return key;
- }
- public LinkedHashEntry getNext() {
- return next;
- }
- public void setNext(LinkedHashEntry next) {
- this.next = next;
- }
- }
- public class Main {
- static Scanner sc = new Scanner(System.in);
- static int[] arr;
- static int[] hash;
- static int[] arrforTable;
- static int arraySize = 100;
- static LinkedHashEntry[] table;
- static void hash_div_10() {
- hash = new int[arr.length];
- for(int i = 0;i < arr.length; i++){
- hash[i] = arr[i]%10;
- System.out.println(hash[i]);
- }
- for(int i = 0;i < arr.length; i++){
- put(i,hash[i]);
- System.out.println("Index " + i%10+ " Value " + get(i));
- }
- System.out.println();
- System.out.println("Number of collisions: " + coll(hash));
- System.out.println();
- }
- static void hash_div_100() {
- hash = new int[arr.length];
- for(int i = 0;i < arr.length; i++){
- hash[i] = arr[i]%100;
- }
- System.out.println("Number of collisions: " + coll(hash));
- }
- static void hash_div_101() {
- hash = new int[arr.length];
- for(int i = 0;i < arr.length; i++){
- hash[i] = arr[i]%101;
- }
- System.out.println("Number of collisions: " + coll(hash));
- }
- static void hash_mult_10() {
- hash = new int[arr.length];
- double A=0.618033;
- for(int i = 0;i < arr.length; i++) {
- double tmp = (arr[i] * A) * arr.length;
- hash[i] = (int) Math.abs(tmp)% 10;
- }
- for(int n=0;n<arrforTable.length; n++){
- int element = hash[n];
- int arrIndex = element % 10;
- //System.out.println(arrIndex);
- System.out.println("Index= " + arrIndex + " for value " + element);
- while (arrforTable[arrIndex] != -1) {
- ++arrIndex;
- System.out.println("Collision Try " + arrIndex + " Instead");
- // If we get to the end of the array go back to index 0
- arrIndex %= arraySize;
- }
- arrforTable[arrIndex] = element;
- System.out.println("Success!");
- }
- System.out.println("Number of collisions: " + coll(hash));
- }
- static void hash_mult_100() {
- hash = new int[arr.length];
- double A=0.618033;
- for(int i = 0;i < arr.length; i++){
- double tmp = (arr[i]*A)*arr.length;
- //System.out.println(tmp%100);
- hash[i] = (int)Math.abs(tmp%100);
- }
- System.out.println("Number of collisions: " + coll(hash));
- }
- static int coll(int[] arr) {
- int counter = 0;
- for (int i = 0; i < arr.length; i++ ){
- for (int j = i+1; j < arr.length; j++){
- if(arr[i] == arr[j])
- counter++;
- }
- }
- return counter;
- }
- static void print(int[] arr){
- for(int i = 0;i < arr.length; i++){
- if(i % 10 == 0)
- System.out.println();
- System.out.print(arr[i]+" ");
- }
- }
- static int get(int key) {
- int hash = (key % 10);
- if (table[hash] == null)
- return -1;
- else {
- LinkedHashEntry entry = table[hash];
- while (entry != null && entry.getKey() != key)
- entry = entry.getNext();
- if (entry == null)
- return -1;
- else
- return entry.getValue();
- }
- }
- static void put(int key, int value) {
- int hash = (key % 10);
- if (table[hash] == null)
- table[hash] = new LinkedHashEntry(key, value);
- else {
- LinkedHashEntry entry = table[hash];
- while (entry.getNext() != null && entry.getKey() != key)
- entry = entry.getNext();
- if (entry.getKey() == key)
- entry.setValue(value);
- else
- entry.setNext(new LinkedHashEntry(key, value));
- }
- }
- public static void main(String[] args){
- arr = new int[100];
- for(int i = 0;i < arr.length; i++){
- arr[i] = 1 + (int)(Math.random()*999);
- if(i%10==0)
- System.out.println();
- System.out.print(arr[i]+" ");
- }
- System.out.println();
- System.out.println();
- boolean tmp = true;
- while(tmp) {
- System.out.println();
- System.out.println("1.Division 10 + Table\n2.Division 100\n3.Division 101\n4.Multiply 10 + Table\n5.Multiply 100\n6.Exit");
- int answ = sc.nextInt();
- switch (answ){
- case 1:{
- table = new LinkedHashEntry[100];
- System.out.println();
- for (int i = 0; i < 100; i++)
- table[i] = null;
- hash_div_10();
- break;
- }
- case 2:{
- hash_div_100();
- print(hash);
- System.out.println();
- break;
- }
- case 3:{
- hash_div_101();
- print(hash);
- System.out.println();
- break;
- }
- case 4:{
- arrforTable = new int[100];
- Arrays.fill(arrforTable, -1);
- hash_mult_10();
- break;
- }
- case 5:{
- hash_mult_100();
- print(hash);
- System.out.println();
- break;
- }
- case 6:{
- tmp = false;
- break;
- }
- default:{
- System.out.println("REenter!");
- break;
- }
- }
- // arrforTable = new int[100];
- // Arrays.fill(arrforTable, -1);
- //
- // hash_mult_10();
- // hash_div_10();
- // print(hash);
- // System.out.println();
- // System.out.println("Number of collisions: " + coll(hash));
- //
- // hash_div_100();
- // print(hash);
- // System.out.println();
- // System.out.println("Number of collisions: " + coll(hash));
- //
- // hash_div_101();
- // print(hash);
- // System.out.println();
- // System.out.println("Number of collisions: " + coll(hash));
- //
- // hash_mult_10();
- // print(hash);
- // System.out.println();
- // System.out.println("Number of collisions: " + coll(hash));
- // hash_mult_100();
- // print(hash);
- // System.out.println();
- // System.out.println("Number of collisions: " + coll(hash));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement