Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(89)

Unified Diff: src/array.js

Issue 6004001: Implement an adaption of Dual-Pivot Quicksort for Array.prototype.sort.
Patch Set: Created 10 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/array.js
diff --git a/src/array.js b/src/array.js
index 467d4eb007769a7efbaa4767d02b807948552947..7235021586f6083abda928d1d7ec24f58fa8e6d6 100644
--- a/src/array.js
+++ b/src/array.js
@@ -675,69 +675,79 @@ function ArraySort(comparefn) {
}
}
- function QuickSort(a, from, to) {
+ // Adaption of dual pivot quicksort.
+ // See http://gdtoolbox.com/DualPivotQuicksort.pdf
+ function DualPivotQuickSort(a, from, to, div) {
Karl Klose 2010/12/17 14:35:42 Why is the divider for the medians a variable? We
// Insertion sort is faster for short arrays.
- if (to - from <= 13) {
- InsertionSort(a, from, to);
- return;
- }
-
- // Find a pivot as the median of first, last and middle element.
- var v0 = a[from];
- var v1 = a[to - 1];
- var middle_index = from + ((to - from) >> 1);
- var v2 = a[middle_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;
+ var length = to - from;
+ while (length > 8) {
+ // Find pivots.
+ var pivotOffset = (length / div) | 0;
+ var pivot1 = a[from + pivotOffset];
+ var pivot2 = a[to - pivotOffset];
+ var cp = %_CallFunction(global_receiver, pivot1, pivot2, comparefn);
+ if (cp > 0) {
+ var tmp = pivot1;
+ pivot1 = pivot2;
+ pivot2 = tmp;
+ %_SwapElements(a, from, to - pivotOffset);
+ %_SwapElements(a, to - 1, from + pivotOffset);
+ } else {
+ %_SwapElements(a, from, from + pivotOffset);
+ %_SwapElements(a, to - 1, to - pivotOffset);
}
- }
- // v0 <= v1 <= v2
- a[from] = v0;
- a[to - 1] = v2;
- var pivot = v1;
- var low_end = from + 1; // Upper bound of the elements lower than pivot.
- var high_start = to - 1; // Lower bound of the elements greater than pivot.
- a[middle_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.
- for (var i = low_end + 1; i < high_start; ) {
- var element = a[i];
- var order = %_CallFunction(global_receiver, element, pivot, comparefn);
- if (order < 0) {
- %_SwapElements(a, i, low_end);
- i++;
- low_end++;
- } else if (order > 0) {
- high_start--;
- %_SwapElements(a, i, high_start);
- } else { // order == 0
- i++;
+ // Pivots are stored outside of the sorting area until we are done.
+ // pivot1 < pivot2.
+
+ var low_end = from + 1; // Upper bound of elements lower than pivots.
+ var high_start = to - 1; // Lower bound of elements greater than pivots.
+
+ // From low_end to i are elements between the pivots (inclusive).
+ // From i to high_start are elements that haven't been compared yet.
+ splitting: for (var i = low_end; i < high_start; ) {
+ var element = a[i];
+ if (%_CallFunction(global_receiver, element, pivot1, comparefn) < 0) {
+ // element < pivot1.
+ %_SwapElements(a, low_end, i);
+ i++;
+ low_end++;
+ } else if (
+ %_CallFunction(global_receiver, element, pivot2, comparefn) > 0) {
+ // element > pivot2.
+ do {
+ high_start--;
+ if (i == high_start) {
+ // All elements from i and up are greater than pivot2.
+ break splitting;
+ }
+ var top_elem = a[high_start];
+ var ct2 =
+ %_CallFunction(global_receiver, top_elem, pivot2, comparefn);
+ } while (ct2 > 0);
+ %_SwapElements(a, i, high_start); // Works even if i == high_start.
+ if (%_CallFunction(global_receiver, top_elem, pivot1, comparefn)
+ < 0) {
+ %_SwapElements(a, low_end, i);
+ low_end++;
+ }
+ i++;
+ } else {
+ // pivot1 <= element <= pivot2.
+ i++;
+ }
+ }
+ %_SwapElements(a, from, low_end - 1);
+ %_SwapElements(a, to - 1, high_start);
+ if (high_start - low_end < 13) div++;
+ DualPivotQuickSort(a, from, low_end - 1, div);
+ if (cp != 0) {
+ DualPivotQuickSort(a, low_end, high_start, div);
}
+ // Final tail-recursion is converted to loop.
+ from = high_start + 1;
+ length = to - from;
}
- QuickSort(a, from, low_end);
- QuickSort(a, high_start, to);
+ if (length > 1) InsertionSort(a, from, to);
}
// Copy elements in the range 0..length from obj's prototype chain
@@ -882,7 +892,7 @@ function ArraySort(comparefn) {
num_non_undefined = SafeRemoveArrayHoles(this);
}
- QuickSort(this, 0, num_non_undefined);
+ DualPivotQuickSort(this, 0, num_non_undefined, 3);
if (!is_array && (num_non_undefined + 1 < max_prototype_element)) {
// For compatibility with JSC, we shadow any elements in the prototype
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698