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. crc16 校验算法 C#

    封装一个静态类 using System; using System.Collections.Generic; using System.Linq; using System.Text; using ...

  2. ParameterizedThreadStart,ThreadStart的使用,线程Thread传参数

    Thread threadWithParam = new Thread(new ParameterizedThreadStart(new ThreadTest().ShowMsg));//thread ...

  3. 基于maven的ssh框架一步一步搭建(一)

    一.新建maven项目,配置ssh框架pom的最低支持 1.新建一个maven项目 2.添加一个web.xml ? 1 2 3 4 5 6 7 8 9 <?xml version="1 ...

  4. ListView局部更新(非notifyDataSetChanged)

    package com.example.test; import java.util.ArrayList; import java.util.List; import android.app.Acti ...

  5. 聊一聊Android的消息机制

    聊一聊Android的消息机制 侯 亮 1概述 在Android平台上,主要用到两种通信机制,即Binder机制和消息机制,前者用于跨进程通信,后者用于进程内部通信. 从技术实现上来说,消息机制还是比 ...

  6. 河南省第七届ACM程序设计大赛赛后总结

    我们学校ACM集训开始于4月5号(清明节),5月25日在郑州解放军信息工程大学举办,集训耗时50天,但是感觉效果还行,但是也不是太好:我们也已经尽力了,虽然说只拿了个银牌,每份收获的背后,都会有辛勤的 ...

  7. Java - web.xml文件中可以配置哪些内容?

    web.xml用于配置Web应用的相关信息,如:监听器(listener).过滤器(filter).Servlet.相关参数.会话超时时间.安全验证方式.错误页面等,下面是一些开发中常见的配置: ①配 ...

  8. 最大子矩阵 hdu1081

    To The Max Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  9. spring-core依赖jar包

  10. 在Linux下搭建git服务器

    http://www.cnblogs.com/dee0912/p/5815267.html 步骤很详细,很受用