Adjust the array order so that the odd number is preceded by even numbers--the sword refers to the offer

Source: Internet
Author: User

Title: Enter an array of integers to implement a function to adjust the numbers 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.

Code:

1#include <stdio.h>2 BOOLIsEven (intN)3 {4     return(N &1) ==0;5 }6 voidReorder (int*pdata, unsignedintLengthBOOL(*func) (int))7 {8     if(PData = = NULL | | length = =0)9         return;Ten     int*pbegin =PData; One     int*pend = pData + Length-1; A      while(Pbegin <pEnd) -     { -          while(Pbegin < pEnd &&!func (*pbegin))//If the previous pointer points to a number that is odd thepbegin++; -          while(Pbegin < PEnd && func (*pend))//If the number pointed to by the following pointer is even -pend--; -         if(Pbegin <pEnd) +         { -             inttemp = *Pbegin; +*pbegin = *pEnd; A*pend =temp; at         } -     } -      - } - intMain () - { in     inta[8] = {5,1,6,2,3,7,8,Ten }; -Reorder (A,8, IsEven); to      for(inti =0; I <8; i++) +printf"%3d", A[i]); -printf"\ n"); the     return 0; * } $ /*Panax Notoginseng 5 1 7 3 2 6 8 - Please press any key to continue ... the  + */
View Code

Adjust the array order so that the odd number is preceded by even numbers--the sword refers to an offer

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.