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

Source: Internet
Author: User

Topic

Enter an array of integers. 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 even digits are in the second half of the array.

Analysis

The more straightforward method of stupidity is to traverse each, just if even, take out, will be the number of subsequent moves forward, the even fill in the end of the array, each encounter an even number to move o (n) numbers, then the total time complexity is O (n^2), the efficiency is too low, how to improve. The ability to observe an array assumes that the odd number is preceded by even numbers, which are reversed in order to be replaced. The method of traversing from both ends to the middle is relatively simple, just do not match the odd in the even before, then must be reversed, otherwise go to the middle to continue to look for traversal, until found all meet the conditions.


For example analysis, array {1. 2,3,4,5}. Finally want to be odd in front. Even after, here is not concerned about the problem of sorting, so, refer to the following process diagram to understand how to adjust the order of high speed.


Finally the result is {1, 5. 3,4,2}

"Test Code"

#include <stdio.h>void Swap (int *P1,int *P2) {}void Reorder_odd_even (int *p,int length){int *P1= P;int *P2= p+length-1; while(P1&LT;P2) { while((*P1&0x1!=0) && (P1&LT;P2)) p1++; while((*P2&0x1) ==0&& (P1&LT;P2)) p2--;if(P1&LT;P2) {inttemp =*P1;*P1=*P2;*P2= temp; }}}voidPrint(int s[],int length){ for(inti =0; i<length; i++)printf("%d",s[i]);}intMain () {int s[ ] = {1,2,3,4,5}; Reorder_odd_even (s,5);Print(s,5);return 0;}

Extension
Suppose this topic changes, for example, to change the filter conditions so that all can be divisible by 3 in the front can not be divisible by 3, or to meet negative numbers in front of non-negative numbers. The above code to the corresponding part of the filter conditions as required changes can be, but, how can we make the program more universal, is similar to the problem with our code directly can solve, do not always change, can think of is to extract the filter conditions to become a function alone. In this test code you simply need to invoke the conditional function you want to infer, without changing, so we write out the following code, for example:

#include <stdio.h>BOOL IsEven (intN) {return(n&1)==0;} void Reorder (int *p,int length, BOOL (*func)(int)){int *P1= P;int *P2= p+length-1; while(P1&LT;P2) { while(!func (*P1) && (P1&LT;P2)) p1++; while(Func (*P2) && (P1&LT;P2)) p2--;if(P1&LT;P2) {inttemp =*P1;*P1=*P2;*P2= temp; }}}void Reorder_odd_even (int s[],int length) {Reorder (s,length, IsEven);} voidPrint(int s[],int length){ for(inti =0; i<length; i++)printf("%d",s[i]);}intMain () {int s[ ] = {1,2,3,4,5}; Reorder_odd_even (s,5);Print(s,5);return 0;}

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.