Algorithm Analysis (2): Merge sort-found a bug on a wiki

Source: Internet
Author: User

The core idea of merge sorting is to merge the already sequenced A[p~q], and A[q~r] (where A is an array, p, Q, r array subscript) into a single sorted set of A[p~r]. The implementation code is as follows:

 void  _merge (int  * Input,int  * output, int  Low, int  Mid, Span style= "color: #0000ff;" >int   "{ int  i = low;     int  J = mid;         while  (I < mid && J < high) { Output[low  + +] = Input[i] <= input[j]?    input[i++]: Input[j++];    } copy ( &input[i], &input[mid], &output[low]); Copy ( &input[j], &input[high], &output[low]);}  

But to arrange an array, you also need to decompose the array. I used the method above the wiki, but found that there was an error. First, I changed the code:

voidMerge (intAintN) {    int*copy_a =A; int*b =New int[n]; intStart =0;  for(intSEG =1; SEG < n; SEG + =seg) {         for(start =0; Start <= n-seg; Start + =2*seg) {_merge_ascending (A, B, start, start+ seg, min (start +2*seg, n)); }        if(Start <N) {copy (&a[start], &a[n], &B[start]);    } swap (A, b); }    if(A! =copy_a) {Copy (A,&A[n], b); b=A; }    Delete[] b;}

The above code completes the process of merging all elements 22, entering into array B, then A/b interchange, merging in 44, and so on until all elements are sorted.

Here's the code for the wiki:

voidMerge_sort (intArr[],intLen) {    int* A =arr; int* B =New int[Len];  for(intSEG =1; SEG < Len; SEG + =seg) {         for(intStart =0; Start + seg <= len; Start + = seg +seg) _merge_sort (A, B, start, start+ seg, min (start + seg +seg, Len));    Swap (A, b); }    if(A! =arr) {Copy (A,&A[len], b); b=A; }    Delete[] b;}

As you can see, the wiki's code does not output the elements that are not merged to the B array after completing a round of merges.

To do this, I also tested the wiki's code. The test method is: the number of array elements varies from 1~1000, and each element uses a random number. Each time you compare the insertion sort of the previous section with the code for the wiki, if different, record the length of the array.

Vector<clock_t>dt1; Vector<clock_t>DT2; Vector<int>seq; Vector<int>Error_count; Vector<int>Error_wiki;  for(inti =1; i < MAX; i++) {seq.clear ();  for(intj =0; J < I; J + +) {Seq.push_back (rand ()% +); }        //Show_array (arr, i);        int*ARR1 =New int[i]; int*ARR2 =New int[i]; int*ARR3 =New int[i];        V2A (seq, arr1);        V2A (seq, arr2);        V2A (seq, ARR3); //AscendingAuto T1 =Getcyclecount ();        Insert_ascending (arr1, i); Auto T2=Getcyclecount ();        Merge_ascending (arr2, i); auto T3=Getcyclecount ();        Merge_ascending_wiki (ARR3, i); //Compare        if(!Compare_array (arr1, ARR2, i)) error_count.push_back (i); Else            if(!Compare_array (arr1, ARR3, i)) error_wiki.push_back (i); //decendingauto T4 =Getcyclecount ();        Insert_decending (arr1, i); Auto T5=Getcyclecount ();        Merge_decending (arr2, i); Auto T6=Getcyclecount (); Dt1.push_back (((T2-T1) + (T5-T4))/2); Dt2.push_back (((T3-T2) + (T6-T5))/2);        Merge_decending_wiki (ARR3, i); //Compare        if(!Compare_array (arr1, ARR2, i)) Error_count.push_back (-i); Else            if(!Compare_array (arr1, ARR3, i)) Error_wiki.push_back (-i); } cout<<"Error Count:"<< error_count.size () <<Endl;  for(Auto &it:error_count) cout<< it <<"\ t"; cout<<Endl; cout<<"Wiki Error Count:"<< error_wiki.size () <<Endl;  for(Auto &it:error_wiki) cout<< it <<"\ t"; cout<< Endl;

The above code also adds a comparison of the time taken to calculate the order of the insertion sort and merge sort to be sorted by the number of different elements. The functions to get CPU time are:

Inline unsigned __int64 getcyclecount () {    0x0F    0x31}

Compare the results to

Algorithm Analysis (2): Merge sort-found a bug on a wiki

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.