Swords refer to offer-High-quality code (adjust array order so that odd digits precede even numbers)

Source: Internet
Author: User

Title: Enter an array of integers to implement a function that adjusts 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 back half of the array.

Idea: With two pointers P1 and P2, respectively pointing to the head and tail of the array, p1 only backward, p2 only forward. When the P1<P2 condition is met, the P1 can be moved backwards and the first even number is found, P2 moves forward until the first odd-numbered is found, and the values of P1 and P2 are exchanged if the conditions of the P1<P2 are also met.

C + + code: This code extensibility is manifested as: the function bool (*FUNC) (int) as a parameter to the preorder, and at this time to determine the parity of the number, you can write a function as shown in the above format: bool IsEven (int n).

When the title changes, it is required to put the number divisible by 3 in the array in front, the other arrays in the back, you can write one of the other functions shown in the format above.

#include <iostream>using namespacestd;voidPreorder (int* pdata,unsignedintLengthBOOL(*func) (int)){    if(pdata==null| | length==0)        return; int* pstart=pdata; int* pend=pdata+length-1;  while(pstart<pend) {         while(Pstart<pend&&!func (*Pstart)) Pstart++;  while(Pstart<pend&&func (*pend)) Pend--; if(pstart<pend) {            inttemp=*Pstart; *pstart=*Pend; *pend=temp; }    }}BOOLIsEven (intN) {    return(n&1)==0;}voidPreorderoddeven (int* pdata,unsignedintlength) {Preorder (pdata,length,iseven);}voidMain () {inta[5]={1,2,3,4,5}; int* Pdata=A; Preorderoddeven (pdata,5);  for(intI=0;i<5; i++) cout<<pdata[i]<<" "; cout<<Endl;}

Java code: The way to embody extensibility is to write an interface, which puts an abstract method boolean func (int n), and other specific topics, as long as the implementation of this method can be.

 Public classPreorderoddeven {/** The odd number in an array is preceded by an even number * @param pdata array * @param length of a length array*/     Public voidPreorder (int[] pdata,intlength) {        if(pdata==NULL|| Length<=0)            return; intPstart=0; intPend=length-1;  while(pstart<pend) {             while(pstart<pend&&!IsEven (Pdata[pstart]) Pstart++;  while(pstart<pend&&IsEven (Pdata[pend]) pend--; if(pstart<pend) {                inttemp=Pdata[pstart]; Pdata[pstart]=Pdata[pend]; Pdata[pend]=temp; }        }    }    /** Determine the parity of numbers in an array * @param number in n array*/     Public  BooleanIsEven (intN) {return(n&1) ==0; }     Public Static voidMain (string[] args) {Preorderoddeven Poe=NewPreorderoddeven (); int[] a={1,2,3,4,5};        Poe.preorder (a,a.length);  for(inti=0;i<5;i++) System.out.print (A[i]+" "); }}

Swords refer to offer-High-quality code (adjust array order so that odd digits precede even numbers)

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.