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:
DD style= "margin:0px; padding:0px ">

Enter an array of integers, Implement a function to adjust the order of the numbers in the array. Causes all the odd digits to be in the first half of the array. All the even digits are located in the second half of the array and are guaranteed to be odd and odd, with the relative positions of even and even numbers unchanged.

input:


For each test case, enter an n for the first line, representing the number of digits in the array.


Enter n integers for the next line. Represents the number of n in an array.

Output:

Each test case,
Enter a row of n digits. Represents the adjusted array.

Note that the numbers and numbers are separated by a space, and there are no spaces after the last number.

Example input:
51 2 3) 4 5
Example output:
1 3 5) 2 4
The problem is a bit tricky. Assuming that you want to use the time complexity of O (N), the spatial complexity will be very high.
Assuming the time complexity is O (n^2), the space is complex and low.

Look at the spatial complexity of 3n first. Code with a time complexity of N:

#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.