(Java版)算法——對數器的使用(驗證算法的正確性)

對數器的使用(驗證算法的正確性)

背景

一般很多的練習算法的平臺都可以對寫出的算法進行測試,但是如果算法不知道在哪裏測試,或者在參加競賽時不知道寫的算法的正確性,這是可以使用對數器來驗證算法的正確性。

對數器的使用步驟

  1. 準備預測定的方法(算法)a
  2. 實現一個絕對正確但複雜度不好的方法b(保證方法b的正確性,很多時候使用系統提供的方法)
  3. 實現一個隨機樣本生成器
  4. 實現對比的方法(可以對比方法a和方法b)
  5. 將方法a和方法b比對很多次來驗證方法a是否正確
  6. 如果有一個樣本使得比對出錯, 打印樣本分析是哪個方法出
    錯(可以快速找出錯誤點)
  7. 當樣本數量很多時比對測試依然正確, 可以確定方法a已經
    正確

使用實例

實例一:排序
public class BubbleSort {

	/**
	 * 功能描述: 冒泡排序算法
	 */
	public static void bubbleSort(int[] arr) {
		if (arr == null || arr.length < 2) {
			return;
		}
		//從數組末尾每次減1(排序一次,減少一個數組元素的排序)
		for (int e = arr.length - 1; e > 0; e--) {
			//循環,每次循環,最大的數排在最後一位
			for (int i = 0; i < e; i++) {
				//從小到大排序。如果左邊大。兩個元素交換位置
				if (arr[i] > arr[i + 1]) {
					swap(arr, i, i + 1);
				}
			}
		}
	}

	/**
	 * 功能描述: 調換兩個元素在數組中的位置
	 */
	public static void swap(int[] arr, int i, int j) {
		//這種方式還不理解
		arr[i] = arr[i] ^ arr[j];
		arr[j] = arr[i] ^ arr[j];
		arr[i] = arr[i] ^ arr[j];
	}

	/**
	 * 功能描述:提供一個絕對正確的方法(不考慮時間複雜度)
	 */
	public static void comparator(int[] arr) {
		//系統排序方法,絕對正確
		Arrays.sort(arr);
	}

	/**
	 * 功能描述: 隨機數組生成器
	 * @param maxSize 數組的最大長度
	 * @param maxValue 數組的最大值
	 * @return 隨機長度、隨機值的數組
	 */
	public static int[] generateRandomArray(int maxSize, int maxValue) {
		int[] arr = new int[(int) ((maxSize + 1) * Math.random())];
		for (int i = 0; i < arr.length; i++) {
			arr[i] = (int) ((maxValue + 1) * Math.random()) - (int) (maxValue * Math.random());
		}
		return arr;
	}

	/**
	 * 功能描述:拷貝數組 test
	 * 返回新數組長度一樣,值一樣
	 */
	public static int[] copyArray(int[] arr) {
		if (arr == null) {
			return null;
		}
		int[] res = new int[arr.length];
		for (int i = 0; i < arr.length; i++) {
			res[i] = arr[i];
		}
		return res;
	}

	/**
	 * 功能描述:判斷兩個數組是否相同
	 */
	public static boolean isEqual(int[] arr1, int[] arr2) {
		if ((arr1 == null && arr2 != null) || (arr1 != null && arr2 == null)) {
			return false;
		}
		if (arr1 == null && arr2 == null) {
			return true;
		}
		if (arr1.length != arr2.length) {
			return false;
		}
		for (int i = 0; i < arr1.length; i++) {
			if (arr1[i] != arr2[i]) {
				return false;
			}
		}
		return true;
	}

	/**
	 * 功能描述:打印數組對象
	 */
	public static void printArray(int[] arr) {
		if (arr == null) {
			return;
		}
		for (int i = 0; i < arr.length; i++) {
			System.out.print(arr[i] + " ");
		}
		System.out.println();
	}

	// for test
	public static void main(String[] args) {
		int testTime = 500000;
		int maxSize = 100;
		int maxValue = 100;
		boolean succeed = true;
		for (int i = 0; i < testTime; i++) {
			int[] arr1 = generateRandomArray(maxSize, maxValue);
			int[] arr2 = copyArray(arr1);
			//待測算法方法
			bubbleSort(arr1);
			//絕對正確的方法
			comparator(arr2);
			if (!isEqual(arr1, arr2)) {
				succeed = false;
				break;
			}
		}
		System.out.println(succeed ? "Nice!" : "Fucking fucked!");

		int[] arr = generateRandomArray(maxSize, maxValue);
		printArray(arr);
		bubbleSort(arr);
		printArray(arr);
	}

}
實例二:封裝類的對數器
/**
 * 比較器
 */
public class MyComparator {

	public static class Student {
		public String name;
		public int id;
		public int age;

		public Student(String name, int id, int age) {
			this.name = name;
			this.id = id;
			this.age = age;
		}
	}

	public static class IdAscendingComparator implements java.util.Comparator<Student> {

		@Override
		public int compare(Student o1, Student o2) {
			return o1.id - o2.id;
		}

	}

	/**
	 *實現比較接口Comparator,動態實現比較
	 *降序
	 */
	public static class IdDescendingComparator implements java.util.Comparator<Student> {

		@Override
		public int compare(Student o1, Student o2) {
			return o2.id - o1.id;
		}

	}

	public static class AgeAscendingComparator implements java.util.Comparator<Student> {

		@Override
		public int compare(Student o1, Student o2) {
			return o1.age - o2.age;
		}

	}

	public static class AgeDescendingComparator implements java.util.Comparator<Student> {

		@Override
		public int compare(Student o1, Student o2) {
			return o2.age - o1.age;
		}

	}

	public static void printStudents(Student[] students) {
		for (Student student : students) {
			System.out.println("Name : " + student.name + ", Id : " + student.id + ", Age : " + student.age);
		}
		System.out.println("===========================");
	}

	public static void main(String[] args) {
		Student student1 = new Student("A", 1, 23);
		Student student2 = new Student("B", 2, 21);
		Student student3 = new Student("C", 3, 22);

		Student[] students = new Student[] { student3, student2, student1 };
		printStudents(students);

		Arrays.sort(students, new IdAscendingComparator());
		printStudents(students);

		Arrays.sort(students, new IdDescendingComparator());
		printStudents(students);

		Arrays.sort(students, new AgeAscendingComparator());
		printStudents(students);

		Arrays.sort(students, new AgeDescendingComparator());
		printStudents(students);

	}

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章