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的更多相关文章

  1. 基数排序(radix sort)

    #include<iostream> #include<ctime> #include <stdio.h> #include<cstring> #inc ...

  2. 经典排序算法 - 基数排序Radix sort

    经典排序算法 - 基数排序Radix sort 原理类似桶排序,这里总是须要10个桶,多次使用 首先以个位数的值进行装桶,即个位数为1则放入1号桶,为9则放入9号桶,临时忽视十位数 比如 待排序数组[ ...

  3. Radix Sort

    为了完成二维数据快速分类,最先使用的是hash分类. 前几天我突然想,既然基数排序的时间复杂度也不高,而且可能比hash分类更稳定,所以不妨试一下. 在实现上我依次实现: 1.一维数组基数排序 基本解 ...

  4. 排序算法七:基数排序(Radix sort)

    上一篇提到了计数排序,它在输入序列元素的取值范围较小时,表现不俗.但是,现实生活中不总是满足这个条件,比如最大整形数据可以达到231-1,这样就存在2个问题: 1)因为m的值很大,不再满足m=O(n) ...

  5. [MIT6.006] 7. Counting Sort, Radix Sort, Lower Bounds for Sorting 基数排序,基数排序,排序下界

    在前6节课讲的排序方法(冒泡排序,归并排序,选择排序,插入排序,快速排序,堆排序,二分搜索树排序和AVL排序)都是属于对比模型(Comparison Model).对比模型的特点如下: 所有输入ite ...

  6. 基数排序(Radix Sort)

    基数排序(Radix Sort) 第一趟:个位 收集: 第二趟:十位 第三趟:百位 3元组 基数排序--不是基于"比较"的排序算法 递增就是把收集的过程返过来 算法效率分析 需要r ...

  7. 【算法】基数排序(Radix Sort)(十)

    基数排序(Radix Sort) 基数排序是按照低位先排序,然后收集:再按照高位排序,然后再收集:依次类推,直到最高位.有时候有些属性是有优先级顺序的,先按低优先级排序,再按高优先级排序.最后的次序就 ...

  8. [Algorithms] Topological Sort

    Topological sort is an important application of DFS in directed acyclic graphs (DAG). For each edge ...

  9. [Algorithms] Sorting Algorithms (Insertion Sort, Bubble Sort, Merge Sort and Quicksort)

    Recently I systematicall review some sorting algorithms, including insertion sort, bubble sort, merg ...

随机推荐

  1. tomcat下运行war包

    例如你下的包名test.war直接放入webapps目录下,到bin目录下双击启动startup.bat,不要关掉tomcat控制台窗口,回到webapps下应该要看到自动解压出一个test的目录,如 ...

  2. 【Java集合源代码剖析】TreeMap源代码剖析

    转载请注明出处:http://blog.csdn.net/ns_code/article/details/36421085 前言 本文不打算延续前几篇的风格(对全部的源代码加入凝视),由于要理解透Tr ...

  3. 解决WinForm界面闪烁问题

    前言 之前將.net 1.1 Windows Form程式升級到.net 4.0,結果在開畫面時,閃的非常利害!  於是就開始找解決方法. 研究及解決 開始找到了設定DoubleBuffer=true ...

  4. scut协议配置工具初始化的一些问题

    1.如果点击协议配置工具左上角的项目按钮没反应,说明数据库没有正确配置. 2.数据库创建脚本运行如果乱码,把utf-8改成ansi 3.如果ContractDB不存在就自行创建数据库. 4.使用sql ...

  5. [gj]狮子经典语录

  6. JavaSE中日期处理

    一.java中日期类之间的关系: 其中: 1):java.sql.Date.java.sql.Time和java.sql.Timestamp这三个类是专门和数据库打交道的,它们都是java.util. ...

  7. HTML5 多图上传

    HTML5 多图上传 时间 2014-06-05 16:06:29  月小升博客 原文  http://java-er.com/blog/html5-many-image-upload/ 主题 HTM ...

  8. 0051 MyBatis关联映射--多对多关系

    用户与订单时一对多关系,再加上商品信息的话,订单与商品之间就是多对多关系了 DROP DATABASE IF EXISTS testdb; USE testdb; /*用户表,记录用户信息:用户与订单 ...

  9. 制作Cubie版OpenWRT(功能齐全,大小仅有11M)

    Allwinner Sun4i/5i/6i/7i (sunxi) Various vendors are offering development boards / single-board comp ...

  10. java.lang.NoSuchMethodError: org.apache.spark.util.ThreadUtils$.newDae

    -classpath "C:\Program Files\Java\jdk1.8.0_131\jre\lib\charsets.jar;C:\Program Files\Java\jdk1. ...