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;
}
}
单元测试:
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\)) .