Merge sort is an effective sorting algorithm based on merge operations. This algorithm is a very typical application using the divide and conquer method (Divide and Conquer).
Merge sorting method is to merge two (or more) ordered lists into a new ordered list, that is, the sequence to be sorted is divided into several subsequences, and each subsequence is ordered. Then merge the ordered subsequences into the overall ordered sequence.
Merge sort is an effective sorting algorithm based on merge operations. This algorithm is a very typical application using the divide and conquer method (Divide and Conquer). Merge the already ordered subsequences to obtain a completely ordered sequence; that is, first make each subsequence orderly, and then make the subsequence segments orderly. If two ordered lists are merged into one ordered list, it is called a 2-way merge.
The process of merge operation is as follows:
1. Apply for space so that its size is the sum of the two sorted sequences. This space is used to store the merged sequence
2. Set two pointers, the initial positions of which are the two sorted sequences. Starting position
3. Compare the elements pointed to by the two pointers, select a relatively small element and put it into the merge space, and move the pointer to the next position
4. Repeat step 3 until a certain pointer reaches the end of the sequence
5. Copy all remaining elements of the other sequence directly to the end of the merged sequence
Example 1: