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

Unified Diff: src/array.js

Issue 4065: Using quick sort for arrays.... (Closed) Base URL: http://v8.googlecode.com/svn/branches/bleeding_edge/
Patch Set: Created 12 years, 3 months 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
===================================================================
--- src/array.js (revision 361)
+++ src/array.js (working copy)
@@ -669,62 +669,33 @@
else return x < y ? -1 : 1;
};
- var old_length = ToUint32(this.length);
-
- %RemoveArrayHoles(this);
-
- var length = ToUint32(this.length);
-
- // Bottom-up max-heap construction.
- for (var i = 1; i < length; ++i) {
- var child_index = i;
- while (child_index > 0) {
- var parent_index = ((child_index + 1) >> 1) - 1;
- var parent_value = this[parent_index], child_value = this[child_index];
- if (Compare(parent_value, child_value) < 0) {
- this[parent_index] = child_value;
- this[child_index] = parent_value;
- } else {
- break;
+ function QuickSort(a, from, to) {
+ if (from >= to - 1) return;
+ var pivot_index = global.Math.floor(global.Math.random() * (to - from)) + from;
Mads Ager (chromium) 2008/09/24 12:34:43 We need to make sure that we use the original Math
+ var pivot = a[pivot_index];
+ a[pivot_index] = a[to - 1];
+ a[to - 1] = pivot;
+ var partition = from;
+ for (var i = from; i < to - 1; i++) {
+ var element = a[i];
+ if (Compare(element, pivot) < 0) {
+ a[i] = a[partition];
+ a[partition] = element;
+ partition++;
}
- child_index = parent_index;
}
+ a[to - 1] = a[partition];
+ a[partition] = pivot;
+ QuickSort(a, from, partition);
+ QuickSort(a, partition + 1, to);
}
- // Extract element and create sorted array.
- for (var i = length - 1; i > 0; --i) {
- // Put the max element at the back of the array.
- var t0 = this[0]; this[0] = this[i]; this[i] = t0;
- // Sift down the new top element.
- var parent_index = 0;
- while (true) {
- var child_index = ((parent_index + 1) << 1) - 1;
- if (child_index >= i) break;
- var child1_value = this[child_index];
- var child2_value = this[child_index + 1];
- var parent_value = this[parent_index];
- if (child_index + 1 >= i || Compare(child1_value, child2_value) > 0) {
- if (Compare(parent_value, child1_value) > 0) break;
- this[child_index] = parent_value;
- this[parent_index] = child1_value;
- parent_index = child_index;
- } else {
- if (Compare(parent_value, child2_value) > 0) break;
- this[child_index + 1] = parent_value;
- this[parent_index] = child2_value;
- parent_index = child_index + 1;
- }
- }
- }
+ var old_length = ToUint32(this.length);
- // We only changed the length of the this object (in
- // RemoveArrayHoles) if it was an array. We are not allowed to set
- // the length of the this object if it is not an array because this
- // might introduce a new length property.
- if (IS_ARRAY(this)) {
Mads Ager (chromium) 2008/09/24 12:34:43 This piece of code should still be needed. If we
- this.length = old_length;
- }
+ %RemoveArrayHoles(this);
+ var length = ToUint32(this.length);
mdakin 2008/09/25 14:25:28 Maybe using insertion sort for small arrays (<10 e
+ QuickSort(this, 0, length);
return this;
};
« 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