lat*_*uga 219 javascript arrays sorting algorithm
JavaScript Array#sort()函数使用哪种算法?我知道它可以采取各种各样的参数和函数来执行不同类型的排序,我只是对vanilla排序使用哪种算法感兴趣.
Kon*_*lph 278
我刚刚看了一下WebKit(Chrome,Safari ......)的来源.根据数组的类型,使用不同的排序方法:
使用C++标准库函数对数字数组(或基本类型的数组)进行排序,该函数std::qsort实现快速排序的一些变体(通常是内部排序).
使用mergesort 对连续的非数字类型数组进行字符串化和排序(如果可用)(以获得稳定的排序)或者qsort没有可用的合并排序.
对于其他类型(非连续数组,可能是关联数组),WebKit使用选择排序(它们称为"min"排序),或者在某些情况下,它通过AVL树进行排序.不幸的是,这里的文档相当模糊,因此您必须跟踪代码路径以实际查看哪种类型使用哪种排序方法.
还有像这样评论的宝石:
// FIXME: Since we sort by string value, a fast algorithm might be to use a
// radix sort. That would be O(N) rather than O(N log N).
Run Code Online (Sandbox Code Playgroud)
- 让我们只希望实际"修复"它的人比这个注释的编写者更好地理解渐近运行时,并且意识到基数排序比简单的O(N)具有稍微复杂的运行时描述.
(感谢phsource指出原始答案中的错误.)
Joe*_*mas 37
JS没有草案要求使用特定的排序algorthim.正如许多人在这里提到的,Mozilla使用合并排序.但是,在Chrome的v8源代码中,截至今天,它使用QuickSort和InsertionSort,用于较小的阵列.
从807 - 891行
var QuickSort = function QuickSort(a, from, to) {
var third_index = 0;
while (true) {
// Insertion sort is faster for short arrays.
if (to - from <= 10) {
InsertionSort(a, from, to);
return;
}
if (to - from > 1000) {
third_index = GetThirdIndex(a, from, to);
} else {
third_index = from + ((to - from) >> 1);
}
// Find a pivot as the median of first, last and middle element.
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
a[from] = v0;
a[to - 1] = v2;
var pivot = v1;
var low_end = from + 1; // Upper bound of elements lower than pivot.
var high_start = to - 1; // Lower bound of elements greater than pivot.
a[third_index] = a[low_end];
a[low_end] = pivot;
// From low_end to i are elements equal to pivot.
// From i to high_start are elements that haven't been compared yet.
partition: for (var i = low_end + 1; i < high_start; i++) {
var element = a[i];
var order = comparefn(element, pivot);
if (order < 0) {
a[i] = a[low_end];
a[low_end] = element;
low_end++;
} else if (order > 0) {
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) {
element = a[i];
a[i] = a[low_end];
a[low_end] = element;
low_end++;
}
}
}
if (to - high_start < low_end - from) {
QuickSort(a, high_start, to);
to = low_end;
} else {
QuickSort(a, from, low_end);
from = high_start;
}
}
};
Run Code Online (Sandbox Code Playgroud)
更新 自2018年起,V8使用TimSort,感谢@celwell.资源
小智 23
ECMAscript标准未指定要使用的排序算法.实际上,不同浏览器具有不同的排序算法 例如,在对地图进行排序时,Mozilla/Firefox的sort()不稳定(在单词的排序意义上).IE的sort()是稳定的.
我认为这将取决于您所引用的浏览器实现.
每种浏览器类型都有自己的javascript引擎实现,所以它取决于.您可以检查Mozilla和Webkit/Khtml的源代码存储库以了解不同的实现.
IE是封闭的来源,所以你可能不得不问微软的人.
从 V8 v7.0 / Chrome 70 开始,V8 使用Python 的排序算法TimSort。Chrome 70 于 2018 年 9 月 13 日发布。
有关此更改的详细信息,请参阅V8 开发者博客上的帖子。您还可以阅读源代码或补丁 1186801。