How to realize merge sorting in Java
It is believed that many inexperienced people are at a loss about how to achieve merging and sorting in Java. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
Public class MergeSort {static int number=0; public static void main (String [] args) {int [] a = {26,5,98,108,28,99,100,56,34,1}; printArray ("before sorting:", a); MergeSort (a); printArray ("after sorting:", a) } private static void printArray (String pre,int [] a) {System.out.print (pre+ "\ n"); for (int item0 / right) return; int mid = (left + right) / 2; / / there are two Sort in the two-way merge sort, and multiple Sort can be written in the multi-path merge sort (a, left, mid) Sort (a, mid + 1, right); merge (a, left, mid, right);} private static void merge (int [] a, int left, int mid, int right) {int [] tmp = new int [a.length]; int R1 = mid + 1; int tIndex = left; int cIndex=left; / / merge while (left) one by one