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. Linux间的进程通信;以及子进程的创建

    "-----第六天-----------------------------------------------------------------------------" .版 ...

  2. 实战Nginx负载均衡高冗余高可用WEB架构

       最近公司主力网站之一改版完成终于上线了,牵扯了我大半年的时间,现在终于有时间坐下来写点东西,总结沉淀一下自己的技术心得.此次,根据服务器的数量和质量,我采用负载均衡高冗余的架构,考虑单点故障,W ...

  3. 【Android应用开发技术:用户界面】布局管理器

    作者:郭孝星 微博:郭孝星的新浪微博 邮箱:allenwells@163.com 博客:http://blog.csdn.net/allenwells Github:https://github.co ...

  4. jQuery 语法(一)

    通过 jQuery,您可以选取(查询,query) HTML 元素,并对它们执行“操作”(actions). jQuery 语法实例 $(this).hide() 演示 jQuery hide() 函 ...

  5. C++忽略字符大小写比较

    在项目中用到对两个字符串进行忽略大小写的比较,有两个方法实现 1.使用C++提供的忽略大小写比较函数实现 代码实现: /* 功能 :忽略大小写进行字符串比较 */ #ifdef __LINUX__ # ...

  6. Nginx+PHP-FPM优化技巧总结

    php-fpm的安装很简单,参见PHP(PHP-FPM)手动编译安装.下面主要讨论下如何提高Nginx+Php-fpm的性能.   1.Unix域Socket通信   之前简单介绍过Unix Doma ...

  7. python笔记7:mysql、redis操作

    模块安装: 数据操作用到的模块pymysql,需要通过pip install pymysql进行安装. redis操作用的模块是redis,需要通过pip install redis进行安装. 检验是 ...

  8. 【C++自我精讲】基础系列三 重载

    [C++自我精讲]基础系列三 重载 0 前言 分二部分:函数重载,操作符重载. 1 函数重载 函数重载:指在同一名字空间中,函数名称相同,参数类型.顺序或数量不同的一类函数,同一函数名的函数能完成不同 ...

  9. 2017 ACM区域赛(南宁站) 参赛流水账

    day0: 早上四点起床赶飞机,还好没有吵醒室友导致被打死.本来想在飞机上准备一下下周的小测,结果飞机一点都不平稳,只能全程和队友吹逼聊天.下午在宾馆里和johann通关了一部合金弹头,重温了童年的经 ...

  10. bash脚本IFS=',' read的意思

    IFS is the Input Field Separator, which means the string read will be split based on the characters ...