计数排序(代码+注释)

#include <stdio.h>
#include <stdlib.h>

// 计数排序函数
void CountingSort(int* a, int n) {
    if (n <= 1) return; // 数组长度不足时直接返回

    // 找到数组中的最大值和最小值
    int max = a[0], min = a[0];
    for (int i = 1; i < n; i++) {
        if (a[i] > max) max = a[i];
        if (a[i] < min) min = a[i];
    }

    // 创建计数数组并初始化
    int range = max - min + 1; // 元素范围
    int* count = (int*)calloc(range, sizeof(int));
    if (count == NULL) {
        perror("Memory allocation failed");
        return;
    }

    // 统计每个元素的出现次数
    for (int i = 0; i < n; i++) {
        count[a[i] - min]++;
    }

    // 累积计数,用于计算每个元素的最终位置
    for (int i = 1; i < range; i++) {
        count[i] += count[i - 1];
    }

    // 创建目标数组
    int* output = (int*)malloc(n * sizeof(int));
    if (output == NULL) {
        perror("Memory allocation failed");
        free(count);
        return;
    }

    // 倒序遍历原数组,将元素放置到正确位置
    for (int i = n - 1; i >= 0; i--) {
        int index = count[a[i] - min] - 1;
        output[index] = a[i];
        count[a[i] - min]--;
    }

    // 将排序结果写回到原数组
    for (int i = 0; i < n; i++) {
        a[i] = output[i];
    }

    // 释放内存
    free(count);
    free(output);
}

int main() {
    int a[] = {4, 2, 2, 8, 3, 3, 1};
    int n = sizeof(a) / sizeof(a[0]);

    printf("Before sorting: ");
    for (int i = 0; i < n; i++) {
        printf("%d ", a[i]);
    }
    printf("\n");

    CountingSort(a, n);

    printf("After sorting: ");
    for (int i = 0; i < n; i++) {
        printf("%d ", a[i]);
    }
    printf("\n");

    return 0;
}

测试结果

详细可见排序学习整理(3)-优快云博客 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值