Adjust the array order so that the odd digits are preceded by even numbers

Source: Internet
Author: User

Topic:

Enter an array of integers to implement a function to adjust the order of the numbers in the array so that all the odd digits are in the first half of the array, and all the even digits are located in the second half of the array.


Resolution: The topic did not say the relative position unchanged, with two cursors pointing to the head and tail, the front of the even and the back of the odd exchange can be.

/* Function function */void reorderoddeven (int a[], int length) {int i,j;//define two cursors int temp;if (a==null| | length==0) {printf ("Invalid Input"); return;} Cursors point to Array first i=0;j=length-1;while (I<J) {//To even while (i<j&& (a[i]&0x1)!=0) {i++;} Point to Odd while (i<j&& (a[j]&0x1) ==0) {j--;} Exchange Temp=a[i];a[i]=a[j];a[j]=temp;}}


Adjust the array order so that the odd digits are preceded by even numbers

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.