算法与数据结构-常用排序算法总结2-基数排序
来源:互联网 发布:autodesk 软件绘图技巧 编辑:程序博客网 时间:2024/06/04 19:39
序言
排序算法大体可分为两种:
比较排序:冒泡排序,选择排序,插入排序,归并排序,堆排序,快速排序等非比较排序:基数排序,计数排序,桶排序等
本文介绍非比较排序算法中的基数排序算法。
基数排序(Radix Sort)
原理:
以整型基数排序为例,整型10进制数按位数切割成不同的数字,然后从低位到高位每个位数分别比较,每次比较完进行排序,直到整个数组有序。
由于整数也可以表达字符串(比如名字或日期)和特定格式的浮点数,所以基数排序也不是只能使用于整数。
动态示意图:http://www.cs.usfca.edu/~galles/visualization/RadixSort.html
步骤:算法主要分为两个过程,分配 + 收集
将所有待比较数值(正整数)统一为同样的数位长度,数位较短的数前面补零。
分配:从低位开始,根据位值(0~9)分别放到0~9号桶中
- 比如26,个位为6,放入6号桶中
收集:再将0~9号桶中的数据按顺序(升序或降序)放到数组中
重复分配和收集过程,从个位到最高位,直到排好序为止
注:为何这样就能完成排序?
答:假设高位相同,次高位已按顺序排好,因此保证了顺序。要点
桶空间
- 假设基数为r,数组元素个数为n,桶空间是一个r × n的空间
分配
收集
时间复杂度分析:
平均时间复杂度: O(d(n + r)) = O(n * d)
- 因为每位每次排序时:
- 分配的时间开销为O(n),n个数需要处理
- 收集的时间开销为O(r),0~r-1大小的数需要处理
- d位比较下来的时间开销为O(d(n + r))
- 因为每位每次排序时:
最好情况:O(d(n + r))
最坏情况:O(d(n + r))
空间复杂度分析:O(rd + n)
评价:
稳定排序
时间复杂度低,需要额外的辅助空间
Code Example1:
/*************************************** 参数: r:基数,比如r = 10表示十进制划分,即表示0~9的序列 d:位数,十进制时即digit有多少位***************************************/#include <stdio.h>#include <stdlib.h> //for malloc()#include <string.h> //for memset()/* 获取一个十进制数的第pos位 */int GetPosInNum(int num, int pos){ int temp = 1; for (int i = 0; i < pos - 1; i++) //整除数值 temp *= 10; return (num / temp) % 10; //取余}/* 基数排序 */int RadixSort(int *array, int n, int r, int d){ //申请序列存储空间 int *radixArray[r]; for (int i = 0; i < r; i++) { radixArray[i] = (int *)malloc(sizeof(int) * (n + 1)); //n + 1,其中一个元素用来计数 memset(radixArray[i], 0, n + 1); radixArray[i][0] = 0; //用于记录这个digit为这个取值的数组元素的个数,digit落在0~r-1 } //位比较排序 for (int pos = 0; pos < d; pos++) { //分配过程:处理n个元素 for (int j = 0; j < n; j++) { int num = GetPosInNum(array[j], pos); int index = ++radixArray[num][0]; //统计落在这个取值的元素个数 radixArray[num][index] = array[j]; } //收集过程:从r个空间取数 for (int k = 0, cnt = 0; k < r; k++) { for (int ele = 1; ele <= radixArray[k][0]; ele++) { array[cnt++] = radixArray[k][ele]; } radixArray[k][0] = 0; //复位用于重新计数 } } return 0;}int main(){ int a[9] = {8, 4, 2, 3, 1, 6, 9, 0, 7}; //函数调用 int r = 10, d = 10; //基数为10,位数为10 RadixSort(a, 9, r, d); //数组输出 for (int j = 0; j < 9; j++) { printf("%d ", a[j]); } printf("\n");}
Code Example2:
在基数排序中使用了计数排序,计数排序详见我的另一篇博客:http://blog.csdn.net/baidu_35692628/article/details/76736208
/**************************************功能:使用计数排序实现基数排序参数: pos:第pos位 n:数组元素个数 d:计数数组需要使用的数组元素最大数值**************************************/#include <stdio.h>#include <stdlib.h>#include <string.h>const int d = 3; //待排序的元素为三位数及以下const int r = 10; //基数为10,每一位的数字都是[0,9]内的整数/* 获取元素的第pos位数字 */int GetDigit(int ele, int pos){ int temp = 1; for (int i = 0; i < pos - 1; i++) temp *= 10; return (ele / temp) % 10;}/* 计数排序:对数组元素的第pos位构成的数组进行计数排序 */void CountingSort(int A[], int n, int pos){ //计数数组初始化 int C[r]; for (int i = 0; i < r; i++) { C[i] = 0; } //统计第pos位构成的数组各个元素出现次数 for (int i = 0; i < n; i++) { C[GetDigit(A[i], pos)]++; } //计数累加 for (int i = 1; i < r; i++) { C[i] = C[i] + C[i - 1]; } //暂存数组赋值:从后往前赋值,保证排序稳定性 int *temp = (int*)malloc(n * sizeof(int)); memset(temp, 0, n); for (int i = n - 1; i >= 0; i--) { temp[C[GetDigit(A[i], pos)] - 1] = A[i]; C[GetDigit(A[i], pos)]--; } //数组赋值 for (int i = 0; i < n; i++) { A[i] = temp[i]; } free(temp);}/* 最低位优先基数排序 */void RadixSort(int A[], int n){ for (int digit = 1; digit <= d; digit++) //从个位到十位到百位 CountingSort(A, n, digit); //对数组A的第digit位构成的数组进行计数排序}int main(){ int A[] = { 20, 90, 64, 289, 998, 365, 852, 123, 789, 456 }; int n = sizeof(A) / sizeof(int); //函数调用,结果输出 RadixSort(A, n); printf("排序结果:"); for (int i = 0; i < n; i++) { printf("%d ", A[i]); } printf("\n"); return 0;}
Acknowledgements:
http://blog.csdn.net/han_xiaoyang/article/details/12163251#t139
http://blog.csdn.net/hitwhylz/article/details/9970451
http://blog.csdn.net/FightLei/article/details/52586814
http://www.cnblogs.com/xiaochun126/p/5086037.html
http://www.cnblogs.com/eniac12/p/5332117.html(推荐)
2017.08.06
阅读全文
0 0
- 算法与数据结构-常用排序算法总结2-基数排序
- 算法与数据结构-常用排序算法总结2-计数排序
- 算法与数据结构-常用排序算法总结2-桶排序
- java数据结构与算法-高级排序-基数排序
- 常用排序算法总结8一一基数排序
- [数据结构与算法]基数排序
- 【数据结构与算法】基数排序
- 算法与数据结构-常用排序算法总结1-比较排序
- 数据结构常用排序算法总结
- 数据结构与算法分析笔记与总结(java实现)--排序7:基数排序练习题
- 常用排序算法之计数排序,基数排序与桶排序
- 常用排序算法之基数排序
- 常用排序算法之基数排序
- 数据结构与算法系列----基数排序
- 排序算法总结(9)--基数排序
- 【数据结构与算法】【排序】总结
- 数据结构与算法-排序总结
- 常用数据结构与算法之排序算法
- linux内核--段页式管理内存的方法
- redis的基本命令
- mybatis示例1
- foreach关键字
- thinkphp 验证码
- 算法与数据结构-常用排序算法总结2-基数排序
- c++中的操作符重载
- Spring Exception:Exception in thread "main" java.lang.NoClassDefFoundError:org/apache/commons/loggin
- 在错误的时间遇上乐视 谁会接手风雨飘摇中的酷派?
- 面向对象的五个基本原则
- 电商项目实战——第一天(环境部署)4搭建Nginx,域名解析配置
- MBR详解
- 3DSlicer13:Command Line Interface(CLI)
- Android零基础入门第26节:layout_gravity和gravity大不同