Merge sort from zero-start learning algorithm [1] (2.2 merge sort)

Source: Internet
Author: User

The idea of merge sorting is to merge each contiguous two digits of a sequence (merge) to form a floor (N/2) sequence, after which each sequence contains two elements that merge the above sequence again, forming a floor (N/4) sequence, each containing four elements repeating step 2, Until all the elements have been sorted.

The following code is a single-merge function that can be used as a template in your code.

PS: Wherein the preceding paragraph is p-bit to the Q-bit, the latter section is the q+1 bit to the R-position.

1 voidMerge (intPintQintR) {2     intN1 = Q-p +1;3     intN2 = R-Q;4     Long LongL[N1],R[N2];5      for(inti =0; i < n1;i++) L[i] = a[i+p];6      for(inti =0; i < n2;i++) R[i] = a[i+q+1];7L[N1] =100000000; R[N2] =100000000;8     inti =0;intj =0;9      for(intK = p;k <= r;k++){Ten             if(L[i] < r[j]) A[k] = l[i++]; One             ElseA[k] = r[j++]; A     } -}

Merge sort from zero-start learning algorithm [1] (2.2 merge sort)

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.