K KrackerJack Member Jan 16, 2001 65 0 0 Apr 9, 2001 #1 Does anybody have an example of bucket sorting in c++ code.Please help! Kracker
K kmmatney Diamond Member Jun 19, 2000 4,363 1 81 Apr 9, 2001 #2 No, but I have bucket sorting examples in Visual Basic code.
H HigherGround Golden Member Jan 9, 2000 1,827 0 0 Apr 9, 2001 #3 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
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