Algorithm Note _049: Odd even order (Java)

Source: Internet
Author: User

Directory

1 Problem Description

2 Solutions

2.1 One end of the hand toward the middle scanning method

2.2 The first and last two pointers at the same time backward scanning method

 1 problem description

given an array of integers, 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. Requires a time complexity of O (n).

2 Solutions 2.1 One end of the hand toward the middle scanning method

The specific code is as follows:

 Packagecom.liuzhen.array_2; Public classOddevensort {//Solution 1: One end of the pointer to the middle scan     Public voidGetOddEvenSort1 (int[] A) {        if(A.length = = 1)            return; intBegin = 0; intEnd = A.length-1;  while(Begin <end) {            if(A[begin]% 2 = = 1)//When A[begin] is oddbegin++; Else if(A[end]% 2 = = 0)//when A[end] is an even numberend--; Else                 //when A[begin] is not odd and a[end] is not an even numberswap (a,begin,end); }    }        //swap the value of the M position and n position of array a     Public voidSwapint[] A,intMintN) {        inttemp =A[m]; A[M]=A[n]; A[n]=temp; }         Public Static voidMain (string[] args) {oddevensort test=NewOddevensort (); int[] A = {2,1,4,7,1,4,7,1,2,8,4,3,6,7,2,14,3,7,4,3,2,4,3,2,7};        Test.getoddevensort1 (A); System.out.println ("Use Method 1: One end of the pointer to the middle scan results:");  for(inti = 0;i < a.length;i++) System.out.print (A[i]+" "); }}

Operation Result:

Use Method 1: One end of the pointer toward the middle scan results:

2.2 The first and last two pointers at the same time backward scanning method

The specific code is as follows:

 Packagecom.liuzhen.array_2; Public classOddevensort {//Solution 2: One last and two pointers back scan     Public voidGetOddEvenSort2 (int[] A) {        if(A.length = = 1)            return; intOrigin = 0;//defines the position of the standard element, and the end result is an odd number on the left side of the element value, an even number to the right of the element value        inti = 0;  for(intj = 1;j < a.length;j++){            if(A[j]% 2 = = 1) {//when A[j] is odd, move right one bit and exchange a[i] and a[j] values, indicating that the left side of I is an odd numberi++;            Swap (A,I,J);    }} swap (A,i,origin); }        //swap the value of the M position and n position of array a     Public voidSwapint[] A,intMintN) {        inttemp =A[m]; A[M]=A[n]; A[n]=temp; }         Public Static voidMain (string[] args) {oddevensort test=NewOddevensort (); int[] B = {2,1,4,7,1,4,7,1,2,8,4,3,6,7,2,14,3,7,4,3,2,4,3,2,7};        Test.getoddevensort2 (B); System.out.println ("\ n Use Method 2: One last two pointers back scan results:");  for(inti = 0;i < b.length;i++) System.out.print (B[i]+" "); }} 

Operation Result:

Use Method 2: One after the last two pointers back scan results:

Resources:

1. Programming method interview and algorithmic experience July

Algorithm Note _049: Odd even order (Java)

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.