源码地址
Array.prototype.sort
function ArraySort(comparefn) {
CHECK_OBJECT_COERCIBLE(this, "Array.prototype.sort");
var array = TO_OBJECT(this);
var length = TO_LENGTH(array.length);
return InnerArraySort(array, length, comparefn);
}
InnerArraySort
function InnerArraySort(array, length, comparefn) {
// In-place QuickSort algorithm.
// For short (length <= 22) arrays, insertion sort is used for efficiency.
//sort是否传递比较函数
if (!IS_CALLABLE(comparefn)) {
//未传递默认设置一个
comparefn = function (x, y) {
if (x === y) return 0;
if (%_IsSmi(x) && %_IsSmi(y)) {
return %SmiLexicographicCompare(x, y);
}
x = TO_STRING(x);
y = TO_STRING(y);
if (x == y) return 0;
else return x < y ? -1 : 1;
};
}
//插入排序
var InsertionSort = function InsertionSort(a, from, to) {
...
};
//获取快排中哨兵(基准值)索引
var GetThirdIndex = function(a, from, to) {
...
}
//快排实现
var QuickSort = function QuickSort(a, from, to) {
...
}
//QuickSort作为程序的入口点
QuickSort(array, 0, num_non_undefined);
}
InsertionSort
var InsertionSort = function InsertionSort(a, from, to) {
for (var i = from + 1; i < to; i++) {
var element = a[i];
for (var j = i - 1; j >= from; j--) {
var tmp = a[j];
var order = comparefn(tmp, element);
if (order > 0) {
//往后赋值
a[j + 1] = tmp;
} else {
break;
}
}
//最终插入元素的位置
a[j + 1] = element;
}
};
let temp = arr[j];
arr[j] = arr[j - 1];
arr[j - 1] = temp;
GetThirdIndex
third_index = from + ((to - from) >> 1);
var GetThirdIndex = function(a, from, to) {
var t_array = new InternalArray();
// Use both 'from' and 'to' to determine the pivot candidates.
var increment = 200 + ((to - from) & 15);
var j = 0;
//从索引位置1和数组长度-1的位置开始取,是因为QuickSort中已经使得首位和尾部有序
from += 1;
to -= 1;
for (var i = from; i < to; i += increment) {
t_array[j] = [i, a[i]];
j++;
}
//即调用ArraySort,从头开始递归给筛选后的数组排序
//如果筛选后的数组每次都大于1000,注意comparefn也会随着递归深度嵌套
t_array.sort(function(a, b) {
//比较每个记录的数的值
return comparefn(a[1], b[1]);
});
//获取中位数的索引
var third_index = t_array[t_array.length >> 1][0];
return third_index;
}
提取后的数组格式假设为:[[1,100],[201,323],[405,122]]
comparefn1=function(a, b) {
return 默认的comparefn(a[1], b[1]);
})
提取后的数组格式假设为:[[1,[1,100]],[201,[40005,221]]]
comparefn2=function(a,b){
return comparefn1(a[1],b[1])
}
QuickSort
var QuickSort = function QuickSort(a, from, to) {
var third_index = 0;
while (true) {
// Insertion sort is faster for short arrays.
// 如果数量小于10,进行插入排序
if (to - from <= 10) {
InsertionSort(a, from, to);
return;
}
if (to - from > 1000) {
//如果数量大于1000,会每隔200~215收集一个元素,并对收集后的数组排序,取中位数的索引
third_index = GetThirdIndex(a, from, to);
} else {
//数量在10~1000之间直接取中位数作为哨兵的索引
third_index = from + ((to - from) >> 1);
}
// Find a pivot as the median of first, last and middle element.
//v8 选择哨兵元素和三个值有关
//从头和尾之外选择一个元素,还有前面步骤中取得的中位数,然后三个值排序取中间值
var v0 = a[from];
var v1 = a[to - 1];
var v2 = a[third_index];
var c01 = comparefn(v0, v1);
if (c01 > 0) {
// v1 < v0, so swap them.
var tmp = v0;
v0 = v1;
v1 = tmp;
} // v0 <= v1.
var c02 = comparefn(v0, v2);
if (c02 >= 0) {
// v2 <= v0 <= v1.
var tmp = v0;
v0 = v2;
v2 = v1;
v1 = tmp;
} else {
// v0 <= v1 && v0 < v2
var c12 = comparefn(v1, v2);
if (c12 > 0) {
// v0 <= v2 < v1
var tmp = v1;
v1 = v2;
v2 = tmp;
}
}
//最终对三个数排序后,
//升序保证: v0 <= v1 <= v2
//降序保证:v0 >= v1 >= v2
//将数组初步有序
a[from] = v0; //即首元素为三个数中最小的
a[to - 1] = v2; //即尾元素为三个数中最大的
var pivot = v1; //哨兵元素
//后续遍历的左右边界
//左边是from+1:因为首元素已经比哨兵元素小(v0<=v1),所以不再考虑
var low_end = from + 1; // Upper bound of elements lower than pivot.
//右边是to-1:因为尾元素已经比哨兵元素大(v1<=v2),所以不再考虑
var high_start = to - 1; // Lower bound of elements greater than pivot.
//将from+1这个元素调整到a[third_index],即上面步骤中位数到位置上
a[third_index] = a[low_end];
//将哨兵元素放到from+1点位置上,此时数组中首元素和首元素+1的位置已经是有序的了(即哨兵元素左边的比哨兵元素小)
//这也是a[third_index] = a[low_end]交换的意义
a[low_end] = pivot;
//上面的步骤初步根据v0、v1、v2,将数组分左右排序,此时from+1左边已经有序,即哨兵元素左边的比哨兵元素小
//后面的步骤将哨兵元素右边的值进行分左右
// From low_end to i are elements equal to pivot.
// From i to high_start are elements that haven't been compared yet.
//从low_end + 1开始是因为low_end左边已经有序
//i < high_start是因为最后一个元素肯定比哨兵元素大,所以不需要再排序
partition: for (var i = low_end + 1; i < high_start; i++) {
var element = a[i];
var order = comparefn(element, pivot);
//哨兵元素后面元素的比哨兵元素小,和哨兵元素交换位置,并low_end++
//low_end是记录哨兵元素的索引
if (order < 0) {
a[i] = a[low_end];
a[low_end] = element;
low_end++;
} else if (order > 0) {
//当哨兵元素后面的元素比哨兵元素大时
//从数组尾部-1的元素开始取值遍历,尾部-1是因为尾部元素在前面步骤处理后是有序的,一定比哨兵元素大
//一直查找到比哨兵元素小的数位置,否则退出循环,本轮排序结束
do {
high_start--;
if (high_start == i) break partition;
var top_elem = a[high_start];
order = comparefn(top_elem, pivot);
} while (order > 0);
//当从尾部查找到<=哨兵元素的元素时
//此处针对元素相等的情况
//将哨兵元素的下一个元素和查找到的元素交换位置,进入这里哨兵元素的下一个元素肯定比哨兵元素,所以可以直接交换,不会破坏排序
a[i] = a[high_start];
a[high_start] = element;
//当查找到的元素比哨兵元素小时
if (order < 0) {
//交换两个元素的位置,并更新low_end指针,指向哨兵元素位置
element = a[i];
a[i] = a[low_end];
a[low_end] = element;
low_end++;
}
}
}
//进入快排分左右的思想
//通过前面的for遍历使得本轮比较已经有序(比哨兵元素小的都在左边,大的在右边)
//优先取根据哨兵元素排序后,左右部分,长度最小的那一部分进行在进行递归排序(取小是因为越小越能达到插入排序的触发条件,更快取得结果)
if (to - high_start < low_end - from) {
QuickSort(a, high_start, to);
//当右边部分有序后,将数组的右边界变成low_end,即哨兵元素的位置
//因为整个sort是个while循环,所以右边排序后,又会进行左边的排序
to = low_end;
} else {
//当左边部分有序后,修改数组边界
QuickSort(a, from, low_end);
from = high_start;
}
}
};
过程分析