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

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.