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