相较于为了对对数器的探究,上一篇对数器中的isEqual和copyArray方法
使用了Array.equal()和System.copyArray()库方法代替,以简化代码和提升美观性
package yzy.algorithm;
import java.util.Arrays;
/*
* 测试对数器改进之
* 去除isEqual和copyArray方法
* 用Array.equal()和System.copyArray()库方法代替
*/
public class testLogarithmMachine2 {
public static int[] generateRandomArray(int size, int value) {
int[] arr = new int[(int)((size+1)* Math.random())]; //(size+1)* Math.random()是[0, size]范围中的随机小数
for(int i=0; i<arr.length; ++i) {
arr[i] = (int) ((value+1)* Math.random() - (value)* Math.random());
}
return arr;
}
//绝对正确的方法
public static void rightMethod(int[] arr) {
Arrays.sort(arr);
}
public static void main(String[] args) {
int testTime = 500000; //测试组数
int size = 10; //对数器生成随机数组长度的范围:[0, 10]
int value = 100; //对数器生成随机数组元素值的范围:(-value, value]
boolean succeed = true;
for(int i=0; i<testTime; ++i) {
int[] arr1 = generateRandomArray(size, value);
int[] arr2 = new int[arr1.length];
System.arraycopy(arr1, 0, arr2, 0, arr1.length);
int[] arr3 = new int[arr1.length];
System.arraycopy(arr1, 0, arr3, 0, arr1.length);
testBubbleSort.bubbleSort(arr1); //测试bubbleSort
// testInsertSort.insertSort(arr1); //测试insertSort
//testSelectSort.SelectSort(arr1); //测试SelectSort
rightMethod(arr2);
if(!Arrays.equals(arr1, arr2)) {
succeed = false;
System.out.println(Arrays.toString(arr3));
break;
}
}
System.out.println(succeed ? "Succeed, nice!" :"Failed, Fucking fucked!");
}
}
testBubbleSort与testLogarithmMachine2类在同一个包中且是public类可以访问到,因此用类名.再加公有静态方法名就能访问bubbleSort函数
testBubbleSort.bubbleSort(arr1);