• MergeSort(Java)


    实现代码:MergeSort.java

    public class MergeSort {
        public int[] sort(int[] input) {
            if (input.length <= 1) return input;
            if (input.length == 2) {
                if (input[0] > input[1]) {
                    int temp = input[0];
                    input[0] = input[1];
                    input[1] = temp;
                }
                return input;
            }
            int mid = input.length / 2;
            int[] firstHalf = sort(getPart(input, 0, mid));
            int[] secondHalf = sort(getPart(input, mid + 1, input.length - 1));
            return mergePart(firstHalf, secondHalf);
        }
    
        private int[] mergePart(int[] firstHalf, int[] secondHalf) {
            int[] result = new int[firstHalf.length + secondHalf.length];
            int firstIndex = 0, secondIndex = 0, resultIndex = 0;
            while (resultIndex < result.length) {
                if (chooseFirstHalf(firstHalf, firstIndex, secondHalf, secondIndex))
                    result[resultIndex++] = firstHalf[firstIndex++];
                else
                    result[resultIndex++] = secondHalf[secondIndex++];
            }
            return result;
        }
    
        private boolean chooseFirstHalf(int[] firstHalf, int firstIndex, int[] secondHalf, int secondIndex) {
            if (firstIndex == firstHalf.length) return false;
            if (secondIndex == secondHalf.length) return true;
            return firstHalf[firstIndex] < secondHalf[secondIndex];
        }
    
        private int[] getPart(int[] input, int begin, int end) {
            int[] result = new int[end - begin + 1];
            for (int i = begin; i <= end; i++) result[i - begin] = input[i];
            return result;
        }
    }
    


     

    测试代码:MergeSortTest.java

    import org.junit.*;
    
    import static org.junit.Assert.*;
    
    public class MergeSortTest {
        MergeSort mergeSort;
        
        @Before
        public void setUp() {
            mergeSort =  new MergeSort();
        }
        
        @Test
        public void should_return_1_for_merge_sort_1() {
            int[] input = {1};
            int[] expected = {1};
            assertArrayEquals(expected, mergeSort.sort(input));
        }
    
        @Test
        public void should_return_12_for_merge_sort_21() {
            int[] input = {2,1};
            int[] expected = {1,2};
            assertArrayEquals(expected, mergeSort.sort(input));
        }
    
        @Test
        public void should_return_1234_for_merge_sort_3214() {
            int[] input = {3,2,1,4};
            int[] expected = {1,2,3,4};
            assertArrayEquals(expected, mergeSort.sort(input));
        }
    
        @Test
        public void should_return_12345_for_merge_sort_54321() {
            int[] input = {5, 4, 3, 2, 1};
            int[] expected = {1, 2, 3, 4, 5};
            assertArrayEquals(expected, mergeSort.sort(input));
        }
    }
    


     

  • 相关阅读:
    BizTalk 2010 Adapter for mySAP Business Suite 所支持版本
    BizTalk Adapter Pack for Oracle 实现Oracle 11g的CRUD
    BizTalk 360 功能介绍 CTP版
    以小见大——那些基于 protobuf 的五花八门的 RPC(5 完)
    BizTalk Server 2010 + SQL Server 2008 R2 通过集成创造更大价值 [ 上篇 ]
    double to float
    Office 2003 PIA
    matlab传参数
    Convert.ToInt16
    矩阵转换
  • 原文地址:https://www.cnblogs.com/java20130722/p/3206877.html
Copyright © 2020-2023  润新知