桶排序

1、算法原理

百度百科上面有詳細講解,http://baike.baidu.com/view/1784217.htm

2、代碼

/*+++++++++++++++++++++++++++++++
+	桶排序(C版)
+
+	原理不是很複雜。
+author:zhouyongxyz			2013-4-17 12:34
++++++++++++++++++++++++++++++++++++++++++*/
#include <cstdio>

#define N 100
typedef 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;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章