public class Solution { private boolean isEven(int n){ return (n&1) == 0; } public void reOrderArray(int [] array) { if(array.length==0){ return; } int left = 0; int right = array.length-1; while(left<right){ while(left<right && !isEven(array[left])){ left++; } while(left<right && isEven(array[right])){ right--; } int temp = array[left]; array[left] = array[right]; array[right] = temp; } } }
插入排序代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
public class Solution { public void reOrderArray(int [] array) { int count = 0;//记录已经摆好位置的奇数的个数 int len = array.length-1; for (int i = 0; i <= len; i++) { if(array[i]%2==1){ int j = i; while(j>count){ int temp = array[j]; array[j] = array[j-1]; array[j-1] = temp; j--; } count++; } } } }