Advertisement
Guest User

Untitled

a guest
Dec 7th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.46 KB | None | 0 0
  1. import java.util.Random;
  2.  
  3. public class Lab22 {
  4.     static int probe = 0;
  5.  
  6.     public static void main(String args[]){
  7.         int count = 0;
  8.         while(count < 100){
  9.             int m = 100000;
  10.             Object[] T = new Object[m];
  11.             int[] lup = new int[m];
  12.             int[] ldown = new int[m];
  13.             Random rand = new Random();
  14.             for(int i = 0; i < 89999; i++){
  15.                 int value = rand.nextInt(100000);
  16.                 insert(T, value, lup, ldown);
  17.             }
  18.             probe = 0;
  19.             insert(T, rand.nextInt(100000), lup, ldown);
  20.             System.out.println(probe);
  21.             count++;
  22.         }
  23.     }
  24.  
  25.     protected static int h(int x, Object[] T){
  26.         return Math.floorMod(x, T.length);
  27.     }
  28.  
  29.     protected static void insert(Object[] T, int x, int[] lup, int[] ldown) {
  30.         int j = h(x, T);
  31.         if (T[j] == null) {
  32.             T[j] = x;
  33.         } else {
  34.             if (ldown[j] <= lup[j]) {
  35.                 hashInsertDown(T, x);
  36.                 ldown[j]++;
  37.             } else {
  38.                 hashInsertUp(T,x);
  39.                 lup[j]++;
  40.             }
  41.         }
  42.     }
  43.  
  44.     protected static void hashInsertDown(Object T[], int x) {
  45.         for(int i = 0; i < T.length; i++) {
  46.             int j = Math.floorMod(h(x, T) + i, T.length);
  47.             if (T[j] == null){
  48.                 T[j] = x;
  49.                 return;
  50.             }
  51.             probe++;
  52.         }
  53.         System.out.println("Element "+x+" går ej att sätta in");
  54.     }
  55.  
  56.     protected static void hashInsertUp(Object T[], int x) {
  57.         for(int i = 0; i < T.length; i++){
  58.             int j = Math.floorMod(h(x, T) - i, T.length);
  59.             if (T[j] == null){
  60.                 T[j] = x;
  61.                 return;
  62.             }
  63.             probe++;
  64.         }
  65.         System.out.println("Element "+x+" går ej att sätta in");
  66.     }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement