21 to adjust the order of the array so that the even-odd in front

Title: Enter an array of integers, to realize a function to adjust the order of the numbers in the array, such that all of the odd part of the front half of the array, all the even located in the second half of the array.

def reorder_odd_event(arry):
    if len(arry)<2:
        return arry
    begin,end = 0,len(arry)-1
    while begin<end:
        if arry[begin]%2==1:
            begin+=1
        if arry[end]%2==0:
            end-=1
        if arry[begin]%2==0 and arry[end]%2==1:
            arry[begin],arry[end] = arry[end],arry[begin]
            begin+=1
            end-=1
    return arry

Note:

Two pointers, from the front, after traversing two directions, the current pointer is encountered odd side, the right location described, continue to the next traverse, when faced with even, odd and traversed back exchange, until the two pointers meet.

Guess you like

Origin www.cnblogs.com/kingshine007/p/11354206.html