Merge Sorting Algorithm and merge algorithm

Source: Internet
Author: User

Merge Sorting Algorithm and merge algorithm

Merging and sorting is to divide unsorted arrays into two arrays in half. The divided array contains only half of the original array. Then, we continue to divide the two arrays. This operation continues until there is only one element in the divided array, and then merge and sort the divided arrays. Merge the two arrays that have been divided into an ordered array until they are finally merged into an array that contains all elements. The following figure shows the process of merging and sorting.

Suppose there is an unordered array: {,}. The following is the division process of the array. First, the array is divided into {} and {} arrays. Then, after dividing the two arrays, we get four arrays: {3}, {2}, {4}, and {1}. The Division is complete.
 

Next, we will merge the arrays {3} and {2} into an ordered array {2, 3}. Similarly, we will perform the same operation on 4, 1, {} is obtained, and then the two sorted arrays are merged, and finally merged into {1, 2, 3, 4.

 

The Merge Sorting Algorithm is implemented using java code as follows:

Public static void MergeSort (int [] array, int head, int tail) {// determines whether the index of the array's header is smaller than the tail index if (head <tail) {int middle = (head + tail)/2; MergeSort (array, head, middle); MergeSort (array, middle + 1, tail); Merge (array, head, middle, tail) ;}} public static void Merge (int [] array, int head, int middle, int tail) {// TODO Auto-generated method stub int [] temp = new int [tail-head + 1]; int a = head; int B = middle + 1; int I = 0; // compare the numbers in the two arrays and store smaller values in the temporary array while (a <= middle & B <= tail) {if (array [a] <array [B]) {temp [I ++] = array [a ++];} else {temp [I ++] = array [B ++] ;}}// add the number of arrays not involved in the comparison to the temporary array while (a <= middle) {temp [I ++] = array [a ++];} while (B <= tail) {temp [I ++] = array [B ++];} // put the sorted array back to the array System. arraycopy (temp, 0, array, head, tail-head + 1 );}

The time complexity of Merge Sorting is O (nlogn), and the space complexity is O (n ).

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.