#include #include #include #include /* #define TABSIZE 2000000 #define TABSIZELESS1 (TABSIZE - 1) */ int TABSIZE=20000000; int *hash; int n; int nextPrime(int x) { // Find a prime number >= x int work,k,remainder,quotient; if (x%2==1) work=x; else work=x+1; while (1) { for (k=3; ;k+=2) { remainder=work%k; if (remainder==0) break; quotient=work/k; if (quotient=0;j--) { jj = (init + inc * j) % TABSIZE; ii = (jj + increment(r[jj]) * (i - j)) % TABSIZE; // printf("i=%d j=%d jj=%d ii=%d\n",i,j,jj,ii); if (r[ii] == (-1)) { r[ii] = r[jj]; r[jj] = key; n++; return i+1; } } } printf("insert failed\n"); return 999999999; } void doInserts() { int key; int i, j, k; int probelengthtotal=0; float startCPU; startCPU=CPUtime(); for (k=1;k<=0.99*TABSIZE;k++) { // scanf("%d",&key); key=generateRandom(0,100*TABSIZE); probelengthtotal+=insert(key, hash); /* printf("hash=%d increment=%d\n",hashfunction(key),increment(key)); for (i=0;iwc) wc=probes; totalProbes+=probes; } printf("Retrievals took %.3f secs\n",CPUtime()-startCPU); printf("Worst case probes is %d\n",wc); printf("Total probes %d\n",totalProbes); printf("Expected probes is %.3f\n",((float)totalProbes)/n); printf("Probe counts:\n"); for (i=1;i<30;i++) printf("Number of keys using %d probes is %d\n",i,count[i]); printf("Number of keys using >=30 probes is %d\n",count[30]); } int main() { int seed; TABSIZE=nextPrime(TABSIZE); hash=(int*) malloc(TABSIZE*sizeof(int)); if (!hash) { printf("malloc failed %d\n",__LINE__); exit(0); } printf("Enter seed:\n"); scanf("%d",&seed); initTable(); srandom(seed); doInserts(); srandom(seed); doRetrievals(); }