[Algorithms] Counting Sort
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的更多相关文章
- [Algorithms] Radix Sort
Radix sort is another linear time sorting algorithm. It sorts (using another sorting subroutine) the ...
- 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 ...
- counting sort 计数排序
//counting sort 计数排序 //参考算法导论8.2节 #include<cstdio> #include<cstring> #include<algorit ...
- HDU 1718 Rank counting sort解法
本题是利用counting sort的思想去解题. 注意本题,好像利用直接排序,然后查找rank是会直接被判WA的.奇怪的推断系统. 由于分数值的范围是0到100,很小,而student 号码又很大, ...
- 41. First Missing Positive(困难, 用到 counting sort 方法)
Given an unsorted integer array, find the first missing positive integer. For example, Given [1,2,0] ...
- 《算法导论》——计数排序Counting Sort
今天贴出的算法是计数排序Counting Sort.在经过一番挣扎之前,我很纠结,今天这个算法在一些scenarios,并不是最优的算法.最坏情况和最好情况下,时间复杂度差距很大. 代码Countin ...
- 【HackerRank】 The Full Counting Sort
In this challenge you need to print the data that accompanies each integer in a list. In addition, i ...
- 排序算法六:计数排序(Counting sort)
前面介绍的几种排序算法,都是基于不同位置的元素比较,算法平均时间复杂度理论最好值是θ(nlgn). 今天介绍一种新的排序算法,计数排序(Counting sort),计数排序是一个非基于比较的线性时间 ...
- [MIT6.006] 7. Counting Sort, Radix Sort, Lower Bounds for Sorting 基数排序,基数排序,排序下界
在前6节课讲的排序方法(冒泡排序,归并排序,选择排序,插入排序,快速排序,堆排序,二分搜索树排序和AVL排序)都是属于对比模型(Comparison Model).对比模型的特点如下: 所有输入ite ...
随机推荐
- Objective-C之定义函数
Demo1.m 一个基础的函数定义 #import<Foundation/Foundation.h> //定义一个返回值为int类型的,名为max的函数.传入的参数为两个int型数据 in ...
- python selenum ---如何定位一组元素
使用findElements方法定位一组对象 定位一组对象一般用于以下场景: · 批量操作对象,比如将页面上所有的checkbox都勾上 · 先获取一组对象,再在这组对象中过滤出需要具体定位的一些对象 ...
- <<Python基础教程>>学习笔记 | 第11章 | 文件和素材
打开文件 open(name[mode[,buffing]) name: 是强制选项,模式和缓冲是可选的 #假设文件不在.会报以下错误: >>> f = open(r'D:\text ...
- css - 当文本内容长度超出屏幕宽度时,以省略号代替
<style> .ellipsis{ text-overflow: ellipsis; overflow: hidden; white-space: nowrap; } </styl ...
- Laravel 5 教程 - 文件上传
一.简介 Laravel 有很棒的文件系统抽象层,是基于 Frank de Jonge 的 Flysystem 扩展包. Laravel 集成的 Flysystem 提供了简单的接口,可以操作本地端空 ...
- POJ2185-Milking Grid(KMP,next数组的应用)
Milking Grid Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 6317 Accepted: 2648 Desc ...
- jquery的input:type=file实现文件上传
<!DOCTYPE html> <html> <head> <title>html5_2.html</title> <style> ...
- request.setCharacterEncoding()对通过method="GET"输入的参数无效
通过GET提交参数有2种 1.浏览器地址栏输入URL?parameter1=value1¶meter2=value2 2.<form method="get" ...
- jquery ui 与 easy ui同时引入 展示效果冲突的问题
jquery ui 由于在定位控件的时候跟easy UI 控件名相同,同时引入会导致冲突 如果需要两个都存在,可以去jquery ui下载定制版ui 脚本文件
- STM32F10x_RTC秒中断
Ⅰ.概述 RTC(Real Time Clock)是实时时钟的意思,它其实和TIM有点类似,也是利用计数的原理,选择RTC时钟源,再进行分频,到达计数的目的. 该文主要讲述关于RTC的秒中断功能,这个 ...