[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 ...
随机推荐
- jboss部署web应用
http://liufei-fir.iteye.com/blog/759772初次部署jboss的web应用,把tomcat/weblogic下的工程移植到jboss上发布 一.修改JBOSS应用服务 ...
- python selenium --层级定位
转自:http://www.cnblogs.com/fnng/p/3193955.html 场景: 假如两个控件,他们长的一模样,还都叫“张三”,唯一的不同是一个在北京,一个在上海,那我们就可以通过, ...
- MS SQL 日常维护管理常用脚本
--[查看数据库服务器名称] --默认实例查询 SELECT @@SERVERNAME AS SERVERNAME; SELECT SERVERPROPERTY('servername') AS Se ...
- 为centos桌面增加在右键中打开终端
万万没有想到这只是安装一个程序的总是,而不是配置的问题.注意要用root身份才能安装软件 1. yum -y install nautilus-open-terminal 2. reboot
- JVM性能调优入门
1. 背景 虽然大多数应用程序使用JVM的默认设置就能很好地工作,仍然有不少应用程序需要对JVM进行额外的配置才能达到其期望的性能要求. 现在JVM为了满足各种应用的需要,为程序运行提供了大量的JVM ...
- atitit. it软件项目管理---自己的员工,雇佣军、援军,混合的员工 杂牌 人员管理架构
atitit. it软件项目管理---自己的员工,雇佣军.援军,混合的员工 杂牌 人员管理架构 1. 企业的正规军,雇佣军,杂牌划分 1 1.1. 企业的员工基本是雇佣而来 1 1.2. 全职员工与兼 ...
- 矩阵乘法C语言实现
/* 矩阵乘法C语言实现 Slyar 2009.3.20 */ #include <stdio.h> #include <stdlib.h> /* 给 int 类型定义 ...
- [Java Web]Hibernate基础总结(四)
性能优化 在大数据量遍历时(比如查找消息敏感词),须要手动使用clear方法释放缓存中的数据,防止缓存中数据过多浪费内存. 1+N问题:将Fetch设为LAZY能够在须要时才发出sql语句,或者设置B ...
- web ul li
<html> <head> <style type="text/css"> ul{float:right} ul li{float:left; ...
- 413. Reverse Integer【easy】
Reverse digits of an integer. Returns 0 when the reversed integer overflows (signed 32-bit integer). ...