Ideas similar to Quick sort
Using two pointers, one pointer to the front element, and the other pointer to the last element;
The first pointer moves to the right until an even number is encountered, and the second pointer moves forward until it encounters an odd number and then swaps the two data.
It ends when the left hand pointer meets the right pointer, and then returns the array.
1 #!/usr/bin/env Python32 3 defJi_first_ou (ary):4Indexa,indexb = 0,len (ary)-15 whileIndexa <Indexb:6 whileAry[indexa] & 1 = = 1 andIndexa <Indexb:7Indexa + = 18 whileARY[INDEXB] & 1! = 1 andIndexa <Indexb:9INDEXB-= 1TenARY[INDEXA],ARY[INDEXB] =Ary[indexb], Ary[indexa] One returnary A - if __name__=="__main__": -A = [1,2,3,4,5,6,7,8,9,10] the Print(a) - Print(Ji_first_ou (a))
Programming Exercise: Enter an array, write a function so that all the odd digits are in the first part of the array, and all the even digits are in the back part of the array