Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 17th, 2012  |  syntax: Java  |  size: 1.74 KB  |  hits: 15  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. package Blatt09;
  2.  
  3. public class HashLinQuad {
  4.         private final int[] table;
  5.         private int size; // current number of elements
  6.         private final int capacity;
  7.  
  8.         // Konstruktor
  9.         private HashLinQuad(int cap) {
  10.                 capacity = cap;
  11.                 table = new int[cap];
  12.         }
  13.  
  14.         public int addLin(int obj) {
  15.                 if (size == capacity) { // Endlosschleifen vermeiden
  16.                         System.out.println("Hashtabelle ist voll!");
  17.                         return 0;
  18.                 }
  19.                 int position = obj % capacity;
  20.                 int collision = 0;
  21.  
  22.                 while (table[position] != 0) { // Kollision!
  23.                         collision++;
  24.                         position++;
  25.  
  26.                         if (position > capacity) { // im "Rahmen" bleiben
  27.                                 position %= capacity;
  28.                         }
  29.                 }
  30.                 table[position] = obj;
  31.                 return collision;
  32.  
  33.         }
  34.  
  35.         public int addQuad(int obj) {
  36.                 if (size == capacity) {
  37.                         System.out.println("Hashtabelle ist voll!");
  38.                         return 0;
  39.                 }
  40.                 int position = obj % capacity;
  41.                 int collision = 0;
  42.                 int counter = 0;
  43.  
  44.                 while (table[position] != 0) {
  45.                         collision++;
  46.                         counter++;
  47.                         position += counter * counter;
  48.  
  49.                         if (position > capacity) {
  50.                                 position %= capacity;
  51.                         }
  52.                 }
  53.                 table[position] = obj;
  54.                 return collision;
  55.         }
  56.  
  57.         @Override
  58.         public String toString() {
  59.                 String str = "";
  60.                 for (int element : table) { // durch die Tabelle iterieren & ausgeben
  61.                         str += " " + element;
  62.                 }
  63.                 return str;
  64.         }
  65.  
  66.         public static void main(String[] args) {
  67.  
  68.                 HashLinQuad lTable = new HashLinQuad(1249);
  69.                 HashLinQuad qTable = new HashLinQuad(1249);
  70.                 int linCol = 0;
  71.                 int quadCol = 0;
  72.  
  73.                 for (int i = 0; i < 1000; i++) {
  74.                         int obj = (int) (Math.random() * 1000);
  75.                         linCol += lTable.addLin(obj); // Kollisionen zaehlen
  76.                         quadCol += qTable.addQuad(obj);
  77.                 }
  78.                 System.out.println("linear: " + linCol);
  79.                 System.out.println("quadratisch: " + quadCol);
  80.         }
  81. }