[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 ...
随机推荐
- android的五大布局(layout)
Android的界面是有布局和组件协同完成的,布局好比是建筑里的框架,而组件则相当于建 筑里的砖瓦.组件按照布局的要求依次排列,就组成了用户所看见的界面.Android的五大布局分别是LinearLa ...
- How to fix Cannot change version of project facet Dynamic Web Module to 3.0 Error in Eclipse---转载
How to fix Cannot change version of project facet Dynamic Web Module to 3.0 Error in Eclipse 原文:http ...
- Javascript 中使用Json的四种途径
1.jQuery插件支持的转换方式: 复制代码代码如下: $.parseJSON( jsonstr ); //jQuery.parseJSON(jsonstr),可以将json字符串转换成json对象 ...
- APP消息推送功能
1.APP内部最好设计-我的消息-的功能,以便用户查看推送消息历史记录,通过角标.已读.未读等设计吸引用户读取消息.(画下来这都是重点) 2.建议提供推送设置功能,允许用户设置推送消息是否显示于通知栏 ...
- Mac 常用的手势
可以在触屏版-更多手势查看 技巧:前期慢慢滑动练习.一定要慢慢滑动,这样可以清楚的看出有没有产生效果,尤其注意大拇指滑动的感觉. 回到桌面:四指向外 打开Lanuchpad:四指向内 查看所有任务:三 ...
- 微信公众号弹出框在IOS最新系统中点击键盘上的“完成”导致事件无法触发问题
微信公众号弹出框在IOS最新系统中点击键盘上的"完成"导致事件无法触发问题 问题描述 微信公众号中有项功能是弹框模态框,输入信息后保存操作.但是在IOS系统中发现,当输入内容后,点 ...
- Android图片二级缓存
点击下载源代码 想起刚開始写代码的时候,领导叫我写一个头像下载的方法,当时屁颠屁颠就写了一个图片下载的,每次都要去网络上请求,最后直接被pass掉了 当时的思路是这种 后来渐渐地就知道了有二级缓存这东 ...
- 新标准C++程序设计读书笔记_运算符重载
形式 返回值类型 operator 运算符(形参表) { …… } 运算符重载 (1)运算符重载的实质是函数重载(2)可以重载为普通函数,也可以重载为成员函数 class Complex { publ ...
- shell编程sed笔记
源文件的内容 <modules> <module name="provider"> <!--发布模式--> <bds_mode/> ...
- SpringMVC返回Json,自定义Json中Date类型格式
http://www.cnblogs.com/jsczljh/p/3654636.html —————————————————————————————————————————————————————— ...