If we consider the solution of violence, every time from the back to sweep the surface, encountered even in the final, so that the time complexity is O (n*n), for this problem is obviously too high, we consider scanning again, with two pointers, one from the back, the other from the back to the front, when the odd moment, the exchange, So the algorithm complexity is only O (n).
#include <stdio.h> #include <stdlib.h>void reset (int a[],int n) {int i=0,j=n-1,tmp=0;while (I<J) {if (a[i ]%2==1) && (a[j]%2==0) {i++;j--;} else if ((a[i]%2==1) && (a[j]%2==1)) I++;else if ((a[i]%2==0) && (a[j]%2==1)) {tmp=a[i];a[i]=a[j];a[j]= TMP;} elsej--;}} int main () {int n;while (scanf ("%d", &n)!=eof) {int *a= (int *) malloc (sizeof (int) *n); int i;for (i=0;i<n;i++) scanf ( "%d", &a[i]), Reset (A,n), for (i=0;i<n;i++) printf ("%d", A[i]);p rintf ("\ n");} return 0;}
Adjust the array so that the odd digits precede the even number