注册 登录
编程论坛 数据结构与算法

一种计数排序算法的实现

烈烈水云天 发布于 2010-01-06 10:46, 751 次点击
#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]]--;
        }
}
0 回复
1