Description: Enter an array of integers to implement a function to adjust the order of the numbers in the array, so that all the odd digits are in the first half of the array, all the even digits are located in the second half of the array, and the relative positions between the odd and odd, even and even, are guaranteed.
If you just want to place the odd number in the first half of the array, the even number is in the second half of the array, and it is better to solve it directly with a quick sort. But there is also a need to ensure that the relative positions of the odd and even numbers are the same, and the insertion sort is used here.
1 classSolution {2 Public:3 voidReorderarray (vector<int> &Array) {4 for(intI=0; I<array.size (); i++){5 if((array[i) &1)==1){6 inttemp=Array[i];7 intj=i-1;8 while(j>=0&&array[j]%2==0){9array[j+1]=Array[j];Tenj--; One } Aarray[j+1]=temp; - } - } the } -};
For the problem of the position relationship after the re-queueing is not considered, it is still variant and will continue to be analyzed later.
Brush Title 14 Adjust the array order so that the odd digits are preceded by even numbers