调整数组循序使奇数位于偶数前面
1 void ReorderOddEven(int *pData , unsigned int length) 2 { 3 if (!pData || length <= 0) 4 { 5 return; 6 } 7 int* pBegin = pData ; 8 int* pEnd = pData + length - 1 ; 9 while (pBegin < pEnd) 10 { 11 while( pBegin<pEnd && (((*pBegin) & 0x1) == 1) )//找到偶数位置 12 { 13 pBegin++; 14 } 15 while( pBegin<pEnd && ( ((*pEnd) & 0x1) != 1) )//找到寄数位置 16 { 17 pEnd--; 18 } 19 if (pBegin<pEnd) 20 { 21 int temp = *pBegin ; 22 *pBegin = *pEnd ; 23 *pEnd = temp ; 24 } 25 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步