033 Adjust the array order so that the odd digits precede the even number (keep it up)

Source: Internet
Author: User

The sword refers to the offer topic: http://ac.jobdu.com/problem.php?pid=1516

Title Description:

Enter an array of integers to implement a function that adjusts the order of the numbers in the array so that all the odd digits are placed 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.

input:

Each input file contains a set of test cases.
For each test case, enter an n for the first row, representing the number of digits in the array.
Enter n integers for the next line. Represents the number of n in an array.

Output:

corresponding to each test case,
Enter a row of n digits, representing the adjusted array. Note that the numbers and numbers are separated by a space, and there are no spaces after the last number.

Sample input:
51 2 3) 4 5
Sample output:
1 3 5) 2 4
This problem is a bit tricky, if you want to use time complexity of O (N), the space complexity will be very high;
If the time complexity is O (n^2), the space is complex and low.

Look at the space complexity 3n, the time complexity of n code:

#include <stdio.h> #define MAXSIZE 100000 int array[maxsize];int evenindex[maxsize];int oddindex[maxsize];    int main () {int i;    int N;    int evencount;     int oddcount;        while (scanf ("%d", &n)! = EOF) {evencount = 0;         Oddcount = 0;             for (i = 0; i < N; ++i) {scanf ("%d", array+i);            If (Array[i]% 2 = = 0) evenindex[evencount++] = i;        else oddindex[oddcount++] = i;        } for (i = 0; i < OddCount-1; ++i) {printf ("%d", Array[oddindex[i]]);            } if (Oddcount > 0) {printf ("%d", array[oddindex[oddcount-1]]);        if (Evencount > 0) printf ("");        } for (i = 0; i < EvenCount-1; ++i) {printf ("%d", Array[evenindex[i]]);        } if (Evencount > 0) {printf ("%d\n", Array[evenindex[evencount-1]]); }} return 0;} /**************************************************problem:1516 user:language:c result:accepted time:70 Ms memory:2084 kb**************** ************************************************/

Time complexity is n^2, timeout:

#include <stdio.h> #define MAXSIZE 100000 int array[maxsize];    void test4 (int varray[], int vLen) {int i;    int k;     int Temp;            for (i = 0; i < VLen, ++i) {if (Varray[i]% 2) {for (k = i-1; k >= 0;--k)                {if (Varray[k]% 2) break;                    If (Varray[k]% 2 = = 0) {Temp = varray[k+1];                    VARRAY[K+1] = varray[k];                Varray[k] = Temp;    }}}} for (i=0; i < vLen-1; ++i) {printf ("%d", varray[i]); } printf ("%d\n", Varray[vlen-1]);}    int main () {int i;     int N;        while (scanf ("%d", &n)! = EOF) {for (i = 0; i < N; ++i) {scanf ("%d", &array[i]);    } test4 (Array, N); } return 0;} /************************************************************** problem:1516 user:language:c result:time L Imit exceed****************************************************************/ 




033 Adjust the array order so that the odd digits precede the even number (keep it up)

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.