Search for reverse pairs for interview training

Source: Internet
Author: User

At first glance, we can see a ratio from start to end, and then find the reverse order.

It is a pity that the method is simple.

Thoughts:

The idea of analysis is to merge.

However, I thought of dividing 7.5.6, 4 into 7, 5, and 6, 4 arrays to reverse order pairs. As for merging these two arrays containing two elements, I cannot find a good solution.

After reading the book, you can understand that you need to sort the merged sub-arrays and then search for them in the two sorted sub-arrays, starting from the last largest element,

If the maximum element of the current half is greater than that of the lower half, add the reverse order to the total number of elements in the lower half to save the first half and move the pointer.

If the maximum element of the current half is smaller than that of the latter half, the reverse order is not changed, and the maximum element of the latter half is kept, moving the pointer

Now let's run the Merged Code first.

Merge can be said to be a stable O (nlogn) algorithm. Why? Because all are left-side and right-side, the same is always on the left.

#include "stdio.h"#include "string.h"#define MAX 1024#define LARGEST 0x7FFFFFFFvoid mSort(int *data,int s,int m,int d){int left[MAX],right[MAX];int i,j,k;int leftMax,rightMax;for(i=s;i<=m;i++){left[i-s]=data[i];}left[i-s]=LARGEST;for(j=m+1;j<=d;j++){right[j-m-1]=data[j];}right[j-m-1]=LARGEST;i=0;j=0;k=s;for(k=s;k<=d;k++){if(left[i]<=right[j])data[k]=left[i++];elsedata[k]=right[j++];}}void mergeSort(int *data,int s,int end){int num=(s+end)/2;if(s<end){mergeSort(data,s,num);mergeSort(data,num+1,end);mSort(data,s,num,end);}}int main(){int data[] = {5,3,1,7,8,11,4,6,4,5,4};int len = sizeof(data)/sizeof(data[0]);int index=0;mergeSort(data,0,len-1);for(index=0;index<len;index++)printf("%d ",data[index]);return 0;}

After running the merge algorithm, you need to think about how to add a reverse order to the merge algorithm.

#include "stdio.h"#include "string.h"#define MAX 1024#define LARGEST 0x7FFFFFFFint globalReverseNum=0;void mSort(int *data,int s,int m,int d){int left[MAX],right[MAX];int i,j,k;int leftMax,rightMax;for(i=s;i<=m;i++){left[i-s]=data[i];}left[i-s]=LARGEST;for(j=m+1;j<=d;j++){right[j-m-1]=data[j];}right[j-m-1]=LARGEST;i=m-s;j=d-m-1;k=d;while(k>=s){if(i<0 ||j<0)break;if(left[i]>right[j]){globalReverseNum+=(j+1);data[k--]=left[i];i-=1;}else{data[k--]=right[j];j-=1;}}for(;j>=0;j--)data[k--]=right[j];for(;i>=0;i--)data[k--]=left[i];}void mergeSort(int *data,int s,int end){int num=(s+end)/2;if(s<end){mergeSort(data,s,num);mergeSort(data,num+1,end);mSort(data,s,num,end);}}int main(){int data[] = {5,3,1};int len = sizeof(data)/sizeof(data[0]);int index=0;mergeSort(data,0,len-1);for(index=0;index<len;index++)printf("%d ",data[index]);printf("\n%d\n",globalReverseNum);return 0;}

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.