In doing Tsinghua OJ on a problem, wrote a merge sort, the code is as follows:
1 voidMergeSort (intX[],intY[],intLointhi)2 {3 /*4 * Merge sort function, x[] as standard sort5 */6 7 if(Lo <hi)8 {9 intMi = (lo + hi) >>1 ;Ten One mergesort (x, Y, lo, MI); AMergeSort (x, Y, mi+1, HI); - Merge (x, Y, lo, MI, hi); - } the } - - - voidMerge (intX[],intY[],intLointMiinthi) + { - /* + * Merge operation, subordinate to MergeSort function A */ at - intlength_1 = Mi-lo +1, length_2 = Hi-(mi +1) +1; - - inta[length_1], b[length_2], c[length_1], d[length_2]; - - inti =0, j =0; in for(i =0; i < length_1; ++i) {C[i] = X[lo+i]; A[i] = y[lo+i]; } - for(j =0; J < length_2; ++J) {D[j] = x[mi+1+J]; B[J] = y[mi+1+J]; } to +I=0, j=0;intt=Lo; - while((I < length_1) && (J <length_2)) the { * if(C[i]<d[j]) {y[t++] = a[i++]; x[t-1] = c[i-1];} $ Else{y[t++] = b[j++]; x[t-1] = d[j-1];}Panax Notoginseng } - the while(i<length_1) {y[t++] = a[i++]; x[t-1] = c[i-1];} + while(j<length_2) {y[t++] = b[j++]; x[t-1] = d[j-1];} A}
The code went through the previous question on the OJ, but the problem was found in the current topic. Once you use this code for a sort operation, the result will be incorrect if you sort again.
Example: a[],b[],c[],d[] is an array of four test instructions, with the following code:
00, N);
compile with sublime + GCC:
A, B is sorted correctly, but c,d errors occur.
Compile with vs2013:
A, B, C, D are sorted incorrectly.
Log an error