对于有效的二分搜索插入,您需要让二分搜索返回一些内容,以指示如果未找到该字符串在数组中的位置。
在其他语言中执行此操作的公认方法是返回字符串所属索引的按位补码。0 的按位补码是 -1,1 的按位补码是 -2,2 是 -3,以此类推。要在 JavaScript 中获取数字的按位补码,请使用~
运算符。
示例代码:
/*
target: the object to search for in the array
comparator: (optional) a method for comparing the target object type
return value: index of a matching item in the array if one exists, otherwise the bitwise complement of the index where the item belongs
*/
Array.prototype.binarySearch = function (target, comparator) {
var l = 0,
h = this.length - 1,
m, comparison;
comparator = comparator || function (a, b) {
return (a < b ? -1 : (a > b ? 1 : 0)); /* default comparison method if one was not provided */
};
while (l <= h) {
m = (l + h) >>> 1; /* equivalent to Math.floor((l + h) / 2) but faster */
comparison = comparator(this[m], target);
if (comparison < 0) {
l = m + 1;
} else if (comparison > 0) {
h = m - 1;
} else {
return m;
}
}
return~l;
};
然后您可以使用 binarySearch 方法编写您自己的 binaryInsert 函数:
/*
target: the object to insert into the array
duplicate: (optional) whether to insert the object into the array even if a matching object already exists in the array (false by default)
comparator: (optional) a method for comparing the target object type
return value: the index where the object was inserted into the array, or the index of a matching object in the array if a match was found and the duplicate parameter was false
*/
Array.prototype.binaryInsert = function (target, duplicate, comparator) {
var i = this.binarySearch(target, comparator);
if (i >= 0) { /* if the binarySearch return value was zero or positive, a matching object was found */
if (!duplicate) {
return i;
}
} else { /* if the return value was negative, the bitwise complement of the return value is the correct index for this object */
i = ~i;
}
this.splice(i, 0, target);
return i;
};
一旦将这些方法原型化到数组对象上,您就可以像这样直接使用它们:
var arr = [];
arr.binaryInsert("Zebra");
arr.binaryInsert("Aardvark");
arr.binaryInsert("Mongoose");
alert(arr);
/* [ "Aardvark", "Mongoose", "Zebra" ] */
随着项目数量的增加,这将比调用快得多Array.sort()
数组属性键污染
请注意,如上述代码中的 Array 对象的原型方法会导致方法显示为数组的可枚举属性,这可能会干扰您在for(var i in arr)
循环中枚举所有属性的任何逻辑。以 的格式编写的循环for(var i=0; i<arr.length; i++)
仍将按设计工作。
如果您不需要支持 Internet Explorer 8 或更低版本,您可以避免Array.prototype
直接调用,而是使用Object.defineProperty
以下示例。
Object.defineProperty(Array.prototype, "binarySearch", {
value: function (target, comparator) {
var l = 0,
h = this.length - 1,
m, comparison;
comparator = comparator || function (a, b) {
return (a < b ? -1 : (a > b ? 1 : 0));
};
while (l <= h) {
m = (l + h) >>> 1;
comparison = comparator(this[m], target);
if (comparison < 0) {
l = m + 1;
} else if (comparison > 0) {
h = m - 1;
} else {
return m;
}
}
return~l;
}
});
Object.defineProperty(Array.prototype, "binaryInsert", {
value: function (target, duplicate, comparator) {
var i = this.binarySearch(target, comparator);
if (i >= 0) {
if (!duplicate) {
return i;
}
} else {
i = ~i;
}
this.splice(i, 0, target);
return i;
}
});
这种方法将避免污染可枚举键,因此for(var i in arr)
循环仍将按预期工作。