排序算法之冒泡排序
点开JDK的Arrays工具类,有个静态方法sort(),一直用着,也没怎么看过源码,于是心血来潮,点开看看。原来是背后用的是DualPivotQuicksort。
原文:
This class implements the Dual-Pivot Quicksort algorithm by Vladimir Yaroslavskiy, Jon Bentley, and Josh Bloch. The algorithm offers O(n log(n)) performance on many data sets that cause other quicksorts to degrade to quadratic performance, and is typically faster than traditional (one-pivot) Quicksort implementations.
对于the Dual-Pivot Quicksort algorithm,虽然看过流程图了,但感觉到确实不太懂。于是回顾回顾基本排序算法知识,深感学习排序算法,要理解,可以这样思考:
一趟排序后,发生什么
一共,需要几趟排序
冒泡排序
冒泡排序是简单排序的一种,思想是:
只在相邻元素间比较或交换,一趟排序后,最大元素放到尾部
那么,此时需要外层循环多少次来执行一趟排序呢?从尾部往前遍历
所以:
步骤一:从头开始循环,比较相邻元素,小的放在前
步骤二:外层循环,从最后一个元素开始,往前遍历
步骤三:当不再发生交换时,结束循环。那么设个标记。
public static void bubbleSort(int[] array,int