Java Implementation of Merge Sorting and java Implementation of Merge Sorting

Source: Internet
Author: User

Java Implementation of Merge Sorting and java Implementation of Merge Sorting

The advantages of Merge Sorting are not mentioned.

Before merging and sorting, I try to sort two ordered arrays and combine them into an ordered array.

Idea: define two ordered arrays. When I understand it, I first think about the sorting of arrays with only one array, and then the sorting of arrays with two elements, first, compare the first element of the two arrays. When the first element is smaller, put it into the result array. Then, move the pointer down and continue the comparison until an array is empty and the comparison is stopped because it is an ordered array, the elements behind an array that is not empty are larger and ordered than those saved in the result array. Therefore, you only need to save the following array to the result array.

The following is code implementation:

/** The grouping algorithm combines the ordered array I and array j into c */public Integer [] sort (Integer [] I, integer [] j, Integer [] c) {c = new Integer [I. length + j. length]; int i1 = 0; // I array pointer int j1 = 0; // j array pointer int c1 = 0; // array pointer of c while (i1 <I. length & j1 <j. length) {if (I [i1]> j [j1]) {c [c1 ++] = j [j1]; j [j1 ++] = null ;} else {c [c1 ++] = I [i1]; I [i1 ++] = null ;}/ ** after I, there are elements */while (i1 <I. length) {c [c1 ++] = I [i1]; I [i1 ++] = null;}/** after j, there are elements */while (j1 <j. length) {c [c1 ++] = j [j1]; j [j1 ++] = null;} return c ;}

The preceding Implementation merges two ordered arrays, and merges and sorts them. Then, you can divide an unordered array into any number of Ordered arrays, and you do not need to check whether the array is an ordered array, an element in an array must be ordered. All I need to do is implement array decomposition recursively and combine two ordinal numbers.

To break down an array, you can use the divide and conquer method to define the header, tail, and intermediate pointer. Next recursion, you only need to change the intermediate pointer.

At the beginning, only one element in the header and one element in the tail must be compared;

Recursive upwards.

Forget it. paste the code.

  

Public int [] mergeSort (int [] num, int first, int last) {int mid = (first + last)/2; if (first <last) {mergeSort (num, first, mid); mergeSort (num, mid + 1, last); merge (num, first, mid, last);} return num ;} public void merge (int [] num, int first, int mid, int last) {int _ left = first; // left pointer int _ right = mid + 1; // right pointer int [] temp = new int [last-first + 1]; int temp_p = 0; while (_ left <= mid & _ right <= last) {if (num [_ left] <num [_ right] ) {Temp [temp_p ++] = num [_ left ++];} else {temp [temp_p ++] = num [_ right ++];} while (_ left <= mid) {temp [temp_p ++] = num [_ left ++];} while (_ right <= last) {temp [temp_p ++] = num [_ right ++];} _ left = 0; // because no array is returned, the sorted array should be placed in the num array, overwrite the content directly. Pay attention to the subscript. For (int I: temp) {num [(_ left ++) + first] = I ;}}

First, last is the array head and tail pointer.

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.