#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;
}
Comments
Leave a comment