Merge sort for reverse order

Source: Internet
Author: User

We know that the most typical method for reverse order is the tree array, but there is another way to do this is Merge_sort (), which is the merge sort.

In fact, the number of exchanges in the merge sort is the inverse of the array, for what?

We can consider this:

The merge sort is to divide the series A[l,h] into two halves a[l,mid] and a[mid+1,h], respectively, and then combine the two halves together.

In the process of merging (set L<=i<=mid,mid+1<=j<=h), when A[i]<=a[j], no reverse order is generated; when A[i]>a[j], the

The first half of the number is larger than a[i] larger than a[j], will a[j] in front of a[i], reverse order number to add mid+1-i. As a result, you can merge

Calculates the number of reverse orders in the merge process in the sort.

Title: http://poj.org/problem?id=1804

Test Instructions: given a sequence of a[], it is allowed to exchange only two adjacent digits at a time, at least how many times it is exchanged to turn it into a non-degradation sequence.

1#include <iostream>2#include <string.h>3#include <stdio.h>4 5 using namespacestd;6 Const intN =1005;7 8 intA[n],tmp[n];9 intans;Ten  One voidMerge (intLintMintR) A { -     inti =l; -     intj = m +1; the     intK =l; In particular, K is started from L (letter) and not 0; -      while(I <= m && J <=R) -     { -         if(A[i] >A[j]) +         { -tmp[k++] = a[j++]; +Ans + = m-i +1; A         } at         Else -         { -tmp[k++] = a[i++]; -         } -     } -      while(i <= m) tmp[k++] = a[i++]; in      while(J <= r) tmp[k++] = a[j++]; -      for(inti=l;i<=r;i++)//must remember to copy the TMP element to a when the merge is complete toA[i] =Tmp[i]; + } -  the voidMerge_sort (intLintR) * { $     if(L <R)Panax Notoginseng     { -         intm = (L + r) >>1; the Merge_sort (l,m); +Merge_sort (m+1, R); A Merge (l,m,r); the     } + } -  $ intMain () $ { -     intn,t,tt=1; -scanf"%d",&T); the      while(t--) -     {Wuyiscanf"%d",&n); the          for(intI=0; i<n;i++) -scanf"%d",&a[i]); WuAns =0; -Merge_sort (0, N-1); Aboutprintf"Scenario #%d:\n%d\n\n", tt++, ans); $     } -     return 0; -}

Transferred from: http://blog.csdn.net/acdreamers/article/details/16849761

Merge sort for reverse order

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.