Merge Sorting of data structures and algorithms 14

Source: Internet
Author: User

Merge Sorting of data structures and algorithms 14

The center of the merge algorithm is to merge two sorted arrays. Merge two ordered arrays A and B to generate the third array C. array C contains all the data items of array A and B, and arrange them in array C in an orderly manner. First, let's take a look at the merge process and then see how it is used in sorting.

Suppose there are two ordered arrays, and do not have the same size. Set array A to have four data items and array B to have six data items merged into array C. At the beginning, group C has 10 buckets, as shown in the merging process:

The idea of merging and sorting is to divide an array into two halves and sort each half. Then, use the merge method to merge the two halves of the array into an ordered array. Each half of the split uses recursion and sorts again until the obtained sub-array contains only one data item. As mentioned above, merging and sorting requires an additional space equal to the sum of the two arrays of AB. If the initial array is almost full of memory, then merging and sorting will not work.

The idea of merging and sorting is very 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 (lowerBound = upperBound) {return ;} else {int mid = (lowerBound + upperBound)/2; recMergeSort (source, workSpace, lowerBound, mid); // recMergeSort (source, workSpace, mid + 1, upperBound); // merge (source, workSpace, lowerBound, mid + 1, upperBound) on the Right; // merge} private void merge (int [], 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) {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 ++] ;}for (j = 0; j <n; j ++) {a [lowerBound + j] = workSpace [j];}

 

Algorithm analysis: The Run Time of Merge Sorting is the worst, the best and the average are O (NlogN), but it requires additional storage space, which may be limited on some memory-intensive machines. The merge algorithm consists of two parts: Split and merge. For each part, if we use binary search, the time is O (NlogN ), in the final merge, the time is O (N), so the total time is O (NlogN ). The space complexity is O (N ).

There are so many words for merging and sorting. If there are any errors, please leave a message to correct them ~

_____________________________________________________________________________________________________________________________________________________

 

Related Article

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.