调整数组顺序使奇数位于偶数前面
作者:互联网
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; } } }
可将判断奇偶处的代码封装成函数,更方便扩展!
标签:arr,奇数,int,while,偶数,pBegin,length,pEnd,数组 来源: https://www.cnblogs.com/yapp/p/14406736.html