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; }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
 

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;
}

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Threads in linked list
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education