Java.util包中的数组排序算法

wli*_*iao 11 java algorithm

 /**
 * Sorts the specified sub-array of bytes into ascending order.
 */
private static void sort1(byte x[], int off, int len) {
// Insertion sort on smallest arrays
if (len < 7) {
    for (int i=off; i<len+off; i++)
    for (int j=i; j>off && x[j-1]>x[j]; j--)
        swap(x, j, j-1);
    return;
}
Run Code Online (Sandbox Code Playgroud)

来自Arrays.java 804-814行

如上所述,它声称使用插入排序.但是,我把它当作冒泡排序?它实际上是哪一个,为什么?

art*_*tol 4

引用的代码插入排序。冒泡排序重复遍历整个数组,而插入排序对第一个元素进行排序,然后是前两个元素,然后是前三个元素,依此类推。您可以看出,因为代码有两个索引循环,而外部循环在冒泡排序只是检查整个数组是否有序。