Algorithm: recursive and non-recursive form of the merge algorithm
The merge algorithm combines two or more ordered tables into a new ordered table. The principle is: Assuming the initial sequence contains n records, it can be regarded as n ordered subsequences, which are merged in two to obtain [n/2] ordered subsequences, and merged again ...... Repeat until it is merged to an ordered sequence with a length of n. This sorting method is called 2-way merge sorting.
Example 1: recursive two-way merge algorithm
# Define MAXSIZE 4int data [MAXSIZE] = {,};/* function: sort the from array min to max-1 subscript data in order, the final result is to [min]... to [max-1] * input: 1. array to be sorted; 2. sort the ordered array; 3. related Position Setting * output: none */void merge (int from [], int to [], int min, int m, int max) {/* sort the elements on both sides of the from [] array on to [] Until one side "overflows" */int I = min, j = m + 1; while (min <= m & j <= max) {if (from [min] <from [j]) to [I ++] = from [min ++]; elseto [I ++] = from [j ++];}/* Since the subsequence has been sorted, when the surface loop ends, assign the remaining elements on one side that have not been "exceeded" to [] */if (min <= m) for (int k = 0; k <= m-min; k ++) to [I + k] = from [min + k]; if (j <= max) for (int k = 0; k <= max-j; k ++) to [I + k] = from [j + k];}/** function: continuous grouping and merging * input: 1. array to be sorted; 2. sort the ordered array; 3. related Position Setting * output: none */void mSort (int from [], int to [], int min, int max) {if (min = max) // keep grouping until a copy of the original array is copied to [min] = from [max]; else {int m = (min + max)/2; int temp [MAXSIZE]; // create a new array to store the sorted sub-sequence mSort (from, temp, min, m); // The subscript in the middle changes to the maximum subscript, after the temp array is recursive, It is the mSort (from, temp, m + 1, max) to the next megre array; // at this time, the subscript + 1 in the middle changes to the minimum subscript merge (temp, to, min, m, max); // you can start to merge minutes after minute division.} void print (int * data) {for (int I = 0; I <MAXSIZE; I ++) printf ("% d", data [I]); printf ("\ n");} void main (int argc, char * argv []) {print (data); mSort (data, data, 0, MAXSIZE-1); print (data );}Print result:
It can be seen from the program that each group creates an array for storing data temporarily once, which will undoubtedly reduce the performance, so non-recursive merge algorithms can be used. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + PGJyPgo8L3A + CjxwPsq1wP22/fingerprint + cjxwp1_vcd4kphbyzsbjbgfzcz0 = "brush: java;" >#include "stdio. h "# include" malloc. h "# define MAXSIZE 8int data [MAXSIZE] = {,};/** function: Merge and sort, bottom-up, no Recursion, instead, the algorithm uses the bottom-up * input: array to be sorted, number of array elements * output: none */void merge_sort (int * list, int length) {int I, left_min, left_max, right_min, right_max, next; int * tmp = (int *) malloc (sizeof (int) * lengt H); // the final result is to deal with the dynamically allocated array for (I = 1; I <length; I * = 2) // when I = 1, the sequence size of the table to be merged is 1, and so on {/* The number of times the sequence of the current size needs to be merged, change left_min to move */for (left_min = 0; left_min <length-I; left_min = right_max) {right_min = left_max = left_min + I; // After the sequence is split into two halves, the related position is set to right_max = right_min + I; if (right_max> length) right_max = length; next = 0; /* sort the two sequences into tmp until a "overflow". Pay special attention to the moving of right_min */while (left_min <left_max & right_min <Right_max) tmp [next ++] = list [left_min]> list [right_min]? List [right_min ++]: list [left_min ++];/* If the overflow is on the right side, the array to be sorted reads large data from the left end */while (left_min <left_max) list [-- right_min] = list [-- left_max]; /* read the data from the array previously stored in tmp to the array */while (next> 0) list [-- right_min] = tmp [-- next];} free (tmp);} void main () {print (data); merge_sort (data, MAXSIZE); print (data );}
The printed result is similar to the preceding one.
An important idea of Merge Sorting is that the subsequences in the merge are sorted, which is also the key to the algorithm. The time complexity of Merge Sorting is O (nlogn). The performance of processing a large amount of data is far better than that of simple sorting algorithms. The space complexity of non-recursive merge algorithms is smaller than that of recursive merge algorithms, the overall performance is better.