Recursive Implementation (function recursive call) -- top-down
1/** 2 * merge_sort: Recursive Implementation of Merge Sorting 3 * Note: the Merge Sorting Algorithm 4 * in the introduction to algorithms splits the set to be sorted into two, 5 * until there is one element left in the sorting set, and then combine two sorted arrays 6 * t (n) = O (nlgn) 7 **/8 # include <stdio. h> 9 # define Len 810 11 // merge 12 Void Merge (int A [], int start, int mid, int end) 13 {14 int n1 = mid-start + 1; 15 int n2 = end-mid; 16 int left [N1], right [n2]; 17 int I, J, K; 18 19 for (I = 0; I <N1; I ++)/* left holds a [start .. mid] */20 left [I] = A [start + I]; 21 for (j = 0; j <N2; j ++) /* right holds a [Mid + 1 .. end] */22 right [J] = A [Mid + 1 + J]; 23 24 I = J = 0; 25 k = start; 26 while (I <N1 & J <N2) 27 if (left [I] <right [J]) 28 A [k ++] = left [I ++]; 29 else30 A [k ++] = right [J ++]; 31 32 While (I <N1) /* left [] is not exhausted */33 A [k ++] = left [I ++]; 34 while (j <N2) /* right [] is not exhausted */35 A [k ++] = right [J ++]; 36} 37 38 // merge_sort (): sort first, merge 39 void merge_sort (int A [], int start, int end) 40 {41 int mid; 42 if (start <End) 43 {44 mid = (start + end) /2; 45 printf ("sort (% d-% d, % d-% d) % d \ n ", 46 start, mid, Mid + 1, end, 47 A [0], A [1], a [2], a [3], a [4], a [5], a [6], a [7]); 48 49 // decomposition + solution: Divide + conquer50 merge_sort (A, start, mid); // recursively divides the left half edge array of the original array [start... mid] 51 merge_sort (A, Mid + 1, end); // recursively divides array [Mid + 1... end] 52 // merge: combine53 Merge (A, start, mid, end); // merge 54 55 printf ("Merge (% d-% d, % d-% d) to % d \ n ", 56 start, mid, Mid + 1, end, 57 A [0], a [1], a [2], a [3], a [4], a [5], a [6], A [7]); 58} 59} 60 61 int main (void) 62 {63 int A [Len] = {5, 2, 4, 7, 1, 3, 2, 6}; 64 merge_sort (A, 0, LEN-1); 65 66 return 0; 67}
Non-recursion (iteration, loop expansion) -- bottom-up
1/** 2 * merge_sort: Non-Recursive Implementation -- iteration 3 * Non-recursive idea: pairing Adjacent Elements in the array. Sort them by using the Merge function, 4 * to form a sorted sub-array segment with a length of 2 in N/2 groups, and then sort them into sub-array segments with a length of 4, 5 * continue until the entire array is sorted. 6 **/7 8 # include <stdio. h> 9 # include <stdlib. h> 10 # define Len 811 12 // merge_sort (): Non-Recursive Implementation-bottom-up 13 // divide the original array into left [min... max] And right [min... max] two parts: 14 void merge_sort (int * List, int length) 15 {16 int I, left_min, left_max, right_min, right_max, next; 17 int * TMP = (int *) malloc (sizeof (INT) * length); 18 19 if (TMP = NULL) 20 {21 fputs ("error: out of memory \ n", stderr ); 22 abort (); 23} 24 25 for (I = 1; I <length; I * = 2) // I is the step size, 1, 2, 4, 8 ...... 26 {27 for (left_min = 0; left_min <length-I; left_min = right_max) 28 {29 right_min = left_max = left_min + I; 30 right_max = left_max + I; 31 32 If (right_max> length) 33 right_max = length; 34 35 next = 0; 36 while (left_min <left_max & right_min <right_max) 37 TMP [next ++] = list [left_min]> list [right_min]? List [right_min ++]: list [left_min ++]; 38 39 while (left_min <left_max) 40 list [-- right_min] = list [-- left_max]; 41 42 while (next> 0) 43 list [-- right_min] = TMP [-- next]; 44} 45} 46 47 free (TMP ); 48 49} 50 51 52 int main (void) 53 {54 int A [Len] = {5, 2, 4, 7, 1, 3, 2, 6 }; 55 merge_sort (A, Len); 56 57 // print array58 int I; 59 for (I = 0; I <Len; I ++) 60 printf ("% d ", A [I]); 61 62 Return 0; 63}