您的位置:首页 > 其它

计数排序 (Counting Sort)

2013-06-18 18:39 316 查看
原文: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 eachinputitem x:
increment Count[key(x)]

''' calculate startingindex for each key: '''
total = 0
fori = 0, 1, ... k:
oldCount = Count[i]
Count[i] = total
total = total + oldCount

''' copyinputsinto output arrayin order: '''
# allocate an output array Output[0..n-1] ; THEN
for eachinputitem x:
Output[Count[key(x)]] = x
decrement Count[key(x)]
return Output


After the first for loop,
Count[i]
stores the number ofitems with key equal to
i
.After the second for loop,itinstead stores the number ofitems with key less than
i
, whichis the same
as the firstindex at which anitem with key
i
should be storedin the output array. Throughout the third loop,
Count[i]
always stores the next positionin the output arrayinto which anitem with key
i
should be
stored, so eachitemis movedintoits correct positionin the output array.
The relative order ofitems with equal keysis preserved here;i.e., thisis a stable sort.

我写的代码: (最新修改 2013-12-12)

/*
* 计数排序: 稳定的排序
* huntinux
* 13-12-12
*
*/

#include <stdio.h>
#include <malloc.h>

int find_max(int *a,int n)
{
inti;
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;
inti;
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)
{
inti;

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];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: