ou need to put counters in the code like you did in lab1. You just have to count all the searches (probes) and the stores. It is very similar to lab1 but easier because you don't have to do the linked lists. Put a counter in to count how many probes (searches) you have to do to find a place to store each piece of data, and also count when you actually store it. lab 2 void Hash(int *dta, int n, int &counter, int *&HashTable, int &TableSize, int swch) { TableSize = ComputeNearestPrime(2*n + 1); // you write this function - computes nearest prime closest but greater than 2 * n try { HashTable = new int [TableSize]; if(HashTable == NULL)throw "allocation error"; } catch (...) { cout << "Table allocation error - out of memory" << endl; } for(int i = 0; i < TableSize; i++)HashTable[i] = -1; // initialize counter = 0; int R; if (TableSize / 3 <= 1)R = 1; else R = ComputeNearestPrime(TableSize/3); // for use in double hashing (this is the for the second hash h(x) = (x%Ts + i * (R - x%R))%Ts // you write the code that does the hashing // *********************** // YOUR CODE GOES HERE // ************************ switch (swch) { case 0: for (int i = 1; i < n; i++) { int m = 0; while (HashTable[((dta[i] % TableSize) + m) % TableSize] != -1) { m++; } HashTable[((dta[i] % TableSize) + m) % TableSize] = dta[i]; counter++; } break; case 1: for (int i = 1; i < n; i++) { int m = 0; while (HashTable[((dta[i] % TableSize) + (m*m)) % TableSize] != -1) { m++; } HashTable[((dta[i] % TableSize) + (m*m)) % TableSize] = dta[i]; counter++; } break; case 2: for (int i = 1; i < n; i++) { int m = 0; while (HashTable[((dta[i] % TableSize) + (m*(R - (dta[i] % R)))) % TableSize] != -1) { m++; counter++; } HashTable[((dta[i] % TableSize) + (m*(R - (dta[i] % R)))) % TableSize] = dta[i]; counter++; } break; } } lab 1 code is mentioned below. lab 1 code int radixSort(int *dta, int n, int *out) { node *bucket[1000]; int count = 0; for(int i = 0; i < n; i++)out[i] = dta[i]; for (int Pass = 0; Pass < 3; Pass++) // this is the outer loop { for (int j = 0; j < 1000; j++)// set bucket[] to all //zeroes(NULL) for each pass bucket[j] = NULL; // loop to place data in correct buckets for current pass for (int i = 0; i < n; i++) // inner loop - walk through the out array ( which contains the data to be sorted ) { int index = 0; switch (Pass) { case 0: { index = out[i] % 1000;//return last three digits (least significant) } break; case 1: { index = (out[i] / 1000) % 1000; // return middle three digits } break; case 2: { index = out[i] / 1000000;// return first 3 digits (most significant) } break; } if (bucket[index] == NULL) // nothing is stored here so { bucket[index] = new node(out[i], NULL); // create a new head node and store this data count++; // count this } else { node *ptr = bucket[index]; while (ptr->next != NULL) { ptr = ptr->next; // walk the list count++; //count this also } // make a new node and attach it to the tail ptr->next = new node(out[i], NULL); count++; // count this } } //Now out must be updated from the buckets, in the new order int idx = 0; for(int j = 0; j < 1000; j++) { node *tmp; node *ptr = bucket[j]; while(ptr != NULL) { tmp = ptr; ptr = ptr->next; out[idx++] = tmp->data; delete tmp; count++; } } //end of pass } return count; }
you need to put counters in the code like you did in lab1. You just have to count all the searches (probes) and the stores.
It is very similar to lab1 but easier because you don't have to do the linked lists.
Put a counter in to count how many probes (searches) you have to do to find a place to store each piece of data, and also count when you actually store it.
lab 2
void Hash(int *dta, int n, int &counter, int *&HashTable, int &TableSize, int swch)
{
TableSize = ComputeNearestPrime(2*n + 1); // you write this function - computes nearest prime closest but greater than 2 * n
try
{
HashTable = new int [TableSize];
if(HashTable == NULL)throw "allocation error";
}
catch (...)
{
cout << "Table allocation error - out of memory" << endl;
}
for(int i = 0; i < TableSize; i++)HashTable[i] = -1; // initialize
counter = 0;
int R;
if (TableSize / 3 <= 1)R = 1;
else R = ComputeNearestPrime(TableSize/3); // for use in double hashing (this is the for the second hash h(x) = (x%Ts + i * (R - x%R))%Ts
// you write the code that does the hashing
// ***********************
// YOUR CODE GOES HERE
// ************************
switch (swch)
{
case 0:
for (int i = 1; i < n; i++)
{
int m = 0;
while (HashTable[((dta[i] % TableSize) + m) % TableSize] != -1)
{
m++;
}
HashTable[((dta[i] % TableSize) + m) % TableSize] = dta[i];
counter++;
}
break;
case 1:
for (int i = 1; i < n; i++)
{
int m = 0;
while (HashTable[((dta[i] % TableSize) + (m*m)) % TableSize] != -1)
{
m++;
}
HashTable[((dta[i] % TableSize) + (m*m)) % TableSize] = dta[i];
counter++;
}
break;
case 2:
for (int i = 1; i < n; i++)
{
int m = 0;
while (HashTable[((dta[i] % TableSize) + (m*(R - (dta[i] % R)))) % TableSize] != -1)
{
m++;
counter++;
}
HashTable[((dta[i] % TableSize) + (m*(R - (dta[i] % R)))) % TableSize] = dta[i];
counter++;
}
break;
}
}
lab 1 code is mentioned below.
lab 1 code
int radixSort(int *dta, int n, int *out)
{
node *bucket[1000];
int count = 0;
for(int i = 0; i < n; i++)out[i] = dta[i];
for (int Pass = 0; Pass < 3; Pass++) // this is the outer loop
{
for (int j = 0; j < 1000; j++)// set bucket[] to all //zeroes(NULL) for each pass
bucket[j] = NULL;
// loop to place data in correct buckets for current pass
for (int i = 0; i < n; i++) // inner loop - walk through the out array ( which contains the data to be sorted )
{
int index = 0;
switch (Pass)
{
case 0:
{
index = out[i] % 1000;//return last three digits (least significant)
}
break;
case 1:
{
index = (out[i] / 1000) % 1000; // return middle three digits
}
break;
case 2:
{
index = out[i] / 1000000;// return first 3 digits (most significant)
}
break;
}
if (bucket[index] == NULL) // nothing is stored here so
{
bucket[index] = new node(out[i], NULL); // create a new head node and store this data
count++; // count this
}
else
{
node *ptr = bucket[index];
while (ptr->next != NULL)
{
ptr = ptr->next; // walk the list
count++; //count this also
}
// make a new node and attach it to the tail
ptr->next = new node(out[i], NULL);
count++; // count this
}
}
//Now out must be updated from the buckets, in the new order
int idx = 0;
for(int j = 0; j < 1000; j++)
{
node *tmp;
node *ptr = bucket[j];
while(ptr != NULL)
{
tmp = ptr;
ptr = ptr->next;
out[idx++] = tmp->data;
delete tmp;
count++;
}
}
//end of pass
}
return count;
}
Trending now
This is a popular solution!
Step by step
Solved in 2 steps