Two-way Merge Sorting (also called Merge Sorting)

Source: Internet
Author: User

Two-way Merge Sorting (also called Merge Sorting)

The following figure shows the two-way merge process.


The core code of the two-way merge is the merge () function. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + y/fingerprint + signature/Signature/Ox38r91 + signature + tdq2/rK/o7q/qrHZ0ru49sr91 + signature/Signature/VvOTSqrbgMdfWt/ u49r/VvOQ8L3A + release/dfW1vC49rHIvc + jrLDRvc/release/dfpQaG + MKG/v6rKvLTmt8WjrNLAtM7N + release/release + release = "http://www.2cto.com/uploadfile/Collfiles/20140929/20140929091830263.jpg" alt = ">

// The first parameter is the array to be sorted, 2nd parameters are the subscript of the starting element of the first split array // 3rd parameters are the subscript of the last element of the first split array // 4th parameters are the last 1 of the array element subscript void Merge (int *, int p, int q, int r) {int n1, n2, I, j, k, g; n1 = q-p + 1; n2 = r-q; int * L, * R; L = (int *) malloc (sizeof (int) * (n1 + 1); R = (int *) malloc (sizeof (int) * (n2 + 1); L [n1] = 0x7fff; // The number of the last two arrays opened up is set to the maximum value R [n2] = 0x7fff; g = 0; for (I = p; I <= q; I ++) {L [g] = A [I]; g ++;} g = 0; for (I = q + 1; I <= r; I ++) {R [g] = A [I]; g ++ ;} // compare the Left and Right arrays one by one, and write smaller values into the original array j = k = 0; for (I = p; I <= r; I ++) {if (L [j]
 
  
Complete code:

# Include
   
    
Using namespace std; // The first parameter is the array to be sorted, 2nd parameters are the subscript of the starting element of the first split array // 3rd parameters are the subscript of the last element of the first split array // 4th parameters are the last 1 of the array element subscript void Merge (int *, int p, int q, int r) {int n1, n2, I, j, k, g; n1 = q-p + 1; n2 = r-q; int * L, * R; L = (int *) malloc (sizeof (int) * (n1 + 1); R = (int *) malloc (sizeof (int) * (n2 + 1); L [n1] = 0x7fff; // The number of the last two arrays opened up is set to the maximum value R [n2] = 0x7fff; g = 0; for (I = p; I <= q; I ++) {L [g] = A [I]; g ++;} g = 0; for (I = q + 1; I <= r; I ++) {R [g] = A [I]; g ++ ;} // compare the Left and Right arrays one by one, and write smaller values into the original array j = k = 0; for (I = p; I <= r; I ++) {if (L [j]
    
     



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.