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

Source: Internet
Author: User

Description: 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, all the even digits are located in the second half of the array, and the relative positions between the odd and odd, even and even, are guaranteed.

If you just want to place the odd number in the first half of the array, the even number is in the second half of the array, and it is better to solve it directly with a quick sort. But there is also a need to ensure that the relative positions of the odd and even numbers are the same, and the insertion sort is used here.

1 classSolution {2  Public:3     voidReorderarray (vector<int> &Array) {4          for(intI=0; I<array.size (); i++){5             if((array[i) &1)==1){6                 inttemp=Array[i];7                 intj=i-1;8                  while(j>=0&&array[j]%2==0){9array[j+1]=Array[j];Tenj--; One                 } Aarray[j+1]=temp; -             }             -         } the     } -};

For the problem of the position relationship after the re-queueing is not considered, it is still variant and will continue to be analyzed later.

Brush Title 14 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.