The running time of counting sort is O(n), and usually, the running time of sorting algorithms will be either O(n^2) or O(n lgn), the reason why counting sort is O(n) is that it doesn't have the comparison.In fact,
the sorting is hidden from line 19 to line 27.
public static int[] CountingSort(int[] A) {
int max = A[0];
for (int i = 1; i < A.length; i++) {
if (A[i] > max) {
max = A[i];
}
}
int[] C = new int[max];
int[] B = new int[A.length];
for (int i = 0; i < A.length; i++) {
C[A[i] - 1] += 1;
}
for (int i = 1; i < C.length; i++) {
C[i] += C[i - 1];
}
//array C record the position of A[i] in array B
for (int i = 0; i < A.length; i++) {
B[C[A[i] - 1] - 1] = A[i];
C[A[i] - 1]--;
}
for (int i = 0; i < A.length; i++) {
A[i] = B[i];
}
return A;
}
The code segment above is only suitable for the case in which all the elements in the array are non-negative. If the array
has negative element, we can simply 'shift' the element, such as the minimum element will be considered as 0. I use 'span'
to shift the element.
public static int[] CountingSort(int[] A) {
int max = A[0];
int min = A[0];
for (int i = 1; i < A.length; i++) {
if (A[i] > max) max = A[i];
if (A[i] < min) min = A[i];
}
int[] C = new int[max - min + 1];
int[] B = new int[A.length];
for (int i = 0; i < A.length; i++) {
C[A[i] - min] += 1;
}
for (int i = 1; i < C.length; i++) {
C[i] += C[i - 1];
}
//array C record the position of A[i] in array B
for (int i = 0; i < A.length; i++) {
B[C[A[i] - min] - 1] = A[i];
C[A[i] - min]--;
}
for (int i = 0; i < A.length; i++) {
A[i] = B[i];
}
return A;
}
分享到:
相关推荐
排序算法 排序算法_基于C语言实现的排序算法之CountingSort实现
PeopleCounting-master.rar
cycle_counting_4.m
density_based_methods_counting-master.zip
Function Point Counting Practices Manual 4.1.1 版本。 来自IFPUG,权威资料 The primary objectives of the IFPUG Counting Practices Manual, Release 4.1, are to • Provide a clear and detailed description...
CrowdCountingDataset是一个高密度人群图像数据,图片来自FLICKR网站。数据集由视频中帧的RGB图像(作为输入)和每帧计数的对象组成,这是图像中行人(对象)的数量。该数据集是用商场的网络摄像头记录的同一地点的3...
本文将介绍: a. 使用CMSIS API ,介绍FreeRTOS中计数信号量 b. 不使用CMSIS API,直接使用FreeRTOS函数 1. 简介 计数信号量可用于控制对资源的访问。要获得对资源的控制,任务必须首先获得信号量。...
simple_vehicle_counting_C++_master_vehicle_counting_源码
文件太大,不能放上来,下载后私信我给百度云盘链接
CountingSort为AlgorithmMan中的计数排序演示工具(这是可执行文件;需要.net 4.0支持;非源代码)。 原文:C#算法设计排序篇之08-计数排序(附带动画演示程序) 链接:...
对一维时间序列采用盒计数法对其进行多重分形谱的计算
检测视频中出现的人头数目,并统计来往的人数。
var countingSort = require ( './countingSort.js' ) ; // Construct input [0, 5], therefore an array of size 6 is needed // for the temporary storage space. var input = [ 2 , 5 , 3 , 0 , 2 , 3 , 0 , 3 ...
vhdl code for error counting blk in lms algorithm
一个统计代码行的很简单很适用的小工具-a statistical line of code is very simple and very applicable to the small tools
伪代码和算法介绍在此:http://www.algorithmist.com/index.php/Counting_sort,本程序是 根据这个伪代码编写的源代码
To reflect that, and to make the Counting Practices Manual (CPM) even more attractive as a reference manual, the Counting Practices Committee (CPC) decided to restructure CPM 4.2 into four parts: ...
To reflect that, and to make the Counting Practices Manual (CPM) even more attractive as a reference manual, the Counting Practices Committee (CPC) decided to restructure CPM 4.2 into four parts: ...
The Function Point Counting Practices Manual is the definitive description of the Function Pointing Counting Standard. Several versions of the manual are available, each describing the standard or ...