桶排序

来源:互联网 发布:mac搜索文件快捷键 编辑:程序博客网 时间:2024/04/28 11:49

1、算法原理

百度百科上面有详细讲解,http://baike.baidu.com/view/1784217.htm。

2、代码

/*++++++++++++++++++++++++++++++++桶排序(C版)++原理不是很复杂。+author:zhouyongxyz2013-4-17 12:34++++++++++++++++++++++++++++++++++++++++++*/#include <cstdio>#define N 100typedef int ElementType;void BarrelSort(ElementType a[],int n);int main(){int a[]={3,5,2,2,7,8,1,6,9,4};BarrelSort(a,10);for(int i=0;i<10;i++)printf("%d ",a[i]);printf("\n");return 0;}void BarrelSort(ElementType a[],int n){int barrel[N]={0};int i,t=0;for(i=0;i<n;i++)barrel[a[i]]++;for(i=0;i<N;i++)while(barrel[i]--)a[t++]=i;}