📄 coutingsort.h
字号:
template
< class T >
void CoutingSort(T a[], int N, int& KCN, int& RMN)
{
int k = 0;
for ( int i = 0;i < N;i++ )
{
if ( ++KCN && a[i] > k )
{
k = a[i];
RMN++;
}
}
int *c = new int[k + 1];
int *b = new int[N];
for ( int j = 0;j <= k;j++ )
{
c[j] = 0;
}
//After this, in array c,c[i] is the times of i appears in array a
for ( i = 0;i < N;i++ )
{
c[ a[i] ]++;
}
//After this,in array c, c[i] is the times of n(n<=i) appears in array a
for ( i = 1;i <= k;i++ )
{
c[i] += c[ i - 1 ];
RMN++;
}
//this is the most powerful part of the algorithm
for ( i = N - 1;i >= 0;i-- )
{
b[ c[a[i]] - 1 ] = a[i];
RMN++;
c[ a[i] ]--;
}
for ( i = 0;i < N;i++ )
{
a[i] = b[i];
RMN++;
}
delete c;
c = NULL;
delete b;
b = NULL;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -