Пример #1
0
  public static void main(String[] args) {

    strings = new String[N];

    for (int i = 0; i < N; i++) {
      strings[i] = rndString();
    }
    System.out.printf("begin\n");

    boolean mine = true;
    if (mine) {

      long time4 = System.currentTimeMillis();

      HashTable ht = new HashTable();

      for (int i = 0; i < N; i++) {
        ht.insert(strings[i]);
      }

      System.out.printf("time: %d\n", System.currentTimeMillis() - time4);

      System.out.printf("col: %d\n", ht.colProbe);

      long time1 = System.currentTimeMillis();

      for (int j = 0; j < 10; j++) {
        for (int i = 0; i < N; i++) {
          int idx = ht.search(strings[i]);
          if (idx == -1) {
            System.out.printf("%s: %d %d %s\n", strings[i], i, idx, idx != -1 ? "true" : "false");
          }
        }
      }
      System.out.printf("time: %d\n", System.currentTimeMillis() - time1);
      System.out.printf("hops: %d %d %f\n", ht.nhops, ht.nsearch, (float) ht.nhops / ht.nsearch);
    }
    if (!mine) {
      long time3 = System.currentTimeMillis();
      HashSet<String> hs = new HashSet<String>();

      for (int i = 0; i < N; i++) {
        hs.add(strings[i]);
      }

      System.out.printf("time: %d\n", System.currentTimeMillis() - time3);

      long time2 = System.currentTimeMillis();

      for (int j = 0; j < 10; j++) {
        for (int i = 0; i < N; i++) {

          if (!hs.contains(strings[i])) {
            System.out.printf("%s: %d\n", strings[i], i);
          }
        }
      }
      System.out.printf("time: %d\n", System.currentTimeMillis() - time2);
    }
  }
  public static void main(String[] args) {
    Link element;
    int size, nrEl;
    Scanner sc = new Scanner(System.in);
    System.out.print("Introduceti dimensiunea tabelei: ");
    size = sc.nextInt();
    System.out.print("Introduceti numarul de elemente: ");
    nrEl = sc.nextInt();

    HashTable htb = new HashTable(size);

    // inserare
    for (int i = 0; i < nrEl; i++) {
      // generare random elemente intre 1 si 100
      Random rand = new Random();
      int key = rand.nextInt((100 - 1) + 1) + 1;
      element = new Link(key);
      htb.insert(element);
    }

    htb.displayTable();

    // inserare element
    System.out.print("Insereaza un element: ");
    int elIns = sc.nextInt();
    element = new Link(elIns);
    htb.insert(element);
    htb.displayTable();

    // stergere element
    System.out.print("Sterge un element: ");
    int elDeSters = sc.nextInt();
    htb.delete(elDeSters);
    htb.displayTable();

    System.out.print("Cauta un element: ");
    int elCautat = sc.nextInt();
    element = htb.find(elCautat);
    if (element != null) System.out.println("A fost gasit " + elCautat);
    else System.out.println("Nu a fost gasit " + elCautat);

    sc.close();
  }
Пример #3
0
  private void grow() {
    String[] oldStrings = strings;
    //	boolean[] oldHt = ht;
    //	int oldM = M;
    System.out.printf("grow: %d %d %d %f\n", M, nihops, ninserts, (float) nihops / ninserts);
    nihops = 0;
    ninserts = 0;

    M *= 2;
    nE = 0;
    colProbe = 0;
    threshold = (int) (M * loadFactor);
    strings = new String[M];
    // ht = new boolean[M];
    ht = new BitSet(M);

    for (int i = 0; i < oldStrings.length; i++) {
      if (oldStrings[i] != null) {
        insert(oldStrings[i]);
      }
    }
  }