Java implementation of merge sorting algorithm
Merge sort is to divide the unordered array into two arrays. The divided array has only half the number of elements of the original array. Then continue to divide the two divided arrays, and cycle this operation until there is only one element in the divided array, and then merge and sort the divided array. Merge the two divided arrays into an ordered array until they are finally merged into an array containing all elements, and the merge sorting operation is completed. The process of merging and sorting is illustrated graphically below.
Suppose there is an unordered array: {3,2,4,1}. The following is the division process of the array. First, divide the array into two arrays {3,2} and {4,1}. Then, after dividing the two arrays, we get four arrays {3}, {2}, {4}, {1} and the division is completed.
Next, merge the arrays. First merge the two arrays {3} and {2} into an ordered array {2,3}. Similarly, perform the same operation on 4,1 to obtain {1,4}. Then merge the two ordered arrays and finally merge them into {1,2,3,4}. The merging is completed.
The merge sorting algorithm is implemented in Java code as follows: