Answer to Question #249756 in C++ for Dunag

Question #249756
Write a program that does Bucketsort in C++
1
Expert's answer
2021-10-12T00:37:01-0400
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
 
void bucketSort(float A[], int x)
{
    vector<float> y[x];
 
    for (int i = 0; i < x; i++) {
        int yi = x * A[i]; 
        y[yi].push_back(A[i]);
    }
    for (int i = 0; i < x; i++)
        sort(y[i].begin(), y[i].end());
    int index = 0;
    for (int i = 0; i < x; i++)
        for (int j = 0; j < y[i].size(); j++)
            A[index++] = y[i][j];
}
int main()
{
    float A[]
        = { 0.544, 0.8645, 0.756, 0.11234, 0.775, 0.3134,0.007};
    int x = sizeof(A) / sizeof(A[0]);
    bucketSort(A, x);
    cout << "Sorted array is \n";
    for (int i = 0; i < x; i++)
        cout << A[i] << " ";
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS