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

Source: Internet
Author: User

Title Description: Given an array of integers, implement a function to adjust the order of numbers in the array so that the odd number is in the first half of the array, even in the latter part of the array.

For example, the array is: [1,2,3,4,5]. Then the adjusted array is: [1,3,5,2,4]

Stupid method

The stupid method is to start looking for the first member of the array, to take it out whenever an even number is encountered, to move all the subsequent members forward one at a time, and then to place the even number that is removed in the last. Time Complexity of O (n^2)


Good way

The stupid method does a lot of useless work, so it can be implemented with two pointers.

Set two pointer p1,p2, which point to the first and last position of the array, and then move backward when the P1 is odd until an even number is encountered. P1 encounter even and then look at P2, if P2 is an even number then move forward one position, until P2 is odd. At this time if P2 is odd, but also to determine if P2 is not on the right side of P1, if on the left, the function has been completed, return a bit, otherwise exchange P1 and P2 point to the number (that is, parity swap).


To illustrate:

Step1:1 2 3 4 5

P1 P2


Step2:1 2 3 4 5

P1 P2


Step3:1 5 3 4 2

P1 P2


Step4:1 5 3 2 4

P1 P2


Step5:1 5 3 2 4

P2 P1

At this time P2 on the left of P1, return [1,5,3,2,4]


Source

#coding: utf-8# face question 14: Let the odd number in the array be preceded by the even-numbered Def reorderoddeven (data): # data: [1,2,3,4,5]    p1 = 0 # previous pointer    p2 = len (data)-1 # After pointer    if p1 = = P2:        return False    else: While        True: If            data[p1]% 2 = = 1: # If the number of P1 position is odd                P1 + = 1            el SE: If                data[p2]% 2 = = 0: # If the P2 position is an even number                    P2-= 1                else:                    if p1 > P2: Break                    Else: # otherwise exchange P1,P2 The number                        flag = Data[p1]                        data[p1] = data[p2]                        data[p2] = Flag        return datadata = [1,2,3,4,5]print Reorderoddeven (data)


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