Rearrange the array without changing the relative order of positive and negative numbers

Source: Internet
Author: User

See a solution on the internet, space complexity O (1), time complexity O (nlogn)

Make a small algorithm question to boost your mind

An unordered integer array with positive and negative numbers. Sort the negative numbers before the positive number, and do not change the relative order between the original positive and negative numbers.

The process of thinking is omitted. Let's talk about the results directly. I think of an algorithm. The space complexity is O (1), and the time complexity is O (n * logn ).

First, define such a process as "flip": (A1, A2 ,..., am, B1, B2 ,..., BN) --> (B1, B2 ,..., BN, A1, A2 ,..., am ). Secondly, for the unsorted integer array to be processed, scan from start to end to find (positive... plus or minus... negative) string; each time such a string is found, the counter is incremented by 1; if the count is an odd number, a "flip" is performed on the current string; scan repeatedly, until it cannot be found again (Zheng... plus or minus... negative) string.

Example:

Input: 1,-1, 2,-2, 3,-3, 4,-4, 5,-5, 6,-6, 7,-7, 8, -8
Step1.1: [1,-1], 2,-2, [3,-3], 4,-4, [5,-5], 6,-6, [7,-7], 8,-8
Step1.2: [-1, 1], 2,-2, [-3, 3], 4,-4, [-5, 5], 6,-6, [-7, 7], 8,-8
Step2.1:-1, [1, 2,-2,-3], 3, 4,-4,-5, [5, 6,-6,-7], 7, 8,-8
Step2.2:-1, [-2,-3, 1, 2], 3, 4,-4,-5, [-6,-7, 5, 6], 7, 8,-8
Step3.1:-1,-2,-3, [1, 2, 3, 4,-4,-5,-6,-7], 5, 6, 7, 8,-8
Step3.2:-1,-2,-3, [-4,-5,-6,-7, 1, 2, 3, 4], 5, 6, 7, 8,-8
Step4.1:-1,-2,-3,-4,-5,-6,-7, [1, 2, 3, 4, 5, 6, 7, 8, -8]
Step4.2:-1,-2,-3,-4,-5,-6,-7, [-8, 1, 2, 3, 4, 5, 6, 7, 8]
Output:-1,-2,-3,-4,-5,-6,-7,-8, 1, 2, 3, 4, 5, 6, 7, 8

The proof is as follows:

Calculate the time complexity of "flip.

(A1, A2 ,..., am, B1, B2 ,..., BN) is flipped to (B1, B2 ,..., BN, A1, A2 ,..., am), as long as three steps:
(A1, A2,..., am, B1, B2,..., bn) -->
(Bn,..., B2, B1, am,..., A2, A1) -->
(B1, B2,..., bn, am,..., A2, A1) -->
(B1, B2,..., bn, A1, A2,..., am)
In general, the time complexity is O (2 m + 2n)

For a number of strings with a length of N, you can select several of them for flip. The total time complexity does not exceed O (n ).

The following question is how many scans were performed.

Because each scan is a flip that is not adjacent (Zheng... plus or minus... negative) string, so all the flipped strings are divided into two parts from the positive and negative Division, respectively divided into two adjacent (positive... plus or minus... negative) string. Therefore, each scan can eliminate half of the strings (positive... Positive and Negative... negative. In the end, the maximum number of scans is logn.

In summary, the time complexity is O (n * logn ).

Address: http://qing.weibo.com/1570303725/5d98eeed33000hcb.html

Of course, this detailed introduction can see: http://blog.csdn.net/v_july_v/article/details/7329314





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.