Put an odd number of an array in front of an even number

Source: Internet
Author: User

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, and all the even digits are in the second half of the array

Ideas:

Declares two pointers, one pointing to the head of the array, the other pointing to the end of the array, and the first even and tail pointers pointed to by the head pointer are exchanged until the head pointer is larger than the tail pointer jumps out of the loop.

The code is as follows:

voidTiaozheng (int*data,unsignedintlength) {    if(data==null| | length<=0)        Throwexception"parameter Error! "); int*begin=data; int*end=data+length-1; //swaps the first even and end of the begin, until Begin>=end, when all the odd numbers are in front of an even number     while(begin<end) {        //the number that the begin points to is odd, begin++, and the even one jumps out of the loop         while(Begin<end && (*begin &0x1)==1) Begin++; //end points to an even number of end++, and an odd number jumps out of the loop         while(Begin<end && (*end &0x1)==0) End--; //If Begin<end exchange these two numbers ,        if(begin<end) {            inttemp=*begin; *begin=*end; *end=temp; }    }}

The above method can only be used to distinguish between odd and even, the program's generality is not high, for example, if you need to separate positive and negative numbers, you need to re-write a function, the following is a common template for solving such problems, if you need to divide positive and negative numbers, the function of the parameter func to determine the positive negative number function. (In this case, the method of judging the odd even number Isjishu)

The code is as follows:

//determines whether the function is odd, odd returns True, even returns falseBOOLIsjishu (intx) {    if(X &0x1==1)        return true; Else if(X &0x1==0)        return false;}//Print functionvoidPrintint*data,intlength) {    int*begin=data; intCount=0;  while(count<length) {cout<<*begin<<"  "; Begin++; Count++; } cout<<Endl;}//use common templates to solve such problemsvoidTiaozheng2 (int*data,unsignedintLengthBOOL(*func) (int))    {    if(data==null| | length<=0)        Throwexception"parameter Error! "); int*begin=data; int*end=data+length-1; //swaps the first even and end of the begin, until Begin>=end, when all the odd numbers are in front of an even number     while(begin<end) {        //the number that the begin points to is odd, begin++, and the even one jumps out of the loop         while(Begin<end && func (*begin)) Begin++; //end points to an even number of end++, and an odd number jumps out of the loop         while(Begin<end &&!func (*end)) End--; //If Begin<end exchange these two numbers ,        if(begin<end) {            inttemp=*begin; *begin=*end; *end=temp; }    }}

Test the code and run the results:

int Main () {    int data[5]={1,2,3,4,5  };    Print (data,5);    Tiaozheng2 (data,5, Isjishu);    Print (data,5);     return 0 ;}

Put an odd number of an array in front of an even number

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.