Algorithm Note: Merge Sorting

Source: Internet
Author: User

I. Thoughts

Merge Sorting: sorts an array. You can first (recursively) divide it into two parts for sorting, and then merge the results;

Ii. Concepts

  • Merge: merge two ordered arrays into a larger ordered array;

    Iii. Features

    • Advantage: the time required to sort arrays with any length of N is proportional to that of NlogN;

    • Disadvantage: additional space is proportional to N;

      IV. Implementation Method

      • Merge two different ordered arrays into the third array;

      • First, sort the first half, then sort the second half, and then move the elements in the array without extra space;

        V. Code

        /*** Merge and Sort ** @ author pengcx **/public class Merge extends Sort {/** auxiliary array */private static Comparable [] aux; public static void main (String [] args) {String [] a = {"d", "a", "w", "B", "q"}; Merge. sort (a); show (a);} public static void sort (Comparable [] a) {aux = new Comparable [. length]; sort (a, 0,. length-1 );} /*** sort array a [lo] to a [hi] element ** @ param a * array a * @ param lo * minimum element position lo * @ param hi * Maximum element location hi */private static void sort (Comparable [], int lo, int hi) {if (hi <= lo) {return ;}// calculates the intermediate position of the array int mid = lo + (hi-lo)/2; // sort the elements sort (a, lo, mid) on the left of array a; // sort the elements sort (a, mid + 1, hi) on the Right of array ); // merge the elements merge (a, lo, mid, hi) on the left and right of array );} /*** merge the elements from a [lo] to a [mid] of array a with those from a [mid] to a [hi] ** @ param * array a * @ param lo * Minimum array element lo * @ param mid * intermediate element location mid * @ param hi * maximum element location hi */public static void merge (Comparable [], int lo, int mid, int hi) {int I = lo, j = mid + 1; for (int k = lo; k <= hi; k ++) {aux [k] = a [k] ;}for (int k = lo; k <= hi; k ++) {// If the left element is exhausted, take the right element if (I> mid) {a [k] = aux [j ++];} // if the right element is used up, take the element else if (j> hi) {a [k] = aux [I ++];} // if the current element of the right half is smaller than the current element of the left half, take the right half edge element else if (less (aux [j], aux [I]) {a [k] = aux [j ++];} // if the current element of the right half side is greater than or equal to the current element of the left half side, take the left half side element else {a [k] = aux [I ++] ;}}}

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.