基数排序,计数排序
来源:互联网 发布:淘宝教育网视频看不了 编辑:程序博客网 时间:2024/05/17 02:07
计数排序:假设n个输入元素的每一个都是在0到k 之间,当k=O(n)时,排序的运行时间为o(n).
计数排序的基本思想是:对每一个输入的元素x,确定小于x的个数,利用这一信息,可以直接把x 放到它在输出数组中的位置了。
//计数排序void Count_Sort1(int *a,int a_length,int *b,int *c,int k,int d) //按指定位进行计数排序{ for(int i=0;i<k;i++) { c[i]=0; } for(int j=0;j<a_length;j++) { c[getdigit(a[j], d)]++; } for(int i=1;i<k;i++) { c[i]=c[i]+c[i-1]; } for(int j=a_length-1;j>=0;j--) { b[c[getdigit(a[j],d)]-1]=a[j]; //一定要进行减一操作,否者会越界 c[getdigit(a[j],d)]--; }}int getdigit(int x,int d) //求指定位置上的值,d 表示是个位,十位还是百位 { int a[] = {1, 1, 10, 100,1000}; //最大三位数,所以这里只要百位就满足了。 return (x/a[d]) % 10; }//基数排序的排序的思想是:先对个位进行计数排数然后再是十位,百位void lsdradix_sort1(int arr[],int length ,int d) //基数排序算法{ const int radix=10; int count[radix],i,j; int *bucket=(int *)malloc(length*sizeof(int)); //中间变量 Count_Sort1(arr,length,bucket,count,radix, d); for(i=0,j=0;i<length;i++,j++) { arr[i]=bucket[j]; int main(void){//含有哨兵int a[8]={2,5,3,0,2,3,0,3};int b[9];int c[6];Count_Sort(a, 8,b,c,6); int br[11] = {20, 80, 90, 589, 998, 965, 852, 123, 456, 789,8899}; int weishu=4; for(int i=1;i<=weishu;i++) { lsdradix_sort1(br,11,i); } cout<<"排序后数据如下:"<<endl; PrintArr(br, 11);/* Max_Heap_Insert(a,11,18); for(int i=1;i<=10;i++) cout<<a[i]<<" "; cout<<endl;*//* QuickSort(b,0,10); for(int i=0;i<=10;i++) cout<<b[i]<<" "; cout<<endl;*/ /* int b[100]={17,4,1,3,2,16,9,10,14,20,7,78, 17, 39, 26, 72, 94, 21, 12, 23, 91};bucket_sort(b, 21);*/ for(int i=0;i<8;i++) cout<<b[i]<<" "; cout<<endl; // HeapSort(a,10); //for(int i=1;i<=10;i++) // cout<<a[i]<<" "; // cout<<endl;return 0;}
0 0
- 计数排序,基数排序
- 计数排序和基数排序
- 计数排序-基数排序
- 计数排序和基数排序
- 基数排序和计数排序
- 计数排序和基数排序
- 计数排序+基数排序
- 计数排序和基数排序
- 基数排序/计数排序
- 计数排序和基数排序
- 基数排序,计数排序
- 计数排序和基数排序
- 计数排序实现基数排序
- 基数排序与计数排序
- 【java】计数排序 基数排序
- 计数排序和基数排序
- 计数排序和基数排序
- 计数排序和基数排序
- hpuoj--校赛--考试来了(水题)
- CodeForces 496C-- Removing Columns-暴力
- Perf -- Linux下的系统性能调优工具介绍
- HTML基础-超链接
- RSA算法
- 基数排序,计数排序
- hdu-1217(floyd)
- hdoj 5573 Binary Tree 【思维】
- python module
- hpuoj--校赛--2015年的第一场雪(暴力)
- 俄罗斯和土耳其
- 关于字符串的倒置
- PHP_XmlRpc简单实现;
- 黑马程序员——javaSE_反射