Title: Enter an array of integers to implement a function to adjust the order of the numbers in the array so that all the odd digits are in the first half of the array, even in the second half. The complexity of the time is as low as possible.
Analysis: We can use two pointers, the first pointer initializes the first digit of the array, it moves backwards, and the second pointer initializes to the last digit of the array, and it moves forward only. Before two pointers meet, the first pointer is always in front of the second pointer. If the number that the first pointer points to is even, and the second pointer is an odd point, we swap the two numbers. The implementation code is as follows:
Void reorderoddeven (int* pdata,unsigned int length) { if (PData==NULL || length==0) return; int *pBegin=pData; int *pEnd=pData+length-1; while (pbegin<pend) { while (pbegin<pend&& (*pbegin&0x1)!=0) pBegin++; while (pbegin<pend&& (*pbegin&0x1) ==0) pEnd--; if (pbegin<pend) { int temp=*pbegin; *pBegin=*pEnd; *pend=temp; } }}
This article from "Fairy Road thousands of dust Dream" blog, please be sure to keep this source http://secondscript.blog.51cto.com/9370042/1583092
Adjust the array order so that the odd digits are preceded by even numbers