/** function: Merge 2 sequential arrays, effective to large input: Array First pdata[begin: mid] Array Second pdata[mid+1..end] output:pdata[begin...end] Space required: O (n) **/void merge (int *pdata,int begin,int mid,int end) { int ptemp[8]={0}; int first =begin; int second =mid+1; int itemp=begin //itemp start position cannot start at zero each time, Begin while (first<=mid && second<=end) { if (PData[first] <=pdata[second]) { pTemp[iTemp++]=pData[first++]; }else { pTemp[iTemp++]=pData[second++]; } } //Array to sort No remaining while (first<=mid && second>end) { pTemp[iTemp++]=pData[first++]; } while (Second<=end && first>mid) { pTemp[iTemp++]=pData[second++]; } for (int i=begin;i<=end;i++) { pdata[i]=ptemp[i]; } / / delete&nbSp []ptemp; }/*** function: Non-recursive form Statistics I back door position, than I small number, joined [0--i] statistics finished, I do not care about the back door is some unnecessary premise: I do not have the statistics completed do not disrupt 1 How to do interval step access observing rules according to step search divided into n groups step=1 [0,1], [2,3] ,[4,5][6,7] begin step=2; [40,80] [20,60] begin 0 mid=1 end 3 [40,80] [20,60] ,[41,81] [21,61] begin 4 mid=5 end 7 step=4; Group after a group of may be less than one step of data 2 How to sort a step data **/void merge_sort (int * pdata,int length) { int step=0;//step avoid all sorts, in order to determine whether first I do not fight with everyone int begin=0;//the next split merge array start position int mid=0;//begin-mid,mid-end split 2 arrays to merge int end =0;//pending Sort End position //int temp[length]={0};//cannot request space for temp because length does not know //control Merge Sort Count for (step =1;step<length;step*=2) { cout<< "step==" <<step< < ":" <<endl; //controls a merge sort, How many pairs of numbers need to be merged //<=length must be equal to less than an array of calculations for (begin=0;Gin+2*step) <=length;begin=end+1) { mid=begin+step-1; end=mid+step; if (end>length-1)// Insufficient number of second series { end=length-1; } //cout<< "Begin_mid__end:" <<begin<< "__" <<mid<< "__" <<end<<endl; merge (pdata,begin,mid,end); //showdata (pdata,length); } showdata (pdata,length); &nBsp; } }void testgetmaxsum () { int array[8]={0,5,-2,1,-8,7,6,-3}; showdata (array,8); merge_sort (array,8); //showdata (array,12); }
Output Result:
-
Title Description:
-
In the array of two digits, if the previous number is greater than the number that follows, then these two numbers form an inverse pair. Enter an array to find the total number of reverse pairs in this array.
-
Input:
-
Each test case consists of two lines:
The first line contains an integer n, which represents the number of elements in the array. where 1 <= n <= 10^5.
The second row contains n integers, and each array is of type int.
47 5 6 4
Sample output:
5
"Problem-solving ideas"
1 simulation manual calculation, disadvantage sequence execution
7 3
5 1
6 1
Sum=3+1+1=5
Cons: N*n
2 How to link up above. For example, student scores compare Class A and Class B if Class A first (i) about Class B (i) First place
comparison, you do not need to compare it with other values in B.
Merge sort-non-recursive application