1. #include<iostream>
  2. #include<ctime>
  3. #include <stdio.h>
  4. #include<cstring>
  5. #include<cstdlib>
  6. #include <map>
  7. #include <string>
  8. using namespace std;
  9. // A utility function to get maximum value in arr[]
  10. int getMax(int arr[], int n)
  11. {
  12. ];
  13. ; i < n; i++)
  14. if (arr[i] > mx)
  15. mx = arr[i];
  16. return mx;
  17. }
  18.  
  19. // A function to do counting sort of arr[] according to
  20. // the digit represented by exp.
  21. void countSort(int arr[], int n, int exp)
  22. {
  23. int output[n]; // output array
  24. ] = {};
  25.  
  26. // Store count of occurrences in count[]
  27. ; i < n; i++)
  28. count[ (arr[i]/exp)% ]++;
  29.  
  30. // Change count[i] so that count[i] now contains actual position of
  31. // this digit in output[]
  32. ; i < ; i++)
  33. count[i] += count[i-];
  34.  
  35. // Build the output array
  36. ; i >= ; i--) {
  37. output[count[(arr[i]/exp)%]-] = arr[i];
  38. count[(arr[i]/exp)%]--;
  39. }
  40.  
  41. // Copy the output array to arr[], so that arr[] now
  42. // contains sorted numbers according to curent digit
  43. ; i < n; i++)
  44. arr[i] = output[i];
  45. }
  46.  
  47. // The main function to that sorts arr[] of size n using Radix Sort
  48. void radixsort(int arr[], int n)
  49. {
  50. // Find the maximum number to know number of digits
  51. int m = getMax(arr, n);
  52.  
  53. // Do counting sort for every digit. Note that instead of passing digit
  54. // number, exp is passed. exp is 10^i where i is current digit number
  55. ; m/exp > ; exp *= )
  56. countSort(arr, n, exp);
  57. }
  58.  
  59. // A utility function to print an array
  60. void print(int arr[], int n)
  61. {
  62. ; i < n; i++)
  63. cout << arr[i] << " ";
  64. }
  65. #if TEST
  66. int main(){
  67. , , , , , , , };
  68. ]);
  69. radixsort(arr, n);
  70. print(arr, n);
  71. }
  72. #endif

基数排序(radix sort)的更多相关文章

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

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

  2. 学习算法-基数排序(radix sort)卡片分类(card sort) C++数组实现

    基数排序称为卡片分类,这是一个比较早的时间越多,排名方法. 现代计算机出现之前,它已被用于排序老式打孔卡. 说下基数排序的思想.前面我有写一个桶式排序,基数排序的思想是桶式排序的推广. 桶式排序:ht ...

  3. 桶排序/基数排序(Radix Sort)

    说基数排序之前,我们先说桶排序: 基本思想:是将阵列分到有限数量的桶子里.每个桶子再个别排序(有可能再使用别的排序算法或是以递回方式继续使用桶排序进行排序).桶排序是鸽巢排序的一种归纳结果.当要被排序 ...

  4. 小小c#算法题 - 9 - 基数排序 (Radix Sort)

    基数排序和前几篇博客中写到的排序方法完全不同.前面几种排序方法主要是通过关键字间的比较和移动记录这两种操作来实现排序的,而实现基数排序不需要进行记录项间的比较.而是把关键字按一定规则分布在不同的区域, ...

  5. [转] 经典排序算法 - 基数排序Radix sort

    原理类似桶排序,这里总是需要10个桶,多次使用 首先以个位数的值进行装桶,即个位数为1则放入1号桶,为9则放入9号桶,暂时忽视十位数 例如 待排序数组[62,14,59,88,16]简单点五个数字 分 ...

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

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

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

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

  8. 基数排序(Radix Sort)

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

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

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

随机推荐

  1. this的使用

    1.使用this调用本类中的属性 class Person{ private String name; private int age; public Person(String name,int a ...

  2. eclipse下载

    http://www.eclipse.org/downloads/eclipse-packages

  3. js中的全选,不选,和反选按钮的设定

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. linux 搭建SVN服务器,为多个项目分别建立版本库并单独配置权限

    1.安装svn服务    # yum install subversion   2.新建一个目录用于存储SVN所有文件    # mkdir /home/svn   3.在上面创建的文件夹中为项目 p ...

  5. seajs+spm之再研究

    好久没有用seajs了,之前对spm也只是一知半解,这些天再次拿起来研究.谈谈我的认识与理解. 声明:本文不适合对seajs完全不了解的同学阅读.对于想知道seajs来龙去脉以及spm相关的同学&qu ...

  6. Mate7微信指纹支付来了 比Touch ID整合微信早一点

    之前我们聊过微信将推指纹支付 "指付通"会与Touch ID整合吗这个话题,现在有国内厂商率先支持微信指纹支付,体验一下美国用户使用Apple Pay搭配Touch ID来实现便捷 ...

  7. java笔记--策略模式和简单工厂模式

    策略模式: --如果朋友您想转载本文章请注明转载地址"http://www.cnblogs.com/XHJT/p/3884781.html "谢谢-- 为什么使用:策略模式主要用于 ...

  8. 获取并设置ListView高度的方法

    01 public void setListViewHeightBasedOnChildren(ListView listView) {  02     ListAdapter listAdapter ...

  9. Pascal’s Triangle

    vector<vector<int>> generate(int num) { vector<vector<int>> result; vector&l ...

  10. Continuous Subarray Sum

    Given an integer array, find a continuous subarray where the sum of numbers is the biggest. Your cod ...