调整数组顺序使奇数位于偶数前面
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; } } }
可将判断奇偶处的代码封装成函数,更方便扩展!