Counting sort is a linear time sorting algorithm. It is used when all the numbers fall in a fixed range. Then it counts the appearances of each number and simply rewrites the original array. For a nice introduction to counting sort, please refer to Introduction to Alogrithms, 3rd edition. The following code is basically a translation of the pseudo code there.

 #include <iostream>
#include <vector>
#include <ctime> using namespace std; // Counting Sort an Array with each element in [0, upper]
void countingSort(vector<int>& nums, int upper) {
vector<int> counts(upper + , );
for (int i = ; i < (int)nums.size(); i++)
counts[nums[i]]++;
for (int i = ; i <= upper; i++)
counts[i] += counts[i - ];
vector<int> sorted(nums.size());
for (int i = (int)nums.size() - ; i >= ; i--) {
sorted[counts[nums[i]] - ] = nums[i];
counts[nums[i]]--;
}
swap(nums, sorted);
} void print(vector<int>& nums) {
for (int i = ; i < (int)nums.size(); i++)
printf("%d ", nums[i]);
printf("\n");
} void countingSortTest(int len, int upper) {
vector<int> nums(len);
srand((unsigned)time(NULL));
for (int i = ; i < len; i++)
nums[i] = rand() % (upper + );
print(nums);
countingSort(nums, upper);
print(nums);
} int main(void) {
countingSortTest(, );
system("pause");
return ;
}

If you run this program, you are expected to see (I run it on Microsoft Visual Studio Professional 2012):

                   

[Algorithms] Counting Sort的更多相关文章

  1. [Algorithms] Radix Sort

    Radix sort is another linear time sorting algorithm. It sorts (using another sorting subroutine) the ...

  2. find out the neighbouring max D_value by counting sort in stack

    #include <stdio.h> #include <malloc.h> #define MAX_STACK 10 ; // define the node of stac ...

  3. counting sort 计数排序

    //counting sort 计数排序 //参考算法导论8.2节 #include<cstdio> #include<cstring> #include<algorit ...

  4. HDU 1718 Rank counting sort解法

    本题是利用counting sort的思想去解题. 注意本题,好像利用直接排序,然后查找rank是会直接被判WA的.奇怪的推断系统. 由于分数值的范围是0到100,很小,而student 号码又很大, ...

  5. 41. First Missing Positive(困难, 用到 counting sort 方法)

    Given an unsorted integer array, find the first missing positive integer. For example, Given [1,2,0] ...

  6. 《算法导论》——计数排序Counting Sort

    今天贴出的算法是计数排序Counting Sort.在经过一番挣扎之前,我很纠结,今天这个算法在一些scenarios,并不是最优的算法.最坏情况和最好情况下,时间复杂度差距很大. 代码Countin ...

  7. 【HackerRank】 The Full Counting Sort

    In this challenge you need to print the data that accompanies each integer in a list. In addition, i ...

  8. 排序算法六:计数排序(Counting sort)

    前面介绍的几种排序算法,都是基于不同位置的元素比较,算法平均时间复杂度理论最好值是θ(nlgn). 今天介绍一种新的排序算法,计数排序(Counting sort),计数排序是一个非基于比较的线性时间 ...

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

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

随机推荐

  1. 将table中的值转换成json格式传到后台接收处理。

    table数据 <table style="border:1px" id="tableID"> <tr> <th>编号< ...

  2. swift向方法传数组参数的语法

    总是记不住向方法中传数组参数的语法,所以记录一下. func calculateStatistics(scores:[Int]) -> (min:Int,max:Int,sum:Int) { v ...

  3. 中小型研发团队架构实践:Redis快速入门及应用

    Redis的使用难吗?不难,Redis用好容易吗?不容易.Redis的使用虽然不难,但与业务结合的应用场景特别多.特别紧,用好并不容易.我们希望通过一篇文章及Demo,即可轻松.快速入门并学会应用. ...

  4. 将本地web项目发布到ubuntu上并运行 第一个本地的.net core2.0项目

    前置条件 ubuntu已安装dotnet 发布版本dotnet与发布机一致 这里用的是vm 所以直接把本地web项目拷贝到vm中运行的ubuntu系统中 web站点需要将 webapplication ...

  5. win8安装Visual C++ 2015 build tools闪退解决办法

    win8安装Visual C++ 2015 build tools闪退解决办法 安装Visual Studio 2015闪退问题也同样应用此解决办法. 1.控制面板——添加删除程序——启动关闭wind ...

  6. Navicat Premium快速导出数据库ER图和数据字典

    2.快速导出数据库数据字典: SQL Server 数据库,生成数据字典 use YourDatabase --指定要生成数据字典的数据库 go SELECT 表名= then d.name else ...

  7. UIView的使用setNeedsDisplay

    1,UIView的setNeedsDisplay和setNeedsLayout方法   首先两个方法都是异步运行的.而setNeedsDisplay会调用自己主动调用drawRect方法.这样能够拿到 ...

  8. 关于UI功能解锁,UI特效动画,UI tips的再思考

    之前写过一篇这样的文章,但当时的思路可行性太低 首先所有的UI面板通过发送字符串消息来告知,是否触发了解锁检测,tips检测,动画特效.可以理解为这样的接口: AsyncResult SendUIMe ...

  9. [svc][op]网站504无法访问问题处理

    504 Gateway Time-out 网站出现了这种情况 架构是:tomcat+nginx, 想通过domain.com 跳转到www.domain.com. 客户端访问流程 客户端--dns-- ...

  10. Myeclipse中误报错误解决办法

    下午写jsp页面的时候,用了一个js文件,拖到MyEclipse下了报错,开始还以为是js文件问题,折腾了半天,后来才知道原来是Myeclipse误报错误.真坑爹啊呀~~ 解决方法: 点击你需要忽略错 ...