【排序算法】计数排序
2015-09-19 22:02
295 查看
转载来自:计数排序之Java实现
比较排序算法可以通过决策树模型证明,其下线是O(nlgn)。而本文介绍的是时间效率为O(n)的计数排序。所谓排序算法,无非就是把正确的元素放到正确的位置,计数排序就是计算相同key的元素各有多少个,然后根据出现的次数累加而获得最终的位置信息。但是计数排序有两个限制条件,那就是存在一个正整数K,使得数组里面的所有元素的key值都不大于N,且key值都是非负整数。
计数排序算法步骤大概有三个步骤:
建一个长度为K+1的的数组C,里面的每一个元素初始都置为0(Java里面默认就是0)。
遍历待排序的数组,计算其中的每一个元素出现的次数,比如一个key为i的元素出现了3次,那么C[i]=3。
累加C数组,获得元素的排位,从0开始遍历C, C[i+1]=C[i]+C[i-1]
建一个临时数组T,长度与待排序数组一样。从数组末尾遍历待排序数组,把元素都安排到T里面,直接从C里面就可以得到元素的具体位置, 不过记得每处理过一个元素之后都要把C里面对应位置的计数减1。
Java实现如下:
测试结果:
计数排序算法介绍
比较排序算法可以通过决策树模型证明,其下线是O(nlgn)。而本文介绍的是时间效率为O(n)的计数排序。所谓排序算法,无非就是把正确的元素放到正确的位置,计数排序就是计算相同key的元素各有多少个,然后根据出现的次数累加而获得最终的位置信息。但是计数排序有两个限制条件,那就是存在一个正整数K,使得数组里面的所有元素的key值都不大于N,且key值都是非负整数。
计数排序算法Java实现
计数排序算法步骤大概有三个步骤:建一个长度为K+1的的数组C,里面的每一个元素初始都置为0(Java里面默认就是0)。
遍历待排序的数组,计算其中的每一个元素出现的次数,比如一个key为i的元素出现了3次,那么C[i]=3。
累加C数组,获得元素的排位,从0开始遍历C, C[i+1]=C[i]+C[i-1]
建一个临时数组T,长度与待排序数组一样。从数组末尾遍历待排序数组,把元素都安排到T里面,直接从C里面就可以得到元素的具体位置, 不过记得每处理过一个元素之后都要把C里面对应位置的计数减1。
Java实现如下:
package sort.algorithm.count; /** * 计数排序算法步骤大概有三个步骤: * 建一个长度为K+1的的数组C,里面的每一个元素初始都置为0(Java里面默认就是0)。 * 遍历待排序的数组,计算其中的每一个元素出现的次数,比如一个key为i的元素出现了3次,那么C[i]=3。 * 累加C数组,获得元素的排位,从0开始遍历C, C[i+1]=C[i]+C[i-1] * 建一个临时数组T,长度与待排序数组一样。从数组末尾遍历待排序数组,把元素都安排到T里面,直接从C里面就可以得到元素的具体位置, 不过记得每处理过一个元素之后都要把C里面对应位置的计数减1。 */ public class CountSort { public static void countSort(int[] array, int range) throws Exception { if (range < 0) throw new Exception("range can't be negative or zero."); if (array.length <= 1) return; int[] countArray = new int[range+1]; for (int i = 0; i < array.length; i++) { int value = array[i]; if (value < 0 || value > range) { throw new Exception("array element overflow range."); } countArray[value] += 1; } for (int i = 1; i < countArray.length; i++) { countArray[i] = countArray[i-1]+countArray[i]; } int[] temp = new int[array.length]; for (int i = array.length-1; i >= 0; i-- ) { int value = array[i]; int position = countArray[value] - 1; temp[position] = value; countArray[value] -= 1; } for (int i = 0; i < array.length; i++) { array[i] = temp[i]; } } public static void main(String[] arg) throws Exception { int[] array = { 9, 23, 12, 6, 15, 4, 33, 2, 16, 1, 0 }; System.out.println("Before sort:"); for (int temp : array) System.out.print(temp+++","); countSort(array, 33); System.out.println(); System.out.println("After sort:"); for (int temp : array) System.out.print(temp+++","); } }
测试结果:
Before sort: 9,23,12,6,15,4,33,2,16,1,0, After sort: 0,1,2,4,6,9,12,15,16,23,33,
相关文章推荐
- 我们敬重你,java之父
- node.js面试题大全-侧重后端应用与对Node核心的理解
- 数据结构面试题1.2.8-判断整数序列是不是二元查找树的后序遍历结果
- poj--2406
- fragment 事务回滚 ---动态创建fragment
- U盾原理研究
- (转载)Sumblime Text 2 常用插件以及安装方法
- HTML5学习之工具使用
- Jesus Is Here(2015年吉林网络赛)
- 2016乐视校园招聘笔试题回忆版
- 【Android小游戏】安卓小游戏开发(直播手把手系列二)
- 笔试题_switch语句里default位置与执行问题
- wamp Could not execute menu item和 HTTP Error 404. The requested resource is not found.
- Android核心基础-10.通知
- 打开Voice Over时,CATextLayer的string对象兼容NSString和NSAttributedString导致的Crash(二解决思路3)
- hdu5455 Fang Fang(沈阳网赛)
- int *p()与int (*p)()的区别
- 内存泄漏,内存溢出
- spring4整合hibernate5报错解决
- 使用自定义Session监听器实现自定义Session管理