Question
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
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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started