Java Merge sort

Source: Internet
Author: User

The code is as follows:

public class MergeSort {public static void MergeSort (Datawrap [] data) {sort (data, 0, data.length-1);}  /** * Merge the index from left to right range of array elements * @param data to be sorted by the array * @param the first index of the element of the left row array * @param the last index of the element of the right-to-row array */private static void sort (datawrap[] data, int left, int right) {if ' < right ' {///Find Intermediate index int center = (Ieft + right)/2;//    Recursive sort (data, left, center);    Recursively sort (data, center+1, right) for the array on the left; Merging merge (data, left, center, right);}} /** * Two arrays to merge, merge the previous two arrays have been ordered, after merging is still ordered * @param data Array Object * @param the index of the first element of left array * @param the index of the last element of the center left array, center+1 right Index of the first element of the array * @param the index of the last element of right array */private static void merge (datawrap[] data, int left, int Center, int.) {D Atawrap [] Tmparry = new Datawrap [data.length];int mid = center +1;//third Record Intermediate array index int third = Left;int TMP = Left;while ( Left <= Center && mid <=right) {//Remove a small number from two arrays into the intermediate array if (Data[left].compareto (Data[mid]) <=0) {tmparry[ third++] = data[left++];} else{tmparry[third++] = data[mid++];}} The remainder is placed in the middle array in turn (mid <= right) {tmparry[third++] = data[mid++];} while (left <= center) {tmparry[third++] = data[left++];} Copies the contents of the intermediate array to the original array//(contents in the original Left-right range are copied back to the original array) while (TMP <= right) {data[tmp] = tmparry[tmp++];}} public static void Main (string[] args) {datawrap[] data = {new Datawrap (""), New Datawrap (""), new Datawrap (49, ""), New Datawrap (""), new Datawrap (+, ""), new Datawrap (9, ""), New Datawrap (-16, "")}; System.out.println ("before sorting: \ n" + java.util.Arrays.toString (data)); MergeSort (data); System.out.println ("After sorting: \ n" + java.util.Arrays.toString (data));}}

  

Java Merge sort

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.