20162330 第三週 藍墨雲班課 算法複雜度練習

題目:用 Java語言實現下列算法並進行單元測試, 請給出算法的時間複雜度。

  • (1)求一個整數二維數組Arr[N][N]的全部元素之和。
    (2)對於輸入的任意 3 個整數, 將它們按從小到大的順序輸出。
    (3)對於輸入的任意 n 個整數, 輸出其中的最大和最小元素。


(1)求一個整數二維數組Arr[N][N]的全部元素之和。

  • 代碼以下:
public class Test1 {
        public static void main(String[] args) {
            int[][] nums = { { 10, 20 }, { 5, 25 }, { 1, 29 } };
            System.out.println(getSum(nums));
        }

        public static int getSum(int[][] arrs) {
            int sum = 0;
            for (int i = 0; i < arrs.length; i++) {
                for (int j = 0; j < arrs[i].length; j++) {
                    sum += arrs[i][j];
                }
            }
            return sum;
        }
}

單元測試:java

public class Test1Test {
    /**
     * Method: getSum(int[][] arrs)
     */
    @Test
    public void testGetSum() throws Exception {
        int[][] list1 = new int[][]{{10, 5}, {25, 25}, {6, 29}};
        Assert.assertEquals(100, Test1.getSum(list1));
        int[][] list2 = new int[][]{{10, 5}, {25, 25}};
        Assert.assertEquals(65, Test1.getSum(list2));
        int[][] list3 = new int[][]{{25, 25}};
        Assert.assertEquals(50, Test1.getSum(list3));
    }
}

複雜度分析】這個例子我給出了二維數組的具體元素,若是沒有給出具體元素,即有n個含有n個元素的一維數組,遍歷求和要用for的嵌套循環,因此時間複雜度爲 O(\(n^2\)) .算法


(2)對於輸入的任意 3 個整數, 將它們按從小到大的順序輸出。

  • 代碼以下:
public class Test2 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("請輸入第一個整數:");
        int a = scan.nextInt();

        System.out.println("請輸入第二個整數:");
        int b = scan.nextInt();

        System.out.println("請輸入第三個整數:");
        int c = scan.nextInt();

        Compare(a, b, c);
    }

    public static String Compare(int a, int b, int c) {
        int t;
        if (a == b || a == c || b == c)
            return "Equality exist.";
        else {
            if (a < b) {
                t = b;
                b = a;
                a = t;
            }
            if (a < c) {
                t = c;
                c = a;
                a = t;
            }
            if (b < c) {
                t = c;
                c = b;
                b = t;
            }
        }
        return c + " < " + b + " < " + a;
    }
}

單元測試:數組

public class Test2Test {
    /**
     * Method: Compare(int a, int b, int c)
     */
    @Test
    public void testCompare() throws Exception {
        int a1 = 1, b1 = 2, c1 = 3;
        Assert.assertEquals("1 < 2 < 3", Test2.Compare(a1, b1, c1));
        int a2 = 20, b2 = 30, c2 = 10;
        Assert.assertEquals("10 < 20 < 30", Test2.Compare(a2, b2, c2));
        int a3 = 22, b3 = 22, c3 = 11;  // bound test
        Assert.assertEquals("Equality exist.", Test2.Compare(a3, b3, c3));
    }
}

複雜度分析】這個例子經過交換賦值法進行排序,規定輸入3個整數,算法執行次數肯定,因此時間複雜度爲 O(1) .單元測試


(3)對於輸入的任意 n 個整數, 輸出其中的最大和最小元素。

  • 代碼以下:
public class Test3 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("請輸入四個整數:");
        int a = scan.nextInt();
        int b = scan.nextInt();
        int c = scan.nextInt();
        int d = scan.nextInt();
        Comparable[] arr = {a, b, c, d};
        selectionSort(arr);
    }

    public static String selectionSort(Comparable[] data) {
        int min;

        for (int index = 0; index < data.length - 1; index++) {
            min = index;
            for (int scan = index + 1; scan < data.length; scan++)
                if (data[scan].compareTo(data[min]) < 0)
                    min = scan;
            swap(data, min, index);
        }
        return "Max:" + data[3] + ",Min:" + data[0];
    }

    private static void swap(Comparable[] data, int index1, int index2) {
        Comparable temp = data[index1];
        data[index1] = data[index2];
        data[index2] = temp;
    }
}

單元測試:測試

public class Test3Test {
    /**
     * Method: selectionSort(Comparable[] data)
     */
    @Test
    public void testSelectionSort() throws Exception {
        Comparable[] list1 = {11, 22, 33, 44};
        Assert.assertEquals("Max:44,Min:11", Test3.selectionSort(list1));
        Comparable[] list2 = {55, 66, 33, 44};
        Assert.assertEquals("Max:66,Min:33", Test3.selectionSort(list2));
        Comparable[] list3 = {44, 44, 44, 44};  // bound test
        Assert.assertEquals("Max:44,Min:44", Test3.selectionSort(list3));
    }
}

複雜度分析】這個例子我只添加了4個整數,若是輸入n個整數,使用選擇排序(for嵌套循環)再返回,因此時間複雜度爲 O(\(n^2\)) .spa

相關文章
相關標籤/搜索