Algorithm-4. Merge sort (top-down)

Source: Internet
Author: User
Tags comparable

1. Basic Ideas

By merging two ordered arrays into a larger, ordered array, people soon invented a simple recursive sorting algorithm based on this operation: merge sort. To sort an array, you can sort it into two halves (recursively), and then merge the results together. You will see that the most appealing nature of the merge sort is that it guarantees that the time required to sort the array of any length n is proportional to the NLOGN, and its main disadvantage is that the extra space required is proportional to n. The simple merge sort is as follows:

Abstract methods for in-situ merging:

A straightforward way to merge is to merge two different ordered arrays into the third array, to implement a simple method, create an array of the appropriate size, and then place the elements from the two input arrays into the array from small to large.

public void Merge (comparable[] A, int lo, int mid, int hi) {int i = lo, j = mid+1;//will A[lo. Hi] Copy to Aux[lo. hi]for (int k = lo; k <= hi; k++) {aux[k] = a[k];} Merge back to A[lo. hi]for (int k = lo; k <= hi; k++) {if (i > Mid) {a[k] = aux[j++];} else if (J > Hi) {a[k] = aux[i++];} else if (less (aux[j], aux[i])) {A[k] = aux[j++];} ELSE{A[K] = aux[i++];}}

The above method will a[lo the sub-array: Mid] and A[mid+1..hi] Merge into an ordered array and store the results in A[lo. Hi]. At the time of merging (the second for loop) 4 conditions are judged: the left half is exhausted (the element with the right half), the right half is exhausted (the element taking the left half), The current element of the right half is less than the current element of the left half (the element that takes the right half) and the current element of the right half is greater than the current element of the left half (the element that takes the left half).

2. Specific algorithms

/** * Top-down merge sort * @author Huazhou * */public class Merge extends Model{private comparable[] aux;//merge required auxiliary array public void SOR T (comparable[] a) {System.out.println ("Merge"), aux = new comparable[a.length];//One-time allocation space sort (A, 0, a.length-1);} A[lo the array: Hi] sort private void sort (comparable[] A, int lo, int hi) {if (Hi <= lo) {return;} int mid = lo + (Hi-lo)/2;sort (A, lo, mid);//The left half is sorted sort (A, mid+1, HI);//The right half is sorted by merge (A, lo, Mid, HI);//merge result}}

This algorithm implements another recursive merging based on the abstraction of in-situ merging, which is also the most typical example of the idea of splitting and treating in the application of efficient algorithm design. This recursive code is the basis for inductive proof that the algorithm can correctly sort the array: if it can sort two sub-arrays, it can sort the entire array by merging two sub-arrays.

To A[lo a pair of arrays: Hi] to sort, divide it into A[lo first. Mid] and A[mid+1..hi] two parts, respectively, by recursive call to sort them separately, and finally merge the ordered sub-arrays into the final sorting results.

To understand the merge sort, it is necessary to study the dynamic behavior of the method call, as shown in the trajectory in. To sort a[0..15], the sort () method calls itself to sort a[0..7], and in which it calls itself to sort a[0..3] and a[0..1]. After A[0] and a[1] are sorted separately, the a[0] and A[1 are finally merged (for simplicity, we omit the call to sort an array of individual elements in the trajectory). The second merge is a[2] and a[3], then a[0..1] and a[2..3], and so on. As you can see from this trajectory, the sort () method actually works by arranging the correct order of multiple merge () method calls.

3. Algorithm Analysis

This code is also the basis for us to analyze the run time of the merge sort. We can also understand the proposition by the tree graph shown. Each node represents a sub-array in which the sort () method is merged by the merge () method. This tree has an n-level. For any k between 0 and N-1, the top-down K-level has a 2k sub-array, and each array has a length of 2n-k, and the merge requires a 2n-k comparison. Therefore, the number of comparisons per layer is 2kx2n-k=2n,n for a total of n2n=nlgn.

proposition: for any array of length n, the top-down merge sort requires a 1/2nlgn to NLGN comparison.

Proof: enables C (N) to indicate the number of comparisons required to sort an array of length n. We have C (0) =c (1) = 0, for n>0, we can get the upper limit of the comparison number by the corresponding inductive relation by the recursive sort () method:

C (N) <=c (└n/2┘) +c (┌n/2┐) +n

The first item on the right is the number of comparisons used to sort the left half of the array, the second is the number of comparisons used to sort the right half of the array, and the third is the number of comparisons used to merge. Because the minimum number of comparisons required for merging is └n/2┘, the lower limit of the number of comparisons is:

C (N) >=c (└n/2┘) +c (┌n/2┐) +└n/2┘

When n is a power of 2 (that is, n=2n) and the equals sign is established, we can get a solution. First, because └n/2┘=┌n/2┐=2n-1, you can get:

C (2n) =2c (2n-1) +2n

Dividing both sides by 2n is possible:

C (2n)/2n=c (2n-1)/2n-1+1

To replace the first item on the right with this formula, you get:

C (2n)/2n=c (2n-2)/2n-2+1+1

Repeat n-1 the previous step to get:

C (2n)/2n=c (/20+n)

By multiplying both sides by 2n, you can solve the following:

C (N) =c (2n) =n2n=nlgn

For General N, it is more complicated to get the exact value. However, it is not difficult to use the same method for the upper and lower bounds inequalities of the number of comparisons to prove the conclusion of any n mentioned above. This conclusion is valid for any input value and order.

proposition: for any array of length n, the top-down merge sort requires access to the array 6NlgN times.

Proof: Each merge needs to access the array 6N times (2N times to copy, 2N Times used to move the ordered elements back, in addition to a maximum of 2N), according to the above proposition can get the result of this proposition.

4. Summary

Two propositions tell us that the time required to merge a sort is proportional to the NLGN. This is not the same as the primary sorting method described earlier, which shows that we can sort a large array by just a few times the number of log factors in the entire array. You can use the merge sort to handle millions of or even larger arrays, which are not available for insertion sorting or sorting. The main disadvantage of merge sorting is that the extra space used by the secondary array is proportional to the size of N.

" Source Download "

Algorithm-4. Merge sort (top-down)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.