标题:一种计数排序算法的实现
只看楼主
烈烈水云天
Rank: 2
来 自:湖南
等 级:论坛游民
帖 子:56
专家分:33
注 册:2009-12-30
结帖率:100%
 问题点数:0 回复次数:0 
一种计数排序算法的实现
#include <stdio.h>
int array[10] = {10, 25, 13, 13, 6, 12, 28, 15, 10, 22};
void counting_sort(int result[], int position[], int len, int max);
int main(int argc, char **argv)
{
        int i = 0;
        int result[10] = {0};
        int position[30] = {0};
       counting_sort(result, position, 10, 30);
        for(i = 0; i < 10; i++)
{
                printf("%d ", result[i]);
        }
        printf("\n");
        return 0;
}
void counting_sort(int result[], int position[], int len, int max)
{
        int i = 0;
        for(i = 0; i < len; i++)
{
                 position[array[i]] += 1;
        }
        for(i = 1; i < max; i++){
                 position[i] += position[i-1];
        }
        for(i = 0; i < len; i++)
{
                 result[position[array[i]]-1] = array[i];
                 position[array[i]]--;
        }
}
搜索更多相关主题的帖子: 计数 算法 
2010-01-06 10:46



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-295110-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019110 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved