计数排序_桶排序
1112-数组排序
题目描述(Description)
对数组的元素按从小到大进行排序。
输入格式(Format Input)
有两行,第一行有一个整数 n (5 <=n <=5000000),第二行有 n 个小于等于 100000, 大于等于 0 的整数
输出格式(Format Output)
输出更新后的数组
输入样例 1(Sample Input 1)
8
1 2 3 6 8 7 4 5
输出样例 1(Sample Output 1)
1 2 3 4 5 6 7 8
限制(Restrictions)
时间限制(Time Limit): 1000 ms
内存限制(Memory Limit): 65536 KB
说明/提示
#include<bits/stdc++.h>
using namespace std;
int f[100001];int main(){memset(f,0,sizeof(f));int n;cin>>n;for(int i=1;i<=n;i++){int x;cin>>x;f[x]++;}for(int i=0;i<=10000;i++){for(int j=1;j<=f[i];j++){cout<<i<<" ";}}return 0;
}
去重代码
#include<bits/stdc++.h>
using namespace std;
int f[10001];
int main(){memset(f,0,sizeof(f));int n;cin>>n;for(int i=1;i<=n;i++){int x;cin>>x;f[x]++;}for(int i=1;i<=10000;i++){if(f[i]>0){cout<<i<<" "; }}return 0;
}