Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I have this Bucket sort. I need to add a counter to check the comparisons. In order words, how often I check one element in

I have this Bucket sort. I need to add a counter to check the comparisons. In order words, how often I check one element in the array against another. Am I doing it, right? If not, How can I fix it? Thank you

//This is a C++ Program to Sort an Array using Bucket Sort #include using namespace std; //Bucket Sort int bucketSort (int array[], int n) { //Here range is [1,100] int m = 101; //Create m empty buckets int buckets[m];

int numOfComparisons = 0; //This will maintain the number of comparisons made. //Intialize all buckets to 0 for (int i = 0; i < m; ++i) { numOfComparisons++; buckets[i] = 0; } //Increment the number of times each element is present in the input //array. Insert them in the buckets for (int i = 0; i < n; ++i) { numOfComparisons++; ++buckets[array[i]]; } //Sort using insertion sort and concatenate for (int i = 0, j = 0; j < m; ++j) for (int k = buckets[j]; k > 0; --k) { array[i++] = j; numOfComparisons++; }

return numOfComparisons; } //Driver function to test above function int main() { int inputArray[] = {1, 4, 0, 4, 0, 0, 3, 4, 0, 3}; int n = sizeof (inputArray) / sizeof (inputArray[0]); int numOfComparisons = bucketSort(inputArray, n); cout << "Sorted Array : " << endl; for (int i = 0; i < n; ++i) cout << inputArray[i] << " "; cout<< endl;

cout<<"The number of comparisons done in this bucket sort is: "<

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

Give three examples of situations which may lead to provisions?

Answered: 1 week ago