[Algorithms] Radix Sort
Radix sort is another linear time sorting algorithm. It sorts (using another sorting subroutine) the numbers from their least significant digits to most significant digits. To guarantee the correctness of radix sort, the sorting subroutine must be stable. Moreover, each digit falls in a fixed range. For example, if the numbers are decimal, then all digits fall in [0, 9]. So counting sort is usually used as the subroutine.
The code is as follows. For more on radix sort, please refer to Introduction to Algorithms, 3rd edition.
#include <iostream>
#include <vector>
#include <ctime>
#include <algorithm> using namespace std; int maximum(vector<int>& nums) {
int mx = nums[];
for (int i = ; i < (int)nums.size(); i++)
mx = max(mx, nums[i]);
return mx;
} void countingSort(vector<int>& nums, int sig) {
vector<int> counts(, );
for (int i = ; i < (int)nums.size(); i++)
counts[nums[i] / sig % ]++;
for (int i = ; i < ; i++)
counts[i] += counts[i - ];
vector<int> sorted(nums.size());
for (int i = nums.size() - ; i >= ; i--) {
sorted[counts[nums[i] / sig % ] - ] = nums[i];
counts[nums[i] / sig % ]--;
}
swap(nums, sorted);
} void radixSort(vector<int>& nums) {
int mx = maximum(nums);
for (int sig = ; mx / sig; sig *= )
countingSort(nums, sig);
} void radixSortTest(void) {
int len = ;
vector<int> nums(len);
srand((unsigned)time(NULL));
for (int i = ; i < (int)nums.size(); i++)
nums[i] = rand() % (len + );
vector<int> copy = nums;
radixSort(nums);
sort(copy.begin(), copy.end());
for (int i = ; i < (int)nums.size(); i++) {
if (nums[i] != copy[i]) {
printf("radixSort() test failed!\n");
return;
}
}
printf("radixSort() test passed!\n");
} int main(void) {
radixSortTest();
system("pause");
return ;
}
[Algorithms] Radix Sort的更多相关文章
- 基数排序(radix sort)
#include<iostream> #include<ctime> #include <stdio.h> #include<cstring> #inc ...
- 经典排序算法 - 基数排序Radix sort
经典排序算法 - 基数排序Radix sort 原理类似桶排序,这里总是须要10个桶,多次使用 首先以个位数的值进行装桶,即个位数为1则放入1号桶,为9则放入9号桶,临时忽视十位数 比如 待排序数组[ ...
- Radix Sort
为了完成二维数据快速分类,最先使用的是hash分类. 前几天我突然想,既然基数排序的时间复杂度也不高,而且可能比hash分类更稳定,所以不妨试一下. 在实现上我依次实现: 1.一维数组基数排序 基本解 ...
- 排序算法七:基数排序(Radix sort)
上一篇提到了计数排序,它在输入序列元素的取值范围较小时,表现不俗.但是,现实生活中不总是满足这个条件,比如最大整形数据可以达到231-1,这样就存在2个问题: 1)因为m的值很大,不再满足m=O(n) ...
- [MIT6.006] 7. Counting Sort, Radix Sort, Lower Bounds for Sorting 基数排序,基数排序,排序下界
在前6节课讲的排序方法(冒泡排序,归并排序,选择排序,插入排序,快速排序,堆排序,二分搜索树排序和AVL排序)都是属于对比模型(Comparison Model).对比模型的特点如下: 所有输入ite ...
- 基数排序(Radix Sort)
基数排序(Radix Sort) 第一趟:个位 收集: 第二趟:十位 第三趟:百位 3元组 基数排序--不是基于"比较"的排序算法 递增就是把收集的过程返过来 算法效率分析 需要r ...
- 【算法】基数排序(Radix Sort)(十)
基数排序(Radix Sort) 基数排序是按照低位先排序,然后收集:再按照高位排序,然后再收集:依次类推,直到最高位.有时候有些属性是有优先级顺序的,先按低优先级排序,再按高优先级排序.最后的次序就 ...
- [Algorithms] Topological Sort
Topological sort is an important application of DFS in directed acyclic graphs (DAG). For each edge ...
- [Algorithms] Sorting Algorithms (Insertion Sort, Bubble Sort, Merge Sort and Quicksort)
Recently I systematicall review some sorting algorithms, including insertion sort, bubble sort, merg ...
随机推荐
- emqtt-benchmark发送指令
- 计算机原理--cpu篇
简介 本文的目的是为了能够对特定的计算模型估算所需的CPU规格,个数. 这里主要介绍CPU的基本工作原理,指令集.(仅以X86体系结构的CPU为例 )
- 快速理解linux流编辑器sed命令
原创 杜亦舒性能与架构 之前介绍过 awk 命令,sed 命令同样是非常重要的文本处理工具,涉及到linux shell开发时,几乎是避不开这两大利器的 sed 是 stream editor 的简写 ...
- unity, change shader for me and all my children
public void changeShaderForMeAndAllMyChildren(){ Transform[] childrenTransformList = GetComponentsIn ...
- Catalog的种类
框架中的Catalog 在MEF框架中,包含了4种Catalog,所有的Catalog的是从System.ComponentModel.Composition.Primitives名称空间下的Comp ...
- [Java Web]Hibernate基础总结(四)
性能优化 在大数据量遍历时(比如查找消息敏感词),须要手动使用clear方法释放缓存中的数据,防止缓存中数据过多浪费内存. 1+N问题:将Fetch设为LAZY能够在须要时才发出sql语句,或者设置B ...
- 文件上传之 MultipartFile
利用MultipartFile(组件)实现文件上传 在java中上传文件似乎总有点麻烦,没.net那么简单,记得最开始的时候用smartUpload实现文件上传,最近在工作中使用spring的Mult ...
- Python 集合常用方法总结
数据类型:int/str/bool/list/dict/tuple/float/set (set类型天生去重) 一.集合的定义 s = set() #定义空集合 s = {'a','b','c' ...
- EM5-PE2B
1. vocabulary once adv. 一次,曾经 They cocktailed once every week. He once lived in shanghai. twice adv ...
- python学习之getpass模块
getpass模块提供两个功能: getpass.getpass(prompt ='Password:',stream = None) 提示用户输入密码而不回显. 使用字符串提示提示用 ...