[Algorithms] Counting Sort

简介: Counting sort is a linear time sorting algorithm. It is used when all the numbers fall in a fixed range.

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.

 1 #include <iostream>
 2 #include <vector>
 3 #include <ctime>
 4 
 5 using namespace std;
 6 
 7 // Counting Sort an Array with each element in [0, upper]
 8 void countingSort(vector<int>& nums, int upper) {
 9     vector<int> counts(upper + 1, 0);
10     for (int i = 0; i < (int)nums.size(); i++)
11         counts[nums[i]]++;
12     for (int i = 1; i <= upper; i++)
13         counts[i] += counts[i - 1];
14     vector<int> sorted(nums.size());
15     for (int i = (int)nums.size() - 1; i >= 0; i--) {
16         sorted[counts[nums[i]] - 1] = nums[i];
17         counts[nums[i]]--;
18     }
19     swap(nums, sorted);
20 }
21 
22 void print(vector<int>& nums) {
23     for (int i = 0; i < (int)nums.size(); i++)
24         printf("%d ", nums[i]);
25     printf("\n");
26 }
27 
28 void countingSortTest(int len, int upper) {
29     vector<int> nums(len);
30     srand((unsigned)time(NULL));
31     for (int i = 0; i < len; i++)
32         nums[i] = rand() % (upper + 1);
33     print(nums);
34     countingSort(nums, upper);
35     print(nums);
36 }
37 
38 int main(void) {
39     countingSortTest(20, 5);
40     system("pause");
41     return 0;
42 }

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

2 3 4 1 5 1 1 5 4 0 3 2 2 5 5 3 5 5 3 4
0 1 1 1 2 2 2 3 3 3 3 4 4 4 5 5 5 5 5 5
目录
相关文章
|
6月前
|
搜索推荐 算法 Java
sort-08-counting sort 计数排序
这是一个关于排序算法的系列文章摘要。文章详细介绍了多种排序算法,包括冒泡排序、快速排序、选择排序、堆排序、插入排序、希尔排序、归并排序、计数排序、桶排序以及大文件外部排序。计数排序是一种线性时间复杂度的稳定排序算法,由 Harold H. Seward 在1954年提出。基础版计数排序通过创建一个与最大元素等长的新数组来统计每个元素出现的次数,然后填充排序结果。改良版则考虑了空间浪费问题,通过找出最小值来减少数组长度。此外,还提出了使用 TreeMap 来扩展排序算法以适应非数字元素的情况。
|
存储 搜索推荐 算法
计数排序(Counting Sort)详解
计数排序(Counting Sort)是一种非比较排序算法,其核心思想是通过计数每个元素的出现次数来进行排序,适用于整数或有限范围内的非负整数排序。这个算法的特点是速度快且稳定,适用于某些特定场景。在本文中,我们将深入探讨计数排序的原理、步骤以及性能分析。
262 1
计数排序(Counting Sort)详解
|
算法 搜索推荐 数据库
一个有点咬文嚼字的 sorting 和 ordering
为什么排序算法的英文是 sorting 而不是 ordering。
138 0
Data Structures and Algorithms (English) - 6-13 Topological Sort(25 分)
Data Structures and Algorithms (English) - 6-13 Topological Sort(25 分)
109 0
PAT (Advanced Level) Practice - 1004 Counting Leaves(30 分)
PAT (Advanced Level) Practice - 1004 Counting Leaves(30 分)
109 0
Data Structures and Algorithms (English) - 6-10 Sort Three Distinct Keys(30 分)
Data Structures and Algorithms (English) - 6-10 Sort Three Distinct Keys(30 分)
103 0
|
C++
Data Structures and Algorithms (English) - 6-9 Sort Three Distinct Keys(20 分)
Data Structures and Algorithms (English) - 6-9 Sort Three Distinct Keys(20 分)
113 0
PAT (Advanced Level) Practice - 1098 Insertion or Heap Sort(25 分)
PAT (Advanced Level) Practice - 1098 Insertion or Heap Sort(25 分)
104 0
【1004】Counting Leaves (30 分)
【1004】Counting Leaves (30 分) 【1004】Counting Leaves (30 分)
94 0
|
SQL 移动开发 算法
New Dynamic Programming Algorithm for the Generation of Optimal Bushy Join Trees
MySQL无疑是现在开源关系型数据库系统的霸主,在DBEngine的最新排名中仍然稳居第2位,与第3位SQL Server的积分差距并不算小,可以说是最受欢迎,使用度最高的数据库系统,这一点看看有多少新型数据库要兼容MySQL的协议和语法就知道了。
323 0
New Dynamic Programming Algorithm for the Generation of Optimal Bushy Join Trees