(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);

	}

}
发布了23 篇原创文章 · 获赞 15 · 访问量 3732

猜你喜欢

转载自blog.csdn.net/qq_42937522/article/details/104512359