Commit 0c4bac29 authored by antonm@chromium.org's avatar antonm@chromium.org

Reimplement InsertSort to use simple linear search.

And various minor cleanups.

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

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@4392 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent bc8f979e
...@@ -649,29 +649,16 @@ function ArraySort(comparefn) { ...@@ -649,29 +649,16 @@ function ArraySort(comparefn) {
function InsertionSortWithFunc(a, from, to) { function InsertionSortWithFunc(a, from, to) {
for (var i = from + 1; i < to; i++) { for (var i = from + 1; i < to; i++) {
var element = a[i]; var element = a[i];
// place element in a[from..i[ for (var j = i - 1; j >= from; j--) {
// binary search var tmp = a[j];
var min = from; var order = %_CallFunction(global_receiver, tmp, element, comparefn);
var max = i; if (order > 0) {
// The search interval is a[min..max[ a[j + 1] = tmp;
while (min < max) {
var mid = min + ((max - min) >> 1);
var order = %_CallFunction(global_receiver, a[mid], element, comparefn);
if (order == 0) {
min = max = mid;
break;
}
if (order < 0) {
min = mid + 1;
} else { } else {
max = mid; break;
} }
} }
// place element at position min==max. a[j + 1] = element;
for (var j = i; j > min; j--) {
a[j] = a[j - 1];
}
a[min] = element;
} }
} }
...@@ -712,8 +699,6 @@ function ArraySort(comparefn) { ...@@ -712,8 +699,6 @@ function ArraySort(comparefn) {
} }
function Compare(x,y) { function Compare(x,y) {
// Assume the comparefn, if any, is a consistent comparison function.
// If it isn't, we are allowed arbitrary behavior by ECMA 15.4.4.11.
if (x === y) return 0; if (x === y) return 0;
if (%_IsSmi(x) && %_IsSmi(y)) { if (%_IsSmi(x) && %_IsSmi(y)) {
return %SmiLexicographicCompare(x, y); return %SmiLexicographicCompare(x, y);
...@@ -727,32 +712,17 @@ function ArraySort(comparefn) { ...@@ -727,32 +712,17 @@ function ArraySort(comparefn) {
function InsertionSort(a, from, to) { function InsertionSort(a, from, to) {
for (var i = from + 1; i < to; i++) { for (var i = from + 1; i < to; i++) {
var element = a[i]; var element = a[i];
// Pre-convert the element to a string for comparison if we know
// it will happen on each compare anyway.
var key = %_IsSmi(element) ? element : ToString(element); var key = %_IsSmi(element) ? element : ToString(element);
// place element in a[from..i[ for (var j = i - 1; j >= from; j--) {
// binary search var tmp = a[j];
var min = from; var order = Compare(tmp, key);
var max = i; if (order > 0) {
// The search interval is a[min..max[ a[j + 1] = tmp;
while (min < max) {
var mid = min + ((max - min) >> 1);
var order = Compare(a[mid], key);
if (order == 0) {
min = max = mid;
break;
}
if (order < 0) {
min = mid + 1;
} else { } else {
max = mid; break;
} }
} }
// place element at position min==max. a[j + 1] = element;
for (var j = i; j > min; j--) {
a[j] = a[j - 1];
}
a[min] = element;
} }
} }
......
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