Problem Description:
Enter an array of integers to implement a function to adjust the order of numbers in the array,
Yes, all the odd digits are in the first half of the array, and all the even digits are in the second half.
Thinking Analysis:
Use two pointers, one to the array header, one to the end of the array, one to the other, the other for the even number in front,
One in the back to find the odd number, found after the interchange, two pointers meet the end.
Reference code:
void Recordoddeven (int *pdata,int nlength)
{
if ((PData = = NULL) | | (nlength = = 0))
{
return;
}
int *pbegin = PData;
int *pend = pdata+nlength-1;
while (Pbegin < pEnd)
{
while ((Pbegin < PEnd) && ((*pbegin &0x1)! = 0))
{
pbegin++;
}
while ((Pbegin < PEnd) && ((*pend &0x1) = = 0))
{
pend--;
}
if (Pbegin < pEnd)
{
int tmp = *pend;
*pend = *pbegin;
*pbegin = tmp;
}
}
cout<<*pbegin<< "" <<*pEnd<<endl;
}
Adjust the array order so that the odd digits are preceded by even numbers