Adjust the order of the arrays so that the odd digits are in the even front

Source: Internet
Author: User

Adjust the order of the arrays so that the odd digits are in the even front

Enter an array of integers to implement a function to adjust the order of the words in the array so that all the odd digits are in the first half of the array, and all the even digits are in the second half of the array.

Can be shipped with two pointers, the first pointer is initialized when it points to the first number of the array, it points back to move. The second pointer initializes with the last digit of the array, it moves forward only, and before two pointers meet, the first pointer is always in front of the second pointer.

(1) Moves backwards when the first pointer points to an odd number, and moves forward when the second pointer points to an even number;

(2) Conversely, the exchange of these two numbers;

#define _crt_secure_no_warnings 1

#include <stdio.h>  

#include <assert.h

void Span style= "font-family: ' The new song Body '; Font-size:20px;background:rgb (255,255,255);" > reorderarray ( int  * pdata int &NBSP; length

{

int *pstart= pdata ;

int *pend= pdata + length -1;

int temp;

assert ( pdata

if (*pdata= =NULL| | length ==0)

{

return ;

}

while (pstart<pend)

{

Move Pstart backwards until an even number is encountered

while(pstart<pend&& (*pstart&1)!=0)

{

pstart++;

}

Move forward pend until an odd number is encountered

while (pstart<pend&& (*pend&1) ==0)

{

pend--;

}

if (pstart<pend)

{

Temp=*pstart;

*pstart=*pend;

*pend=temp;

}

}

}

int Main ()

{

int arr[]={1,2,3,4,5,6,7,8,9,10};

int length= sizeof (arr)/ sizeof (Arr[0]);

int i=0;

Reorderarray (arr,length);

for (i=0;i<length;i++)

printf ("%d", Arr[i]);

System ("pause");

return 0;

}


Adjust the order of the arrays so that the odd digits are in the even front

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.