Title: Given an array of integers, write an algorithm that moves an even number to the right of the array, and an odd number on the left side of the array, considering the optimal algorithm for time and space.
PackageCom.forezp.util;/*** Title: Given an array of integers, write an algorithm that moves an even number to the right of the array, and an odd number on the left side of the array, considering the optimal algorithm for time and space. * * @authorAdministrator **/ Public classArraydemo {//determine if an even number Public Static BooleanIsEven (intN) { return(n & 1) = = 0 ; } //determine if an even number Public Static BooleanIsEven2 (intN) { return(n% 2) ==0; } //Defining Algorithms Public voidOrderint[] arr) { if(NULL==arr) { return ; } inti = 0; intj = Arr.length-1 ; while(i<j) { //I is an even number, J is an odd number if(IsEven (arr[i]) &&!IsEven (Arr[j])) { inttemp =Arr[i]; Arr[i]=Arr[j]; ARR[J]=temp; }Else if(!iseven (Arr[i]) &&IsEven (Arr[j])) {i++; }Else if(IsEven (Arr[i]) &&IsEven (Arr[j])) {J--; }Else{i++; } } } Public Static voidMain (string[] args) {Arraydemo Arraydemo=NewArraydemo (); int[] arr = {4,3,5,2,10,6,7,22,23,34,45,56}; Arraydemo.order (arr); for(intI:arr) {System.out.println (i); } System.out.println (IsEven (1)); System.out.println (IsEven (12)); System.out.println (IsEven (13)); System.out.println (IsEven (14)); System.out.println (IsEven (15)); System.out.println ("--------------"); System.out.println (1 & 1); System.out.println (& 1); System.out.println (& 1); System.out.println (& 1); } }
Operation Result:
The first of these is the method of determining whether an even number is:
Java Common Algorithm questions (27)