【算法】基数排序

简介

基数排序(*Radix sort)是一种非比较排序算法(non-comparative sorting algorithm )。现代计算机的基数排序算法由 计数排序 算法的开发人哈罗德·H·西华德(Harold H. Seward)于1954年于麻省理工大学开发。

算法步骤

  1. 将待排序序列中的所有数视为同样的数位长度。
  2. 从最低位开始,依次按位进行一次计数排序。
  3. 从最低位排序一直到最高位排序完成以后,数列就变成一个有序序列。

计数排序可参考之前发布的【算法】计数排序

因为要计算负数,因此计数用的 数组 如下:

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
-9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9
  • [0 ~ 8] 用来表示负数 -9-1
  • [9] 用于表示 0
  • [10 ~ 18] 用来表示整数 19

举例有未排列序列如下:

26 33 -5 -2 15

从个位数开始排序:

26 33 -5 -12 15
6 3 -5 -2 5

计数数列则为:

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
1 1 1 1 1

个位数排序为:

-12 -5 33 15 26

从十位数开始排序:

-12 -05 33 15 26
-1 0 3 1 2

计数序列为:

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
1 1 1 1 1

十位数排序为:

-12 -5 15 26 33

完成排序

C语言实现

c 复制代码
// 获取序列中最大位数
unsigned _maxSizeOfItem(const int *array, const unsigned length) {
    int max = array[0];

    unsigned index = 1;
    unsigned number_1 = 0;
    unsigned number_2 = 0;

    while (index < length) {
        number_2 = array[index];

        if (max < 0) {
            number_1 = max * -1;
        } else {
            number_1 = max;
        }

        if (number_2 < 0) {
            number_2 *= -1;
        }

        if (number_2 > number_1) {
            max = array[index];
        }

        index += 1;
    }

    unsigned count = 0;
    while (max != 0) {
        max /= 10;
        count += 1;
    }

    return count;
}
c 复制代码
// 复制数组。
void _copyArray(int *from_arr, int *to_arr, const unsigned length) {
    for (unsigned index = 0; index < length; index++) {
        to_arr[index] = from_arr[index];
    }
}
c 复制代码
// 按位获取某个数对应的计数序列的索引值。
unsigned _getDigitByPlace(int num, const int place) {
    num /= place;
    num = num - num / 10 * 10;

    return num + 9;
}
c 复制代码
void radixSort(int *array, const unsigned length) {
    unsigned radixs[RADIXS_SIZE] = {0}; /* initialize array with 0. */
    unsigned radix = 0;
    int *tmp_array = calloc(length, sizeof(int));
    unsigned index = 0;
    unsigned size = _maxSizeOfItem(array, length);
    int place = 1;

    for (unsigned count = 0; count < size; count++) {
    	// 按位开始计数排序。
        for (index = 0; index < length; index++) {
            radix = _getDigitByPlace(array[index], place);

            radixs[radix] += 1;
        }

        for (index = 1; index < RADIXS_SIZE; index++) {
            radixs[index] = radixs[index] + radixs[index - 1];
        }

        for (index = 0; index < length; index++) {
            radix = _getDigitByPlace(array[length - index - 1], place);
            radixs[radix] -= 1;

            tmp_array[radixs[radix]] = array[length - index - 1];
        }

		// 将完成计数排序后的序列 复制回原数组。
        _copyArray(tmp_array, array, length);

		// 重置计数序列。
        for (index = 0; index < RADIXS_SIZE; index++) {
            radixs[index] = 0;
        }

		// 下一个位。
        place *= 10;
    }

    free(tmp_array);
}
相关推荐
水木流年追梦14 分钟前
大模型入门-大模型优化方法13- MTP 多 token 输出、DCA 双块注意力
人工智能·分布式·算法·正则表达式·prompt
数据皮皮侠20 分钟前
全国消协智慧 315 平台投诉信息数据库
大数据·人工智能·算法·百度·制造
8Qi828 分钟前
LeetCode 115 & 392:不同子序列 / 判断子序列
算法·leetcode·职场和发展·动态规划
小蒋学算法1 小时前
算法-乘法表中第K小的数-二分
数据结构·算法
智者知已应修善业1 小时前
【51单片机8个LED,已经使用了D1D2,怎么样在不动D1D2的前提下实现D6~D8的流水灯】2024-1-19
c++·经验分享·笔记·算法·51单片机
Evand J1 小时前
【MATLAB例程】自适应渐消扩展卡尔曼滤波(AFEKF)三维雷达目标跟踪|效果已调优,附下载链接和运行结果,代码直接运行即可
开发语言·算法·matlab·目标跟踪·卡尔曼滤波·自适应滤波·代码定制
插件开发2 小时前
矢量路径运算如何选GPU技术?——适用算法对比及OpenGL/Direct3D/CUDA选型指南
算法·3d
8Qi82 小时前
LeetCode 72:编辑距离(Edit Distance)—— 题解
算法·leetcode·职场和发展·动态规划
SoftLipaRZC2 小时前
顺序表的应用:通讯录项目与经典算法实战
算法
8Qi82 小时前
LeetCode 583. 两个字符串的删除操作
算法·leetcode·职场和发展·动态规划