排序算法 - 基数排序(C++)

/*
 * Get the spcific digit of given number.
 * For example, number 234, 
 *	the 0st digit is 4, 
 *	the 1st digit is 3,
 * 	the 2nd digit is 2,
 *	the 3th digit is 0.
 */
int GetNDigit(int nNumber, int nIdx)
{
	for (int i = nIdx; i > 0; i--) {
		nNumber /= 10;
	}
	return nNumber % 10;
}

/*
 * Counting Sort the given array according to specific digit.
 * array: 待排序数组.
 * nLength: 待排序数组长度
 * nIdxDigit: 排序要依据的位. 0为最低位,高位依次加1.
 * nK: *nIdxDigit位上可能出现的最大数字(对10进制数排序则nK=9).
 */
void CountingSort_SpecificDigit(int array[], size_t nLength, int nIdxDigit, int nK=9)
{
	if (NULL == array || 0 == nLength || 0 == nK)
		return;
	
	int *digitNum = new int[nLength];
	memset(digitNum, 0, sizeof(int)*nLength);
	int *count = new int[nK+1];
	memset(count, 0, sizeof(int)*(nK+1));
	int *arrayResult = new int[nLength];
	memset(arrayResult, 0, sizeof(int)*nLength);
	
	// 数组digitNum[],保存每个元素指定位上的数字
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值