/**
* 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;
}
From Arrays.java line 804-814
如上所述,它声称使用插入排序。 但是,我将其视为冒泡排序? 它实际上是哪一个,为什么?
最佳答案
引用的代码是插入排序。冒泡排序重复遍历整个数组,而插入排序排序第一个元素,然后是前两个元素,然后是前三个元素,等等。你可以分辨出来,因为代码有两个索引循环,而外循环在冒泡排序只是检查整个数组是否有序。
关于java - Java.util包中Arrays的排序算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6014639/