Merge Sorting by sorting algorithm (JAVA)

Source: Internet
Author: User

Merge Sorting by sorting algorithm (JAVA)
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 consists of two sub-tables with good sorting order. The index midpoint (mid) is used as the dividing line. The following is a sequence, in a sequence such as 48, it is divided into two subsequences: 7, 10, 19, 25, and 12, 17, and 48. The next step is to use the merge algorithm: 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, place the smaller 10 to the Temporary Variable tempArray, and then indexA ++; Step 3: compare v [indexA] = 19 and v [indexB] = 12, store the smaller 12 to the Temporary Variable tempArray, and then indexB ++; Step 4 to Step 7: compare and store the data according to the above rules. The last step is to add the remaining items in sub-Table B to the temporary vector tempArray and then sort the values in the temporary variables according to the index position, copy back to vector v to complete the Merge Sorting of vector v java code implementation: All functions share a temporary array temp, each time a new temporary array overhead is too large

Package org. conan. myhadoop. mr; import java. util. arrays; public class MergeSort {public static int [] sort (int [] array, int low, int high, int [] temp) {int mid = (low + high) /2; 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.