【排序算法(八)】排序算法总结(下) - Go语言中文社区

【排序算法(八)】排序算法总结(下)


1. 八大排序算法目录总结

所有排序算法源码地址https://github.com/WeCanRun/Sorting-Algorithm
【排序算法(一)】选择排序及其改进
【排序算法(二)】冒泡排序及其改进
【排序算法(三)】直接插入排序及其改进
【排序算法(四)】希尔排序
【排序算法(五)】归并排序
【排序算法(六)】快速排序
【排序算法(七)】基数排序
【数据结构】堆应用之堆排序
【排序算法(八)】排序算法总结(上)

2. 性能测试

2.1 测试代码

package com.wuyi.notecode.sort;

import java.util.Random;

public class Main<T extends Comparable<T>> {

    public long testSort(Sort<Integer> sort) {
        Integer[] nums = new Integer[10000];
        Random random = new Random();
        for (int i = 0; i < 10000; i++) {
            nums[i] = random.nextInt();
        }

        long start = System.currentTimeMillis();
        sort.sort(nums);
        long end = System.currentTimeMillis();

        return end - start;
    }


    public static void main(String[] args) {
        Main<Integer> main = new Main<>();
        BubbleSort1<Integer> bubbleSort1 = new BubbleSort1<>();

        long res = main.testSort(bubbleSort1);
        System.out.println("BubbleSort1 运行时间:" + res + "毫秒");


        BubbleSort2<Integer> bubbleSort2 = new BubbleSort2<>();
        res = main.testSort(bubbleSort2);
        System.out.println("BubbleSort2 运行时间:" + res + "毫秒");


        SelectionSort1<Integer> selectionSort1 = new SelectionSort1<>();
        res = main.testSort(selectionSort1);
        System.out.println("SelectionSort1 运行时间:" + res + "毫秒");


        SelectionSort2<Integer> selectionSort2 = new SelectionSort2<>();
        res = main.testSort(selectionSort2);
        System.out.println("SelectionSort2 运行时间:" + res + "毫秒");


        InsertionSort1<Integer> insertionSort1 = new InsertionSort1<>();
        res = main.testSort(insertionSort1);
        System.out.println("InsertionSort1 运行时间:" + res + "毫秒");


        InsertionSort2<Integer> insertionSort2 = new InsertionSort2<>();
        res = main.testSort(insertionSort2);
        System.out.println("InsertionSort2 运行时间:" + res + "毫秒");


        ShellSort<Integer> shellSort = new ShellSort<>();
        res = main.testSort(shellSort);
        System.out.println("ShellSort 运行时间:" + res + "毫秒");


        HeapSort<Integer> heapSort = new HeapSort<>();
        res = main.testSort(heapSort);
        System.out.println("HeapSort 运行时间:" + res + "毫秒");


        MergeSort<Integer> mergeSort = new MergeSort<>();
        res = main.testSort(mergeSort);
        System.out.println("MergeSort 运行时间:" + res + "毫秒");


        QuickSort<Integer> quickSort = new QuickSort<>();
        res = main.testSort(quickSort);
        System.out.println("QuickSort 运行时间:" + res + "毫秒");


        ThreeWayQuickSort<Integer> threeWayQuickSort = new ThreeWayQuickSort<>();
        res = main.testSort(threeWayQuickSort);
        System.out.println("ThreeWayQuickSort 运行时间:" + res + "毫秒");

    }
}

2.2 测试结果

使用 1 w 个随机数据测试结果如下:

在这里插入图片描述
在这里插入图片描述

使用 100 w 个随机数据测试非O(n2)排序算法的结果如下:

在这里插入图片描述

在这里插入图片描述

快速排序是最快的通用排序算法,它的内循环的指令很少,而且它还能利用缓存,因为它总是顺序地访问数据。它的运行时间近似为~cNlogN,这里的 c 比其它线性对数级别的排序算法都要小。

使用三向切分快速排序,实际应用中可能出现的某些分布的输入能够达到线性级别,而其它排序算法仍然需要线性对数时间。

Java 的排序算法实现

Java 主要排序方法为 java.util.Arrays.sort (),对于原始数据类型使用三向切分的快速排序,对于引用类型使用归并排序。

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/qq_41900081/article/details/97238959
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢