所有代码均通过G++编译器测试,仅为练手纪录。
//面试题30:最小的K个数
//题目:输入N个整数,找出其中最小的K个数。
// 例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4.
//面试题30:最小的K个数
//题目:输入N个整数,找出其中最小的K个数。
// 例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4.
void GetLeastNum(int *pInput,int nInSize,int *pOutput,int nOutSize)
{
if(NULL == pInput || nInSize <=0 || NULL == pOutput || nOutSize <= 0)
{
return;
}
int nBeg = 0;
int nEnd = nInSize - 1;
int index = NumPartition(pInput,nInSize,nBeg,nEnd);
while (index != nOutSize -1)
{
if(index > nOutSize -1)
{
nEnd = index - 1;
index = NumPartition(pInput, nInSize, nBeg, nEnd);
}
else
{
nBeg = index + 1;
index = NumPartition(pInput, nInSize, nBeg, nEnd);
}
}
for(index = 0; index < nOutSize; ++index)
{
pOutput[index] = pInput[index];
}
}
ZhaiPillary
2017-01-07