Java 内部排序算法性能测试

内部排序算法性能测试

package sort;

public class SortTest {
    public static void main(String[] args) {
        //baseTest();
        batchSortTest();
    }

    private static void baseTest() {
        int[] baseTemp = {4, 2, 6, 1, 8, 5};

        //冒泡排序调试测试
        //BubbleSortDebugTest(baseTemp);

        //选择排序调试测试
        SelectSortDebugTest(baseTemp);
    }

    /**
     * 批量排序测试
     */
    private static void batchSortTest() {
        int[] sortDataTotal1 = {100, 1000, 5000, 10000};
        int[] sortDataTotal02 = {100, 1000, 5000, 10000, 50000};
        int[] sortDataTotal03 = {100, 1000, 5000, 10000, 50000, 100000, 150000, 2000000};
        for (int item : sortDataTotal02) {
            singleSortTest(item);
            System.out.println();
        }
    }

    /**
     * 某个量级排序测试
     * @param total
     */
    public static void singleSortTest(int total) {
        long start = System.currentTimeMillis();
        int[] array = new int[total];
        for (int i = 0; i < total; i++) {
            // 乘100是为了使随机数充分的散开
            array[i] = (int) (Math.random() * total + 100);
        }
        long end = System.currentTimeMillis();
        System.out.print(total + " Add Time : " + (end - start));
        System.out.println();

        // 冒泡排序测试
        BubbleSortTest(array);

        // 冒泡排序测试
        SelectSortTest(array);
    }


    /**
     * 冒泡排序测试
     * @param array
     */
    public static void BubbleSortTest(int[] array) {
        long start = System.currentTimeMillis();
        BubbleSort.sortAsc(array);
        long end = System.currentTimeMillis();
        System.out.print("Bubble Sort Time : " + (end - start));
        System.out.println();
    }

    /**
     * 冒泡排序测试
     * @param array
     */
    public static void BubbleSortDebugTest(int[] array) {
        long start = System.currentTimeMillis();
        BubbleSort.sortAscDebug(array);
        long end = System.currentTimeMillis();
        System.out.print("Bubble Sort Time : " + (end - start));
    }

    /**
     * 冒泡排序测试
     * @param array
     */
    public static void SelectSortTest(int[] array) {
        long start = System.currentTimeMillis();
        SelectSort.sortAsc(array);
        long end = System.currentTimeMillis();
        System.out.print("Select Sort Time : " + (end - start));
        System.out.println();
    }

    /**
     * 冒泡排序测试
     * @param array
     */
    public static void SelectSortDebugTest(int[] array) {
        long start = System.currentTimeMillis();
        SelectSort.sortAscDebug(array);
        long end = System.currentTimeMillis();
        System.out.print("Select Sort Time : " + (end - start));
    }
}

 使用的工具类:

package util;

public class BaseUtil {
    public static void arrayIntPrint(int[] array){
        for(int item : array){
            System.out.print(item + " ");
        }
        System.out.println();
    }

}

 

猜你喜欢

转载自www.cnblogs.com/6xiong/p/13211385.html
今日推荐