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

Unified Diff: src/array.js

Issue 1611021: Reimplement InsertSort to use simple linear search. (Closed)
Patch Set: Created 10 years, 8 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
diff --git a/src/array.js b/src/array.js
index c1ba3c399d904533a1613b86f07b90d76a906678..54d7e57b833b9e214892efd9d6f8006a41fb73fc 100644
--- a/src/array.js
+++ b/src/array.js
@@ -649,29 +649,16 @@ function ArraySort(comparefn) {
function InsertionSortWithFunc(a, from, to) {
for (var i = from + 1; i < to; i++) {
var element = a[i];
- // place element in a[from..i[
- // binary search
- var min = from;
- var max = i;
- // The search interval is a[min..max[
- 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;
+ for (var j = i - 1; j >= from; j--) {
+ var tmp = a[j];
+ var order = %_CallFunction(global_receiver, tmp, element, comparefn);
+ if (order > 0) {
+ a[j + 1] = tmp;
} else {
- max = mid;
+ break;
}
}
- // place element at position min==max.
- for (var j = i; j > min; j--) {
- a[j] = a[j - 1];
- }
- a[min] = element;
+ a[j + 1] = element;
}
}
@@ -712,8 +699,6 @@ function ArraySort(comparefn) {
}
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 (%_IsSmi(x) && %_IsSmi(y)) {
return %SmiLexicographicCompare(x, y);
@@ -727,32 +712,17 @@ function ArraySort(comparefn) {
function InsertionSort(a, from, to) {
for (var i = from + 1; i < to; 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);
Lasse Reichstein 2010/04/12 07:35:42 I'm very much attached to this line, but on the ot
antonm 2010/04/12 15:24:25 Sure. Sending to golem in no time.
- // place element in a[from..i[
- // binary search
- var min = from;
- var max = i;
- // The search interval is a[min..max[
- 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;
+ for (var j = i - 1; j >= from; j--) {
+ var tmp = a[j];
+ var order = Compare(tmp, key);
+ if (order > 0) {
+ a[j + 1] = tmp;
} else {
- max = mid;
+ break;
}
}
- // place element at position min==max.
- for (var j = i; j > min; j--) {
- a[j] = a[j - 1];
- }
- a[min] = element;
+ a[j + 1] = element;
}
}
« 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