原文:wiki : http://en.wikipedia.org/wiki/Counting_sort
http://zh.wikipedia.org/wiki/%E8%AE%A1%E6%95%B0%E6%8E%92%E5%BA%8F
计数排序(Counting sort)是一种稳定的排序算法。计数排序使用一个额外的数组C,其中第i个元素是待排序数组A中值等于i的元素的个数。然后根据数组C来将A中的元素排到正确的位置。
计数排序的特征
当输入的元素是 n 个 0 到 k 之间的整数时,它的运行时间是 Θ(n + k)。计数排序不是比较排序,排序的速度快于任何比较排序算法。
由于用来计数的数组C的长度取决于待排序数组中数据的范围(等于待排序数组的最大值与最小值的差加上1),这使得计数排序对于数据范围很大的数组,需要大量时间和内存。例如:计数排序是用来排序0到100之间的数字的最好的算法,但是它不适合按字母顺序排序人名。但是,计数排序可以用在基数排序中的算法来排序数据范围很大的数组。
算法的步骤如下:
- 找出待排序的数组中最大和最小的元素
- 统计数组中每个值为i的元素出现的次数,存入数组C的第i项
- 对所有的计数累加(从C中的第一个元素开始,每一项和前一项相加) (记录小于它的元素个数,用来计算它在整体整体中的排名)
- 反向填充目标数组:将每个元素i放在新数组的第C(i)项,每放一个元素就将C(i)减去1
In pseudocode, this may be expressed as follows:
''' calculate histogram: ''' # allocate an array Count[0..k] ; THEN # initialize each array cell to zero ; THEN for each input item x: increment Count[key(x)] ''' calculate starting index for each key: ''' total = 0 for i = 0, 1, ... k: oldCount = Count[i] Count[i] = total total = total + oldCount ''' copy inputs into output array in order: ''' # allocate an output array Output[0..n-1] ; THEN for each input item x: Output[Count[key(x)]] = x decrement Count[key(x)] return Output
After the first for loop, Count[i]
stores the number of items with key equal to i
.After the second for loop, it instead stores the number of items with key less than i
, which is the same as the first index at which an item with keyi
should be stored in the output array. Throughout the third loop,Count[i]
always stores the next position in the output array into which an item with key i
should be stored, so each item is moved into its correct position in the output array. The relative order of items with equal keys is preserved here; i.e., this is a stable sort.
我写的代码: (最新修改 2013-12-12)
/*
* 计数排序: 稳定的排序
* huntinux
* 13-12-12
*
*/
#include <stdio.h>
#include <malloc.h>
int find_max(int *a, int n)
{
int i;
int max = a[0];
for(i = 1; i < n; i++){
if(a[i]>max)
max = a[i];
}
return max;
}
void counting_sort(int *a, int n)
{
int *count, *tmparr;
int i;
int size;
size = find_max(a,n); // 找到最大元素
size += 1; // 因为数据从0开始,所以要多申请一个。
size = size > n ? size : n; //元素个数和最大元素之间较大的作为计数数组的大小。
count = malloc(sizeof(int) * size );
if(!count)
perror("malloc");
for(i = 0; i < size; i++)
count[i] = 0;
for(i = 0; i < n; i++)
count[a[i]]++;
for(i = 1; i < size; i++)
count[i] += count[i-1];
tmparr = malloc(sizeof(int) * n);
if(!tmparr)
perror("malloc");
for(i = 0; i < n; i++)
tmparr[i] = 0;
for(i = n-1; i >= 0; i--) //这里让i从n-1开始,是保证在数字相等的情况下,默认靠前的数字要排在前面。(保证稳定排序)
tmparr[--count[a[i]]] = a[i];
for(i = 0; i < n; i++)
a[i] = tmparr[i];
free(count);
free(tmparr);
}
void print_arr(int *a, int n, char *msg)
{
int i;
if(msg)
printf("%s\n", msg);
for(i = 0; i < n; i++)
printf("%d\t", a[i]);
printf("\n");
}
int main()
{
int a[15] = {8, 4 , 2, 6, 3, 1, 9, 3, 5, 7, 1, 100, 0, 2, 5};
print_arr(a, 15, "before sorting:");
counting_sort(a, 15);
print_arr(a, 15, "after sorting:");
return 0;
}
注意下面的代码是保证稳定排序的关键:
for(i = n-1; i >= 0; i--) //这里让i从n-1开始,是保证在数字相等的情况下,默认靠前的数字要排在前面。(保证稳定排序)
tmparr[--count[a[i]]] = a[i];