Merge sort of data structure and algorithm 14

Source: Internet
Author: User

This article for Bo Master original article, reprint please indicate source : http://blog.csdn.net/eson_15/article/details/51193139


The center of the merge algorithm is the merging of two already ordered arrays. Merging two ordered arrays A and b produces a third array, C, which contains all the data items for arrays A and b, and arranges them in an orderly manner in array C. First, let's look at the merging process and see how it's used in the sort.

Suppose there are two ordered arrays, which do not require the same size. Array A has 4 data items, array B has 6 data items, they are to be merged into the array C, at the beginning of the array C has 10 storage space, the merge process as shown:


The idea of a merge sort is to divide an array into two halves and sort each half. The merge method is then used to merge the two halves of the array into an ordered array. Each half of the split is recursively divided and sorted again until the resulting sub-array contains only one data item. As mentioned above, the merge sort requires an extra space equal to the sum of the AB two arrays, and if the initial array is almost covered with the entire memory, then the merge sort will not work.

The idea of merging sort is simple, let's take a look at the specific implementation:

public void MergeSort (int[] source) {    int[] WorkSpace = new int[source.length];     Recmergesort (source,workspace, 0, source.length-1);} private void Recmergesort (int[] source, int[] workSpace, int lowerbound, int upperbound) {    if (Lowerboun D = = upperbound) {        return;    }    else {         int mid = (lowerbound + upperbound)/2;        recmergesor T (source, WorkSpace, lowerbound, mid); Left row         recmergesort (source, WorkSpace, mid+1, upperbound); Right row         merge (source, WorkSpace, lowerbound, mid+1, upperbound);//merge    & nbsp;}} private void Merge (int[] A, int[] workSpace, int lowptr, int highptr, int upperbound) {    int j = 0;     int lowerbound = lowptr;    int mid = highptr-1;     int n = upperbound-lowerbound + 1;    while (lowptr <= mid && highptr <= upperbound) {&nbsp ;       if (A[lowptr] < a[highptr]) {            workspace[j++] = a[lowptr++];        }        else {            workspace[j++] = a[highptr++];         }    }    while (lowptr <= mid) {         Workspace[j++] = a[lowptr++];    }        while (highptr <= upperbound ) {        workspace[j++] = a[highptr++];    }     & nbsp  for (j = 0; J < N; j + +) {        a[lowerbound + j] = workspace[j];   &nbsp ;}}

Algorithmic analysis: The worst, best, and average run time of a merge sort is O (Nlogn), but it requires additional storage space, which can be limited on some memory-intensive machines. The merging algorithm consists of two parts: Split and merge, for each part, if we use binary search, Time is O (Nlogn), Time is O (N) at the last merge, so the total time is O (Nlogn). The spatial complexity is O (N).

Merge sort to write so much, if there is a mistake, welcome message ~

___________________________________________________________________________________________________________ __________________________________________

-----willing to share and progress together!

-----More articles please see: http://blog.csdn.net/eson_15

Merge sort of data structure and algorithm 14

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.