[6 2 4 1 5 9]
[6 2 4 1 5 9] 待排数组 [0 0 0 0 0 0 0 0 0 0] 空桶 [0 1 2 3 4 5 6 7 8 9] 桶编号(实际不存在)
[6 2 4 1 5 9] 待排数组 [0 0 0 0 0 0 6 0 0 0] 空桶 [0 1 2 3 4 5 6 7 8 9] 桶编号(实际不存在)
[6 2 4 1 5 9] 待排数组 [0 0 2 0 0 0 6 0 0 0] 空桶 [0 1 2 3 4 5 6 7 8 9] 桶编号(实际不存在) 3,4,5,6省略,过程一样,全部入桶后变成下边这样 [6 2 4 1 5 9] 待排数组 [0 1 2 0 4 5 6 0 0 9] 空桶 [0 1 2 3 4 5 6 7 8 9] 桶编号(实际不存在)
1 2 4 5 6 9
#include<iostream>
#include<list>
#include<algorithm>
using namespace std;
void bucketsort(double* a, int n) {
list<double>* b = new list<double>[n];
for (int i = 0; i < n; i++) {
b[int(a[i])].push_back(a[i]);
}
for (int i = 0; i < n; i++) {
b[i].sort();
}
for (int i = 0,j=0; i < n; i++) {
while (b[j].size() < 1)j++;
a[i] = b[j].front();
b[j].pop_front();
}
}
int main() {
double arr[] = {0.1,1.1,2.2,3.5,1.5,2.3,7.5,1.7};
int n = 8;
bucketsort(arr, n);
for (int i = 0; i < 8; i++) {
cout << arr[i] << " ";
}
cout << endl;
return 0;
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有