Commit e61b8034 authored by olehougaard's avatar olehougaard

Various minor improvements of sort.

Review URL: http://codereview.chromium.org/6035

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@405 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent 8d617a7b
...@@ -650,14 +650,10 @@ function ArraySort(comparefn) { ...@@ -650,14 +650,10 @@ function ArraySort(comparefn) {
// In-place QuickSort algorithm. // In-place QuickSort algorithm.
// For short (length <= 22) arrays, insertion sort is used for efficiency. // For short (length <= 22) arrays, insertion sort is used for efficiency.
var custom_compare = IS_FUNCTION(comparefn);
function Compare(x,y) { function Compare(x,y) {
if (IS_UNDEFINED(x)) { if (custom_compare) {
if (IS_UNDEFINED(y)) return 0;
return 1;
}
if (IS_UNDEFINED(y)) return -1;
if (IS_FUNCTION(comparefn)) {
return comparefn.call(null, x, y); return comparefn.call(null, x, y);
} }
if (%_IsSmi(x) && %_IsSmi(y)) { if (%_IsSmi(x) && %_IsSmi(y)) {
...@@ -691,15 +687,13 @@ function ArraySort(comparefn) { ...@@ -691,15 +687,13 @@ function ArraySort(comparefn) {
} }
} }
// place element at position min==max. // place element at position min==max.
for (var j = min; j < i; j++) { for (var j = i; j > min; j--) {
var tmp = a[j]; a[j] = a[j - 1];
a[j] = element;
element = tmp;
} }
a[i] = element; a[min] = element;
} }
} }
function QuickSort(a, from, to) { function QuickSort(a, from, to) {
// Insertion sort is faster for short arrays. // Insertion sort is faster for short arrays.
if (to - from <= 22) { if (to - from <= 22) {
...@@ -743,6 +737,18 @@ function ArraySort(comparefn) { ...@@ -743,6 +737,18 @@ function ArraySort(comparefn) {
%RemoveArrayHoles(this); %RemoveArrayHoles(this);
var length = ToUint32(this.length); var length = ToUint32(this.length);
// Move undefined elements to the end of the array.
for (var i = 0; i < length; ) {
if (IS_UNDEFINED(this[i])) {
length--;
this[i] = this[length];
this[length] = undefined;
} else {
i++;
}
}
QuickSort(this, 0, length); QuickSort(this, 0, length);
// We only changed the length of the this object (in // We only changed the length of the this object (in
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment