Merge sort for reverse order

Source: Internet
Author: User

Merge sort for reverse order

By MPs

"1" What is reverse order?

for a sequence to be ordered from small to large, if there is Ai,aj and meet Ai>aj and I<j Ai,aj for a set of reverse

"2" How to find the reverse order?

we find that we can force enumeration of i,j and then judge and accumulate the answer, time complexity O (N2)

But for a larger number of topics, the only way to keep the tle →_→

"3" merging sort for reverse order

The definition of reverse pairs (see "1") is a set of reverse pairs in the sequence that should be ordered, then we think of the sort, but because it is to 22 matches, we think of the merge sort

The merge sort roughly reads as follows:

a set of data is divided into a group, and then for each interval from the very large to make an orderly table merge (O (N)), and then back up, the main idea is divided

We found that, when merging, if a group of merge errors occur

(that is, not a direct A and then the B, but an AI in the middle is not in the right place, is replaced by a number in B, it proves that there must be mid-i+1 A reverse order, because the front must be orderly)

So the reverse is very simple for itself, only need to write a merge sort, in the process of merging, if not insert a element, but B, then the answer class plus mid-i+1

"4" Template

1 voidUnion (intLintMid,intR) {2     intI=l,j=mid+1, len=l-1;3      while(I<=mid && j<=R)4     {5         if(A[i]<=a[j]) b[++len]=a[i++];6         Else {7b[++len]=a[j++];8ans+=mid-i+1;9ans=ans%Mod;Ten         } One     } A      while(I<=mid) b[++len]=a[i++]; -      while(j<=r) b[++len]=a[j++]; -      for(i=l;i<=r;i++) a[i]=B[i]; the } -  - voidMerge_sort (intLintR) { -     if(l<R) { +         intMid= (l+r) >>1; - Merge_sort (l,mid); +Merge_sort (mid+1, R); A Union (l,mid,r); at     } - } -  - intMain () { - init (); -Merge_sort (1, n); inprintf"%d", ans); -   return 0; to}

"5" Summary

The time complexity of the algorithm is reduced from O (N2) to O (Nlogn)

Space complexity is raised from O (1) to O (N)

space Change Time

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.