二路の帰合アルゴリズムの実現


原文の住所:http://blog.csdn.net/middlekingt/article/details/8446552
    public class MergeSort {  
        /** 
         *      
         *   :   (     )                             ,         。                   
         *       O(nlogn) 
         *        
         * @param nums       
         * @return        
         */  
        public static int[] sort(int[] nums, int low, int high) {  
            int mid = (low + high) / 2;  
            if (low < high) {  
                //     
                sort(nums, low, mid);  
                //     
                sort(nums, mid + 1, high);  
                //       
                merge(nums, low, mid, high);  
            }  
            return nums;  
        }  

        public static void merge(int[] nums, int low, int mid, int high) {  
            int[] temp = new int[high - low + 1];  
            int i = low;//      
            int j = mid + 1;//      
            int k = 0;  

            //               
            while (i <= mid && j <= high) {  
                if (nums[i] < nums[j]) {  
                    temp[k++] = nums[i++];  
                } else {  
                    temp[k++] = nums[j++];  
                }  
            }  

            //              
            while (i <= mid) {  
                temp[k++] = nums[i++];  
            }  

            //               
            while (j <= high) {  
                temp[k++] = nums[j++];  
            }  

            //          nums    
            for (int k2 = 0; k2 < temp.length; k2++) {  
                nums[k2 + low] = temp[k2];  
            }  
        }  


        //          
        public static void main(String[] args) {  

            int[] nums = { 2, 7, 8, 3, 1, 6, 9, 0, 5, 4 };  

            MergeSort.sort(nums, 0, nums.length-1);  
            System.out.println(Arrays.toString(nums));  
        }  
    }