The odd array on the left, on the right the even

/ * * 
 * I using two pointers and j, are initialized to zero. J traverse back and, if encountered odd, then A [j] and A [i] exchange position while i is incremented by one, down to this operation, the same may be placed in front of all of the even number odd 
 * 
 * / 
public  class SortArrayByParity { 

    
    public  static  void main (String [] args) { 
        
        int [] ARR = new new  int [] { 2 , . 3 , . 4 , . 5 , . 6 , . 7 , . 8 , . 9 , 10 , 12 is , 14 , 15 , 16 , . 17 }; 
        Sort (ARR); 
        Arrays.stream (ARR) .forEach (S-> System.out.print(s+ " "));
    }
    
    
    public static void sort(int [] arr) {
        int i = 0 ;
        for(int j = 0 ; j < arr.length ; j ++) {
            if(arr[j] % 2 == 1 ) {
                swap(j,i,arr);
                i++;
            }
        }
    }
    
    public static  void swap(int a , int b , int [] arr) {
        int temp = arr[a];
        arr[a] = arr[b];
        arr[b] = temp; 
    }
}

 

Guess you like

Origin www.cnblogs.com/moris5013/p/11805110.html