Bucket sorting in C++

HigherGround

Golden Member
Jan 9, 2000
1,827
0
0
here's c++ implementation and a driver (using map<...> class as bucket holder). ask away if you have any questions.


void bucket_sort(double a[], int n)
{
int k, j = 0;

map<double, int> buckets;

for(k=0; k<n; k++)
buckets[a[k]]++;

map<double, int>::iterator it = buckets.begin( );
while(it != buckets.end( ))
{
for(k=0; k<it->second; k++)
a[j++] = it->first;
it++;
}
}

int main(int argc, char* argv[])
{
double a[] = { 4, 5, 1, 5, 11, 4, 11, 7, 9, 0.1, 1, 6, 453.1, 23.1, 34.12, 33.22, 4, 6.0, 1.0, 1.0 };
bucket_sort(a, 20);
return 0;
}


grrrr, [i.]s are converted to italics :)