public class Solution { public void reOrderArray(int [] array) { //相对位置不变,稳定性 //插入排序的思想 int m = array.length; int k = 0;//记录已经摆好位置的奇数的个数 for (int i = 0; i < m; i++) { if (array[i] % 2 == 1) { int j = i; while (j > k) {//j >= k+1 int tmp = array[j]; array[j] = array[j-1]; array[j-1] = tmp; j--; } k++; } } } }
//两个思路吧,第一个思路:类似冒泡算法,前偶后奇数就交换: class Solution { public: void reOrderArray(vector<int> &array) { for (int i = 0; i < array.size();i++) { for (int j = array.size() - 1; j>i;j--) { if (array[j] % 2 == 1 && array[j - 1]%2 == 0) //前偶后奇交换 { swap(array[j], array[j-1]); } } } } }; //第二个思路:再创建一个数组 class Solution{ public: void reOrderArray(vector<int> &array) { vector<int> array_temp; vector<int>::iterator ib1, ie1; ib1 = array.begin(); for (; ib1 != array.end();){ //遇见偶数,就保存到新数组,同时从原数组中删除 if (*ib1 % 2 == 0) { array_temp.push_back(*ib1); ib1 = array.erase(ib1); } else{ ib1++; } } vector<int>::iterator ib2, ie2; ib2 = array_temp.begin(); ie2 = array_temp.end(); for (; ib2 != ie2; ib2++) //将新数组的数添加到老数组 { array.push_back(*ib2); } } };
时间复杂度为O(n),空间复杂度为O(n)的算法 /* 整体思路: 首先统计奇数的个数 然后新建一个等长数组,设置两个指针,奇数指针从0开始,偶数指针从奇数个数的末尾开始 遍历,填数 */ public class Solution { public void reOrderArray(int [] array) { if(array.length==0||array.length==1) return; int oddCount=0,oddBegin=0; int[] newArray=new int[array.length]; for(int i=0;i<array.length;i++){ if((array[i]&1)==1) oddCount++; } for(int i=0;i<array.length;i++){ if((array[i]&1)==1) newArray[oddBegin++]=array[i]; else newArray[oddCount++]=array[i]; } for(int i=0;i<array.length;i++){ array[i]=newArray[i]; } } }
public class Solution { public void reOrderArray(int [] array) { if (array != null) { int[] even = new int[array.length]; int indexOdd = 0; int indexEven = 0; for (int num : array) { if ((num & 1) == 1) { array[indexOdd++] = num; } else { even[indexEven++] = num; } } for (int i = 0; i < indexEven; i++) { array[indexOdd + i] = even[i]; } } } }
类似插入排序,当前数是奇数,就往前找,遇到偶数就往它前面插
class Solution { public: void reOrderArray(vector<int> &array) { for (int i = 1; i < array.size(); i++){ int tmp = array[i]; if (tmp % 2 == 1){ for (int j = i; j > 0; j--){ if (array[j - 1] % 2 == 0){ int t = array[j]; array[j] = array[j - 1]; array[j - 1] = t; } } } } } };
public void reOrderArray(int [] array) { int [] tmp = new int[array.length]; int j=0; int o=array.length - 1; for (int i = 0; i < array.length; i++) { if (array[i] % 2 != 0) { tmp[j++] = array[i]; } if (array[array.length - i - 1] % 2 == 0) { tmp[o--] = array[array.length - i - 1]; } } for (int i = 0; i < tmp.length; i++) { array[i] = tmp[i]; } }
if(array==null){ return; } //原书中的方法 类似于快排 /*int i=0; int j=array.length-1; while(i<j){ while(i<j&&array[i]%2==1){ i++; } while(i<j&&array[j]%2==0){ j--; } int temp=array[j]; array[j]=array[i]; array[i]=temp; }*/ //由于要保证稳定即证奇数和奇数,偶数和偶数之间的相对位置不变 使用插入排序思想 for(int i=0;i<array.length;i++){ //int temp=array[i]; if(array[i]%2==1){ int temp=array[i]; int j=i-1; while(j>=0&&array[j]%2==0){ array[j+1]=array[j]; j--; } array[j+1]=temp; } }
public class Solution { public void reOrderArray(int [] array) { for(int i=0;i<array.length-1;i++) for(int j=0;j<array.length-i-1;j++){ if(array[j]%2==0 && array[j+1]%2==1){ int temp=array[j]; array[j]=array[j+1]; array[j+1]=temp; } } } }采用冒泡排序的思想,如果采用另开辟一个数组的话,相当于拿空间换时间。
public void reOrderArray(int [] array) { for(int i=1;i<array.length;i++){ int target = array[i]; if(array[i] % 2 == 1){ int j = i; while(j >= 1 && array[j-1] % 2 == 0){ array[j] = array[j-1]; j--; } array[j] = target; } } }
void reOrderArray(vector<int> &array) {
vector<int>::iterator currentIter = array.begin();
for (vector<int>::iterator it = array.begin(); it != array.end(); it++)
{
if (*it % 2 == 1)
{
if (it != currentIter)
{
int nTemp = *it;
for (vector<int>::iterator it1 = it; it1 != currentIter; it1--)
{
*it1 = *(it1 - 1);
}
*currentIter = nTemp;
}
currentIter++;
}
}
}
其实就是快排的变种,虽然快排是不稳定的,但是可以稍微改进一下,下面是根据算法导论的快排改编的 class Solution { public: void reOrderArray(vector<int> &array) { int size = array.size(); if(size == 0 || size == 1) return; int p = -1; int q = 0; while(q < size) { if ((array[q] & 1) != 0) { p++; int i = q; int temp = array[i]; while(i > p) { array[i] = array[i-1]; i--; } array[p] = temp; } q++; } } };
//时间17ms,内存9288k public class Solution { public void reOrderArray(int [] array) { int [] odd = new int[array.length];//奇数数组 int [] even = new int[array.length];//偶数数组 int odd_number = 0;//奇数个数 int even_number = 0;//偶数个数 for(int i = 0;i<array.length;i++){//遍历原数组 switch(array[i]%2){ case 1://奇数 odd[odd_number] = array[i]; odd_number ++; break; default://偶数 even[even_number] = array[i]; even_number++; break; } } for(int i =0,x=0;i<array.length;i++){ if(i<odd_number){ array[i] = odd[i]; }else{ array[i] = even[x]; x++; } } } }