1. Preface
This is the fourth article on the classic sorting algorithm series, which resolves the Merge Sorting.
For more information about sorting algorithms, see:
Classic sorting algorithms: 1. Deep analysis from simple sorting to heap sorting
Typical sorting algorithms: 2. Analysis of insertion sorting and Performance Comparison
Classic sorting algorithms: 3. Bubble Sorting & quick sorting
Classic sorting algorithms: iv. Merge Sorting
Typical sorting algorithms: 5. linear time sorting (counting, base, and bucket sorting)
Typical sorting algorithms: 6. Summary of various sorting algorithms
Note: For ease of description, a [0] is not considered in this article and the source code. The default subscript starts from 1.
2. Merge and sort 2.1
Merge Sorting is another sort algorithm. It is an algorithm based on the "divide and conquer" policy. Merge Sorting algorithms are typical grouping algorithms. For large-scale problems, they can be divided into several simple problems that are easy to solve. Finally, they can be merged to form a solution to the initial problem. For detailed sorting procedures, see data structure or introduction to algorithms.
Code 2.2
# Include <stdio. h> # include <stdlib. h> # define infinite 1000 // merge the two sequences and separate the arrays from the mid. // pair a [start... mid] And a [start + 1... end] merge void Merge (int * a, int start, int mid, int end) {int I, J, K; // apply for an auxiliary array int * array1 = (int *) malloc (sizeof (INT) * (mid-start + 2); int * array2 = (int *) malloc (sizeof (INT) * (end-Mid + 1); // separate a from mid and assign them to the array for (I = 0; I <mid-start + 1; I ++) * (array1 + I) = A [start + I]; * (array1 + I) = infinite; // serve as the sentry for (I = 0; I <End-mid; I ++) * (array2 + I) = A [I + Mid + 1]; * (array2 + I) = infinite; // orderly merge to array a I = J = 0; For (k = start; k <= end; k ++) {If (* (array1 + I)> * (array2 + J) {A [k] = * (array2 + J); j ++ ;} else {A [k] = * (array1 + I); I ++ ;}} free (array1); free (array2 );} // merge and sort void mergesort (int * a, int start, int end) {int mid = (start + end)/2; If (start <End) {// break down mergesort (A, start, mid); mergesort (A, Mid + 1, end); // merge Merge (A, start, mid, end );}} void main () {int I; int A [7] = {, 9, 1, 2}; // do not consider a [0] mergesort (); for (I = 1; I <= 6; I ++) printf ("%-4D", a [I]); printf ("\ n ");}
2.3 Efficiency Analysis
It can be said that Merge Sorting is a complex sort, especially for those who do not understand the basic idea of the division and Control Law, it may be difficult to understand. Total time = Decomposition Time + solution time + merge Time. The decomposition time is to break down a sequence to be sorted into two sequences. The time is a constant and the time complexity is O (1 ). the time for solving the problem is two recursion models. We divide a problem with a scale of N into two subproblems with a scale of n/2, with a time of 2 TB (n/2 ). the merging time complexity is O (n ). Total time t (n) = 2 T (n/2) + O (n ). this recursive formula can be solved using a recursive tree, whose solution is O (nlogn ). in addition, the Merge Sorting time complexity is O (nlogn) in the worst, best, and average cases ). from the merge process, we can see that the Merge Sorting is stable.
Use the recursive tree method to solve the recursive T (n) = 2 T (n/2) + O (n): assume that the constant C is used to solve the final subproblem, for N to-be-sorted records, the overall problem scale is CN.
From this recursive tree, we can see that the first layer of time price is CN, and the second layer of time price is cN/2 + cN/2 = cn ..... the cost for each layer is CN, with a total of logn + 1 layers. Therefore, the total time cost is Cn * (logn + 1). the time complexity is O (nlogn ).
3. Appendix
Reference books: Introduction to Algorithms