Principles of the Merge Sorting Algorithm and JAVA Implementation

Source: Internet
Author: User

Merge Sorting uses recursive and divide-and-conquer technologies to divide data sequences into smaller half-child tables, and then sort the half-child tables, finally, we use recursive steps to merge the prepared semi-subtables into an ever-increasing ordered sequence. The merging and sorting involves two steps: 1) Partitioning Table 2) merge a semi-subtable. first, we will discuss the merge algorithm. The merge algorithm puts a series of data into a vector and the index range is [first, last]. this sequence is composed of two sub-tables with good sorting order. The index endpoint (mid) is the dividing line. The following is a sequence, in a sequence such as 48, there are two subsequences: 7, 10, 19, 25, 12, 17, and 48, as shown in: the steps for using the merge algorithm are as follows: Step 1: compare v [indexA] = 7 and v [indexB] = 12. Extract the smaller v [indexA] and put it in the temporary vector tempArray. Then add Step 1 to indexA: compare v [indexA] = 10 and v [indexB] = 12, and place the smaller 10 In the temporary variable tempArray, then indexA ++; Step 3: Compare v [indexA] = 19 and v [indexB] = 12, and store the smaller 12 in the Temporary Variable tempArray, then indexB ++; Step 4 to Step 7: Compare and store the data according to the preceding rules. The following result is displayed: add the remaining items in sub-Table B to the temporary vector tempArray and copy the values in the temporary variables back to the vector v according to the index position, java implementation code: package com. sort. merge; public class Merge {/*** division and control method, top-down, recursively split the array, and finally merge */public static void merge (int [] arr, int start, int end) {if (start <end) {int mid = (start + end)/2; www.2cto. commerge (arr, start, mid); // Recursively call arr [start... mid] Sort merge (arr, mid + 1, end); // recursively pairs arr [mid + 1... end] Sort doMerge (arr, start, mid, end); // combine two ordered areas into one ordered area} // combination, merge public static void doMerge (int [] arr, int start, int mid, int end) {int tempIndex = start; int Index = start; int right = mid + 1; int temp [] = new int [arr. length]; // two subsequences are compared. Small values are placed in a temporary array while (start <= mid & right <= end) {if (arr [start] <= arr [right]) {temp [tempIndex ++] = arr [start ++];} Else {temp [tempIndex ++] = arr [right ++] ;}}// Add the elements on the right to the temporary array while (start <= mid) {temp [tempIndex ++] = arr [start ++];} // Add the elements on the right to the temporary array while (right <= end) {temp [tempIndex ++] = arr [right ++];} // copy the data in the temporary data temp [] to the arr [] array while (Index <end) {arr [Index] = temp [Index ++] ;}} public static void main (String [] args) {// TODO Auto-generated method stubint arr [] = {2, 4, 6, 8, 1, 3, 5, 9}; merge (arr, 0, arr. length-1); for (int I = 0; I <arr. length; I ++) {System. out. print (arr [I]) ;}}use the divide and conquer method to design a top-down algorithm, which is more concise. (1) In the three steps of the division and Control Law, set the current range of the Merge Sorting to R [low .. high], the three steps of the divide and conquer method are: ① decomposition: Split the current interval into two parts, that is, finding the split point ② solution: recursively pair the two subintervals R [low .. mid] and R [mid + 1 .. high] to merge and sort; ③ combination: to sort the two subintervals R [low .. mid] and R [mid + 1 .. high] is merged into an ordered interval R [low .. high]. Recursive termination condition: the subinterval length is 1 (a record is naturally ordered ). (2) specific algorithm void MergeSortDC (SeqList R, int low, int high) {// divide and conquer R [low .. high] performs binary Merge Sorting int mid; if (low

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.