Classical algorithm (9) from the merge sort to the sequence of the reverse number pairs (Microsoft written questions)

Source: Internet
Author: User
Tags printf sort

First, look at the original question.

Microsoft 2010 Writing Test

In an arrangement, if the front and rear positions of a pair of numbers are the opposite of the size order, that is, the preceding number is greater than the number in the following, then they are called an inverse number pair. The total number of reverse order in an arrangement is called the reverse number of this permutation. As in {2,4,3,1}, 2 and 1,4 and 3,4 and 1,3 and 1 are the reverse number pairs, so the entire array has a number of reverse-order pairs of 4, now given an array, we need to count the number of reverse pairs of the array.

The simplest way to calculate the number of reverse numbers in a sequence is to count whether each number and the number following it can be used to make up the inverse number pairs. The code is as follows:

#include <stdio.h>  
int main ()  
{  
    printf ("     inverse number of sequences to \ n");      
    printf ("-by Morewindows (http://blog.csdn.net/MoreWindows)--\n\n");      
      
    const int MAXN = 8;  
    int A[MAXN] = {1, 7, 2, 9, 6, 4, 5, 3};  
          
    int ncount = 0;  
    int I, J;  
    for (i = 0; i < MAXN. i++) for  
        (j = i + 1; j < Maxn; J +)  
            if (A[i] > A[j])  
                ncount++;  
      
    printf ("Reverse order number to:%d\n", ncount);  
}

The results of the operation are as follows:

This method uses a double loop, the time complexity of O (n^2), is a less elegant method. So we try to solve it in other ways.

In the "realization of the five merging sort of the vernacular classical algorithm series", we observe the merging sort-merging sequence (1,3,5) and (2,4):

1. First, take out 1 of the previous series.

2. Then take out the 2 in the back series, obviously! This 2 and the preceding 3,5 can be composed of a reverse number pair that is 3 and 2,5 and 2 are in reverse order pairs.

3. Then take out 3 of the previous series.

4. And then take out the 4 in the back series, and the same is to know that the 4 and the 5 in the preceding series can form a pair of reverse 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.