I was doing chemistry last night ... The result is nothing to start reading to buy the "Introduction to the algorithm" ... It looks like the end of the semester is about to hang up.
Even read 44 pages of acid cool ... A simple look at the algorithm time complexity of the analysis, after all, is not very important ... (Not only I'm joking but I don't want to see it), O (NLGN)
So somehow I understand the calculation of merge sort and reverse order ... emmm is still the Bible. That's a very detailed story.
Write the code according to the book: "Merge Sort"
1#include <iostream>2 using namespacestd;3 Const intMAXN =2<< -;4 inta[ -];5 intb[Wuyi],c[Wuyi];6 voidPrintint*,int);7 voidMERGE (intBintLintMidintR)8 {9 intn1=mid-l+1;Ten intn2=r-mid; One for(intI=1; i<=n1;i++) b[i]=a[l+i-1]; A print (B,N1); - for(intI=1; i<=n2;i++) c[i]=a[mid+i]; - print (C,N2); theb[n1+1]=MAXN; -c[n2+1]=MAXN; - intI=1, j=1; - for(intk=l;k<=r;k++){ + if(b[i]<=C[j]) { -a[k]=B[i]; +i++; A } at Else{ -a[k]=C[j]; -J + +; - } - } - } in voidMerge_sort (int*a,intLintR) - { to if(L>=R)return; + intMid= (L+R)/2; - Merge_sort (a,l,mid); theMerge_sort (a,mid+1, R); * MERGE (a,l,mid,r); $ }Panax Notoginseng voidPrintint*a,intN) - { the for(intI=1; i<=n;i++){ + if(I-1) cout<<" "; Acout<<A[i]; the } +cout<<Endl; - } $ intMain () $ { -cout<<maxn<<Endl; - intN;cin>>N; the for(intI=1; i<=n;i++){ -Cin>>A[i];Wuyi } theMerge_sort (A,1, n); - print (a,n); Wu return 0; -}
Because it is a test, the array is not too large, where array A is the entire sequence, B is the left sequence, and C is the right sequence
And then there's the regular one, just the part of the division that writes the merge function alone.
Also read the Book of exercise 2.4 to tell the truth at first did not do it and then the Internet downloaded an answer ...
After reading the feeling good, a little change also wrote: "Reverse order"
1#include <iostream>2 using namespacestd;3 Const intMAXN =2<< -;4 inta[ -],cnt=0;5 intb[Wuyi],c[Wuyi];6 voidPrintint*,int);7 voidMERGE (intBintLintMidintR)8 {9 intn1=mid-l+1;Ten intn2=r-mid; One for(intI=1; i<=n1;i++) b[i]=a[l+i-1]; A //print (B,N1); - for(intI=1; i<=n2;i++) c[i]=a[mid+i]; - //print (C,N2); theb[n1+1]=MAXN; -c[n2+1]=MAXN; - intI=1, j=1; - //int cnt=0; + BOOLCounter=false; - for(intk=l;k<=r;k++){ + if(counter==false&&C[j]<B[i]) { Acnt=cnt+n1-i+1; atCounter=true; - } - if(b[i]<=B[j]) { -a[k]=B[i]; -i++; - } in Else{ -a[k]=C[j]; toJ + +; +Counter=false; - } the } * //return CNT; $ }Panax Notoginseng voidMerge_sort (int*a,intLintR) - { the if(L>=R)return; + intMid= (L+R)/2; A Merge_sort (a,l,mid); theMerge_sort (a,mid+1, R); + MERGE (a,l,mid,r); - } $ voidPrintint*a,intN) $ { - for(intI=1; i<=n;i++){ - if(I-1) cout<<" "; thecout<<A[i]; - }Wuyicout<<Endl; the } - intMain () Wu { -cout<<maxn<<Endl; About intN;cin>>N; $ for(intI=1; i<=n;i++){ -Cin>>A[i]; - } -Merge_sort (A,1, n); A print (a,n); +cout<<cnt<<Endl; the return 0; -}
Yes really very lazy is to merge sort to change, even variable name has not changed, even directly with the global variable ...
In fact, the division of the main part is to set a bool counter to determine whether the number includes c[j] in reverse order, when this c[j] is the first occurrence, and b than c[j] Large value is also the first occurrence, the counter to false, and self-increasing CNT, This will count an inverse pair.
Then is the merger, very simple merge_sort (), because this reverse order to the algorithm is only in the merge sort based on a self-increment operation, so the complexity is O (NLGN). Probably
The pseudo-code answer to the introduction of the algorithm is not put out, because it is in English and it is using a parameter to pass it.
The road is long ... This is the second chapter of the algorithm introduction ...
Merge sort in reverse order