21 wins the face questions offer- adjusted so that the array sequentially positioned in front of the even odd - bis pointer

/ * 
Title: 
	adjust the order of the array so that the even-odd in front. 
* / 
/ * 
Ideas: 
	the double pointer: 
		a pointer for the last traverse, when an odd number is +1, 
		when an even number, the exchange value and the last point of the pre, pre pointer moves forward. 
* / 
#Include <the iostream> 
#include <string.h> 
#include <algorithm> 
#include <the cmath> 
#include <stdio.h> 
the using namespace STD; 

void reOrderArray (Vector <int> & ARR) { 
    IF (arr.empty ()) return; 
    int = 0 pre, Last = 0; 
    int length = arr.size (); 

    the while (Last <length) { 
        IF (ARR [Last] == & 0x1. 1) { 
            ! IF (= pre Last) { 
                int TEMP = ARR [Last]; 
                ARR [Last] = ARR [pre]; 
                ARR [pre] = TEMP; 
            }
            pre++;
        }
        last++;
    }
}


int main(){
    int a[] = {2,4,6,8,1,3,5};
    vector<int> arr(a,a+7);
    reOrderArray(arr);
    for(int i = 0; i < 7; i++){
        cout<<arr[i]<<" ";
    }

}

   

Guess you like

Origin www.cnblogs.com/buaaZhhx/p/11900938.html