Import java. util .*;
Public class MergeSortTest {
Public static void main (String [] args ){ Int arr [] = new int [] {10, 9, 12, 4, 11, 7, 8, 3 }; Sort (arr ); System. out. println (Arrays. toString (arr); // convert an integer array to a String and print it out.
}
Public static void Sort (int arr []) { Int tmpArray [] = new int [arr. length]; // create a temporary array to store the arrays in the temporary sorting. MergeSort (arr, tmpArray, 0, arr. length-1); // starts sorting the array Merging
} // Use recursive methods to merge and sort Arrays Private static void mergeSort (int arr [], int tmpArray [], int first, int last ){ If (first <last) {// confirm that the array is not empty Int mid = (first + last)/2; // divide the array into two segments // The following uses a recursive method to continuously split the array. That is, 1 minute, 2 minute, 4 minute, and so on. Until each array has only two elements left. MergeSort (arr, tmpArray, first, mid ); MergeSort (arr, tmpArray, mid + 1, last ); // The following uses the temporary array tmpArray [] to sort and merge the split arrays. Merge (arr, tmpArray, first, mid, last ); System. out. println (Arrays. toString (arr )); } }
Private static void merge (int a [], int tmpArray [], int first, int mid, int last ){ Int beginHalf1 = first; // subscript of the starting element of the first array Int endHalf1 = mid; // The following table lists the elements at the end of the first array. Int beginHalf2 = mid + 1; // subscript of the starting element of the second array Int endHalf2 = last; // subscript of the end element of the second array Int index = beginHalf1; // index of the temporary Array Int num = last-first + 1; // number of times required to copy elements in the temporary tmpArray to the array arr, (if there is no sorted element, the elements do not need to be copied. Therefore, the unordered elements of arr are still unchanged.
While (beginHalf1 <= endHalf1) & (beginHalf2 <= endHalf2) {// check whether the last element is obtained for the split array. If (a [beginHalf1] <= a [beginHalf2]) {// extract the values of two elements from two Arrays for comparison. // If the element value is small, it is copied to the position corresponding to the tmpArray of the temporary array, then index ++, And the array is then taken to the next element.
TmpArray [index ++] = a [beginHalf1 ++]; } Else { TmpArray [index ++] = a [beginHalf2 ++];
}
} // After the while LOOP above, all the elements of the array must be taken out, that is, there must be another array that does not take out the light, not only does not take the light, the remaining elements are sorted. Therefore, only one of the following two while statements will be executed, that is, the elements of the remaining array in the sorted order will be copied to the corresponding position in the temporary array tmpArray.
While (beginHalf1 <= endHalf1 ){ TmpArray [index ++] = a [beginHalf1 ++]; } While (beginHalf2 <= endHalf2 ){ TmpArray [index ++] = a [beginHalf2 ++]; } // Copy all the elements sorted in the temporary array tmpArray [] to the original array arr [] For (int I = 0; I <num; I ++, endHalf2 --){ A [endHalf2] = tmpArray [endHalf2]; }
}
} The output result is as follows: [9, 10, 12, 4, 11, 7, 8, 3] [9, 10, 4, 12, 11, 7, 8, 3] [4, 9, 10, 12, 11, 7, 8, 3] [4, 9, 10, 12, 7, 11, 8, 3] [4, 9, 10, 12, 7, 11, 3, 8] [4, 9, 10, 12, 3, 7, 8, 11] [3, 4, 7, 8, 9, 10, 11, 12] [3, 4, 7, 8, 9, 10, 11, 12]
|