您的位置:首页 > 其它

【排序算法】计数排序

2015-09-19 22:02 295 查看
转载来自:计数排序之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,
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: