Merge Sorting
I. The efficiency of Merge Sorting is second only to the stable Sorting Algorithm of fast sorting. Its time complexity is O (nlog2n ). When we merge and sort n elements at a time, the number of merging operations is about log2n, and the number of comparison times of each two-way merging and sorting element is about n-1.
Ii. basic idea of merging and sorting:
Merging and sorting combines elements in an array sequence as a small sequence until all elements are merged, the sorting operation is successfully completed.
Iii. Principles:
For example, let's set n to 9, which is better, for example, an array like a [9] = {, 8, 6, 7, 9:
Original array:
1 2 5 4 3 8 6 7 9
First merge:
[1 2] [4 5] [3 8] [6 7] [9]
The second merge:
[1 2 4 5] [3 6 7 8] [9]
The third merge:
[1 2 3 4 5 6 7 8] [9]
The fourth merge:
[1 2 3 4 5 6 7 8 9]
Operation Code for merging:
For (I = L1, j = L2; I <= u1 & j <= u2;) {if (a [I] <= a [j]) {A-> swap [pos] = a [I]; I ++; pos ++;} else {A-> swap [pos] = a [j]; j ++; pos ++ ;}} // what we have to do now has been merged. // at this time, we need to merge the while (I <= u1) that is not merged in the array sequence) {A-> swap [pos] = a [I]; I ++; pos ++;} while (j <= u2) {A-> swap [pos] = a [j]; j ++; pos ++ ;}// the two array sequences have been completely merged, in addition, this is an ordered sequence // L1 = u2 + 1 L1 = u2 + 1; // here we store only one group in swap for (int I = L1; I
Swap [I] = a [I];}
Core code for one merge execution:
/*** Merge and sort an execution * @ param int a [] is the received array * @ param int lenghth1 the length of the received array * @ param int swap [], * @ param int leng2's array * @ return none */void Merge (int a [], int length1, array * A, int leng22. {// After receiving the Array, We need to merge it and sort it at A time. At this time, we define two "Pointers" int u1, u2; // This is the two easy-to-scan pointers // at this time, you need to define the lower bounds of the two array sequences, int L1 and L2; // We can know that the lower bound of the first array sequence is 0L1 = 0; // set the pos value in swap to save the number of successfully merged values, because pos is for this entire cycle, it can only be placed outside the cycle int pos = 0; while (L1 + leng22.
Swap [pos] = a [I]; * I ++; * pos ++; *} else {* A-> swap [pos] = a [j]; * j ++; * pos ++; *} */for (;) {if (I <= u1 & j <= u2) {if (a [I] <= a [j]) {A-> swap [pos] = a [I]; I ++; pos ++ ;} else {A-> swap [pos] = a [j]; j ++; pos ++ ;}} else {break ;}} // what we have to do now has been merged. // at this time, we need to merge the while (I <= u1) that is not merged in the array sequence) {A-> swap [pos] = a [I]; I ++; pos ++;} while (j <= u2) {A-> swap [pos] = a [j]; j ++; pos ++ ;}// the two array sequences have been completely merged, in addition, this is an ordered sequence // L1 = u2 + 1 L1 = u2 + 1;} // cout <"L1" <
Swap [I]... I = "<
Swap [I] = a [I] ;}// this merge operation is complete/* for (int I = 0; I
Swap [I]... I = "<
Code segment for Merge Sorting:
/*** Merge sort ** @ param int a [] array sequence with merge * @ param int length1 is the length of the array sequence with merge * @ return none */void Merge_sort (int a [], int length1) {// we know that we use the array length as 1 to start merging int lengh2 = 1; // here we need to apply for A swap space as the swap space Array * A = NULL; if (! = NULL) {free (A);} A = (Array *) malloc (sizeof (Array) * length1); if (! = NULL) {cout <"Space Application successful! "<
Swap [I]; // Save the merged array sequence back to a [I]} // you need to change the length of the new array sequence after merging. lengh2 = 2 * leng22 ;} // remember to release the free space ();}
All code:
/*** Merge and sort is to divide an array into several arrays and then merge them in pairs until the last array is merged * @ author * @ version 2014.6.15 */# include
# Include
# Include
# Define maxSize 100 using namespace std; typedef int DataType; typedef struct {DataType swap [maxSize];} Array; /*** merge and sort an execution * @ param int a [] is the received array * @ param int lenghth1 the length of the received array * @ param int swap [], * @ param int leng2's array * @ return none */void Merge (int a [], int length1, array * A, int leng22. {// After receiving the Array, We need to merge it and sort it at A time. At this time, we define two "Pointers" int u1, u2; // This is the two easy-to-scan pointers // at this time, you need to define the lower bounds of the two array sequences, int L1 and L2; // We can know that the lower bound of the first array sequence is 0L1 = 0; // set the pos value in swap to save the number of successfully merged values, because pos is for this entire cycle, it can only be placed outside the cycle int pos = 0; while (L1 + leng22.
Swap [pos] = a [I]; * I ++; * pos ++; *} else {* A-> swap [pos] = a [j]; * j ++; * pos ++; *} */for (;) {if (I <= u1 & j <= u2) {if (a [I] <= a [j]) {A-> swap [pos] = a [I]; I ++; pos ++ ;} else {A-> swap [pos] = a [j]; j ++; pos ++ ;}} else {break ;}} // what we have to do now has been merged. // at this time, we need to merge the while (I <= u1) that is not merged in the array sequence) {A-> swap [pos] = a [I]; I ++; pos ++;} while (j <= u2) {A-> swap [pos] = a [j]; j ++; pos ++ ;}// the two array sequences have been completely merged, in addition, this is an ordered sequence // L1 = u2 + 1 L1 = u2 + 1;} // cout <"L1" <
Swap [I]... I = "<
Swap [I] = a [I] ;}// this merge operation is complete/* for (int I = 0; I
Swap [I]... I = "<
Swap [I]; // Save the merged array sequence back to a [I]} // you need to change the length of the new array sequence after merging. lengh2 = 2 * leng22 ;} // remember to release free (A);}/*** at last, an output function is required to output the array sequence out_put () * @ param int a [] indicates accepting the address of this array * @ param int length indicates the length of this array * @ return none */void out_put (int a [], int length) {for (int I = 0; I <length; I ++) {cout <"no." <