⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 coutingsort.h

📁 比快速排序更快的排序算法;这个了示例包含多种数据结构的算法
💻 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 + -