The principle of merging sort and the realization of Java code _java

Source: Internet
Author: User

Overview

The merge (merge) Sort method combines two (or more) ordered tables into a new ordered table, which divides the sequence into several subgroups, each of which is ordered. Then the ordered Subsequence is merged into the whole ordered sequence.

The merge sort uses the recursive return implementation, belong to "divide and conquer", the target array is divided in the middle, after the two arrays are sorted, after sorting, the sequence of two arrays "merged" together, the most important merge sort is the "merge" process, The merging process requires additional space that is consistent with the two number of group leaders that need to be merged.

Effect Chart:

Steps

Application space so that the sum of the two sorted sequences is used to store the merged sequence
Set two pointers, starting at the beginning of two sorted sequences, respectively
Compares the elements pointed to by two pointers, selects the relatively small elements into the merged space, and moves the pointer to the next position
Repeat step 3 until a pointer reaches the end of the sequence
Copy all remaining elements of another sequence directly to the end of the merge sequence
Instance

Raw data:

3 5 2 6 2
The premise of merging is to separate the array, split in Split, and then divided into no more points to merge.

First-round dividers, index 2 ((0+4)/2=2) for the middle

[3 5 2] [6 2]

Second-round separation, separating [3 5 2]

[3 5] [2] [6 2]

Third-round separation, separate for [3 5]

[3] [5] [2] [6 2]

merging [3] [5]

[3 5] [2] [6 2]

Merging [3 5] [2]

[2 3 5] [6 2]

Fourth-round separation, separating for [6 2]

[2 3 5] [6] [2]

merging [6] [2]

[2 3 5] [2 6]

Merge [2 3 5] [2 6]

[2 2 3 5 6]

Code implementation (Java)

Package com.coder4j.main.arithmetic.sorting;

  public class Merge {private static int mark = 0; /** * Merge Sort * @param array * @param low * @param high * @return/private static int[] sort (int[
    array, int low, int high) {int mid = (low + high)/2;
      if (Low < high) {mark++;
      System.out.println ("in progress" + Mark + "secondary separation, get");
      System.out.println ("[" + Low + "-" + mid + "] [" + (mid + 1) + "-" + High + "]");
      Left array sort (array, low, mid);
      Right array sort (array, mid + 1, high);
    Merge merges around (array, low, mid, high);
  } return array;  /** * Array * @param array * @param low * @param mid * @param high */private static void Merge (int[] array, int low, int mid, int high) {System.out.println ("merged: [+ Low +"-"+ mid +"] and [] + (mid + 1) +
    "-" + High + "]");
    int[] Temp = new Int[high-low + 1]; int i = low;//left pointer int j = mid + 1;//right pointer int k = 0; Move the smaller number first to the new array while (I <= mid && J <= High) {if (Array[i] < array[j]) {temp[k++] =
      Array[i++];
      else {temp[k++] = array[j++];
    One of the two arrays may have remaining elements//move the left remaining number into the array while (I <= mid) {temp[k++] = array[i++];
    ///Move the remaining number on the right side into the array while (J <= High) {temp[k++] = array[j++];
    (int m = 0; m < temp.length m++) {array[m + low] = Temp[m] in the new array; /** * Merge Sort * * @param array * @return/public static int[] Sort (int[] array) {return sor
  T (array, 0, array.length-1);
    public static void Main (string[] args) {int[] array = {3, 5, 2, 6, 2};
    int[] sorted = sort (array);
    SYSTEM.OUT.PRINTLN ("final result");
    for (int i:sorted) {System.out.print (i + "");

 }
  }
}

Test output results:

A 1th separation is in progress, the 2nd separation is being
made [0-2] [3-4], and the [
0-1] [2-2]
is undergoing a 3rd separation, resulting in
[0-0] [1-1]
merging: [0-0] and [1-1]
merge: [0-1] and [2-2]
are being separated for the 4th time, get
[3-3] [4-4]
merge: [3-3] and [4-4]
merge: [0-2] and [3-4]
end result
2 2 3 5 6 

Tested, consistent with the results in the example.

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.