Log an error

Source: Internet
Author: User

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

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.