桶排序算法 ← C++ 实现

【算法介绍】
桶排序的思想:
若待排序的值在一个有限范围内(整型)时,可设计有限个有序桶桶号就是待排序的值。
之后,将待排序的值依次装入对应的有序桶中(重复值都装入对应的桶),然后顺序输出各桶的值,便得到有序的序列。

【算法代码】

#include<bits/stdc++.h>
using namespace std;

const int maxn=500;
int t[maxn];

int main() {
    int n;
    cin>>n;

    int x; //x is less than maxn
    for(int i=1; i<=n; i++) {
        cin>>x;
        t[x]++;
    }

    for(int j=1; j<=maxn; j++) {
        while(t[j]>0) {
            cout<<j<<" ";
            t[j]--;
        }
    }
    return 0;
}

/*
in:
10
20 40 32 67 40 20 89 300 400 15
out:
15 20 20 32 40 40 67 89 300 400
*/


【参考文献】
https://blog.csdn.net/sdau20171989/article/details/79198443
https://blog.csdn.net/qq_27124771/article/details/87651495

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值