一. 算法描述

  基数排序(以整形为例),将整形10进制按每位拆分,然后从低位到高位依次比较各个位。主要分为三个过程:
  1. 分配,先从个位开始,根据位值(0-9)分别放到0~9号桶中(比如53,个位为3,则放入3号桶中)
  2. 收集,再将放置在0~9号桶中的数据按顺序放到数组中
  3. 重复(1)(2)过程,从个位到最高位(比如32位无符号整形最大数4294967296,最高位10位)
  下面以【521 310 72 373 15 546 385 856 187 147】序列为例,具体细节如下图所示:

二. 算法实现

#include<stdio.h>
#define MAX 20
#define SHOWPASS
#define BASE 10 // 打印数组
void print(int *a, int n) {
int i;
for (i = ; i < n; i++) {
printf("%d\t", a[i]);
}
} // 基数排序
void radixsort(int *a, int n) {
int i, b[MAX], m = a[], exp = ; // 得到数组的最大值
for (i = ; i < n; i++) {
if (a[i] > m) {
m = a[i];
}
} while (m / exp > ) {
int bucket[BASE] = { }; for (i = ; i < n; i++) {
bucket[(a[i] / exp) % BASE]++;
} for (i = ; i < BASE; i++) {
bucket[i] += bucket[i - ];
} for (i = n - ; i >= ; i--) {
b[--bucket[(a[i] / exp) % BASE]] = a[i];
} for (i = ; i < n; i++) {
a[i] = b[i];
} exp *= BASE; #ifdef SHOWPASS
printf("\nPASS : ");
print(a, n);
#endif
}
} int main() {
int arr[MAX];
int i, n;
printf("请输入总元素数目 (n <= %d) : ", MAX);
scanf("%d", &n);
n = n < MAX ? n : MAX; printf("请输入 %d 个元素 : \n", n);
for (i = ; i < n; i++) {
scanf("%d", &arr[i]);
} printf("\n排序前 : ");
print(&arr[], n); radixsort(&arr[], n); printf("\n排序之后 : ");
print(&arr[], n);
printf("\n"); return ;
}

三. 算法分析

  • 平均时间复杂度:O(dn)(d即表示整形的最高位数)
  • 空间复杂度:O(rn) (r表示0~9,用于存储临时的序列)
  • 稳定性:稳定

参考资料

  [1] http://blog.csdn.net/cjf_iceking/article/details/7943609

  [2] http://zh.wikipedia.org/wiki/%E5%9F%BA%E6%95%B0%E6%8E%92%E5%BA%8F

  [3] http://baike.baidu.com/view/1617233.htm?fr=aladdin

【Algorithm】基数排序的更多相关文章

  1. 【HNOI 2018】寻宝游戏

    Problem Description 某大学每年都会有一次 \(Mystery\ Hunt\) 的活动,玩家需要根据设置的线索解谜,找到宝藏的位置,前一年获胜的队伍可以获得这一年出题的机会. 作为新 ...

  2. Algorithm in Practice - Sorting and Searching

    Algorithm in Practice Author: Zhong-Liang Xiang Date: Aug. 1st, 2017 不完整, 部分排序和查询算法, 需添加. Prerequisi ...

  3. BZOJ.5285.[AHOI/HNOI2018]寻宝游戏(思路 按位计算 基数排序..)

    BZOJ LOJ 洛谷 话说vae去年的专辑就叫寻宝游戏诶 只有我去搜Mystery Hunt和infinite corridor了吗... 同样按位考虑,假设\(m=1\). 我们要在一堆\(01\ ...

  4. [Algorithm] Warm-up puzzles

    闲下来后,需要讲最近涉及到的算法全部整理一下,有个indice,方便记忆宫殿的查找 MIT的算法课,地球上最好:https://ocw.mit.edu/courses/electrical-engin ...

  5. 小白初识 - 基数排序(RadixSort)

    基数排序算是桶排序和计数排序的衍生吧,因为基数排序里面会用到这两种其中一种. 基数排序针对的待排序元素是要有高低位之分的,比如单词adobe,activiti,activiti就高于adobe,这个是 ...

  6. 基数排序——尚未补完的坑QAQ

    基数排序复杂度是(n+b)logn/logb 我们找一个基数 每次处理一部分位 从低位到高位处理 t是出现次数 s是这个桶管辖的起点 然后就可以写了 不过我这里是指针版的 有点难看 #include& ...

  7. 洛谷【P1177】【模板】基数排序

    题目传送门:https://www.luogu.org/problemnew/show/P1177 我对计数排序的理解:https://www.cnblogs.com/AKMer/p/9649032. ...

  8. POJ 2388 基数排序

    这题可以直接nth_element过去 比如这样子 //By SiriusRen #include <cstdio> #include <algorithm> using na ...

  9. [Algorithm] 面试题之犄角旮旯 第贰章

    闲下来后,需要讲最近涉及到的算法全部整理一下,有个indice,方便记忆宫殿的查找 MIT的算法课,地球上最好: Design and Analysis of Algorithms 本篇需要重新整理, ...

随机推荐

  1. jQuery对象

    $(document).ready(function(){ //第二种获取方法,通过标签的名<h2>Dom来获取 var h1 = document.getElementsByTagNam ...

  2. yii源码一 -- CComponent

    CComponent: path:framework/base/CComponent.php overview:This file contains the foundation classes fo ...

  3. Mybatis源码分析之SqlSessionFactory(一)

    简介 MyBatis的前身叫iBatis,本是apache的一个开源项目, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBa ...

  4. thinkphp3返回json或jsonp数据

    1.返回json数据 public function demo1() { $data = 'ok'; $this->ajaxReturn($data); } public function de ...

  5. JavaScript 时间、格式、转换及Date对象总结

    悲剧的遇到问题,从前台得到时间,“Tue Jan 29 16:13:11 UTC+0800 2008”这种格式的,想再后台解析成想要的格式,但是在后台就是解析不了SimpleDateFormat也试着 ...

  6. 调用OpenCVSharp进行拍照

    一.核心代码: using OpenCvSharp; using System; using System.Collections.Generic; using System.IO; using Sy ...

  7. Go语言中使用SQLite数据库

    Go语言中使用SQLite数据库 1.驱动 Go支持sqlite的驱动也比较多,但是好多都是不支持database/sql接口的 https://github.com/mattn/go-sqlite3 ...

  8. angular中的 登录检查 和 过期Session清理

    angular利用ui-router进行登录检查 SAP都会有这个问题,session过期或者页面被刷新的情况下应该进入登录页. 监听ui-router的satte事件可以实现当state切换的时候检 ...

  9. 在hadoop上运行java文件

    hadoop 2.x版本 编译:javac -d . -classpath /usr/lib/hadoop/hadoop-common-2.2.0.2.0.6.0-102.jar TestGetPat ...

  10. 远程IPC种植木马

    要实现代码例如以下: ///////////////////////////////////////////////////////////////////////////////////// typ ...