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]