Algorithms basis (a): The number of devices

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/qq_42191317/article/details/102746112

Brief introduction

When we want to verify the correctness of an algorithm, the general need to find their own test cases, test cases and find their own is difficult to cover all the circumstances, this time you need a means to help us verify the algorithm is correct, while the number of devices is one such means.

use

  1. There is a want to test method A
  2. Absolutely right but to achieve a higher degree of complexity of Method B
  3. To achieve a random sample generator
  4. Comparison of methods to achieve
  5. The methods A and B to verify multiple comparison method A correct
  6. If a sample obtained different results, then print out the sample
  7. When a large number of samples is consistent comparison test results can be considered correct method A

Code


import java.util.Arrays;

/**
 *@ClassNameDetector
 *@Description  对数器
 *@Author lzx
 *@Date2019/10/25 17:05
 *@Version V1.0
 **/
public class Detector {


    //方法A   冒泡排序  以排序算法为例测试
    public static void bubbleSort(int[] arr) {
        if (arr == null || arr.length < 2) {
            return;
        }
        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];
    }


    //方法B   绝对正确的比对方法
    public static void comparator(int[] arr) {
        Arrays.sort(arr);
    }


    //样本产生器
    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;
    }

    //数组复制
    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();
    }

    //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 ? "True!" : "False!");

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


}

 

Guess you like

Origin blog.csdn.net/qq_42191317/article/details/102746112