调整数组顺序使奇数位于偶数前面

void ReorderOldEven(int* arr, unsigned int length)
{
	if (arr == nullptr || length == 0)
		return;

	int* pBegin = arr;
	int* pEnd = arr + length - 1;
	while (pBegin < pEnd)
	{
		while (pBegin < pEnd && (*pBegin & 0x1) != 0)
			pBegin++;

		while (pBegin < pEnd && (*pEnd & 0x1) == 0)
			pEnd--;

		if (pBegin < pEnd)
		{
			int nTemp = *pBegin;
			*pBegin = *pEnd;
			*pEnd = nTemp;
		}
	}
}

  可将判断奇偶处的代码封装成函数,更方便扩展!

posted on 2021-02-16 15:18  Noora&w  阅读(61)  评论(0编辑  收藏  举报