我想在数组中查找项目我的问题是当数组的大小为4或6时它返回-1
以下是我的测试:
var tab = [1,2,3] ;
binarySearch(tab,3);
2
var tab = [1,2,3,4,5];
binarySearch(tab,3);
2
binarySearch(tab,2);
-1
}
答案 0 :(得分:0)
我有我为开源forensic js库编写的代码。这就是我如何进行二进制搜索我的数组包含实用程序方法来帮助搜索数组中的所有类型的数据。希望你觉得它有用。我只是复制了部分并对其进行了一些修改。
/**
*@description - default callback sorting function
*@param {*} value1 - comparison arg 1
*@param {*} value2 - comparison arg 2
*@returns {Number}
*/
var FN_SORT = function (value1, value2) {
var type1 = typeof value1,
type2 = typeof value2;
if (type1 === 'number' && type2 === 'number') {
return value1 - value2;
}
else if (type1 === 'number') {
return -1;
}
else if (type2 === 'number') {
return 1;
}
else if (value1 < value2) {
return -1;
}
else if (value1 > value2) {
return 1;
}
else {
return 0;
}
},
/**
*@description - default callback searching function
*@param {*} value1 - comparison arg 1
*@param {*} value2 - comparison arg 2
*@param {Boolean} [casesensitive=false] - boolean value indicating if the comparison is case sensitive or not
*@returns {Number}
*/
FN_SERACH = function (value1, value2, casesensitive) {
if (!casesensitive) {
value1 = typeof value1 === 'string' ? value1.toLowerCase() : value1;
value2 = typeof value2 === 'string' ? value2.toLowerCase() : value2;
}
var type1 = typeof value1,
type2 = typeof value2;
if (type1 === 'number' && type2 === 'number') {
return value1 - value2;
}
else if (type1 === 'number') {
return -1;
}
else if (type2 === 'number') {
return 1;
}
else if (value1 < value2) {
return -1;
}
else if (value1 > value2) {
return 1;
}
else {
return 0;
}
},
/**
*@memberof Forensic#util
*@description - searches an Array for a key. returns key index position if found or -1 if not found
*@param {Array} array - array to search from
*@param {mixed} key - key to search
*@param {Boolean} [casesensitive=false] - a boolean value indicating if key or item search should respect case
*@param {Function} [fnsort] - sorting function which is optional. your sorting function must return a number, while it accepts two
*arguments, the first value, second value.
*the sorting function should return -1 if argument one should come before argument two, 1 if argument two should come before argument
*one, zero if the order is unimportant
*@param {Function} [fnsearch] - searching function which is optional. your searching function must return a number, while it accepts three
*arguments, the first value is the value being searched for, second value, a third casesensitive value as you specified in argument two.
*the searching function should return -1 if argument one should come before argument two, 1 if argument two should come before argument
*one, zero if the two are equal. it may seem redundant having to specify sorting and searching function at the same time. it is done for
*performance reasons, sorting functions will not require case conversion while searching functions may require case conversion
*@returns {Number}
*@Exception {Exception} argument one is not an Array
*/
arrayContains = function (array, key, casesensitive, fnsort, fnsearch) {
if (typeof key === 'undefined' || array.length === 0) {
return -1;
}
fnsort = fnsort ? fnsort : FN_SORT;
fnsearch = fnsearch ? fnsearch : FN_SERACH;
var low = 0,
high = array.length - 1,
middle = parseInt((high + low + 1) / 2, 10),
locationindex = -1,
searchindex = 0;
array.sort(fnsort);
do {
searchindex = fnsearch(key, array[middle], casesensitive);
if (searchindex === 0) {
locationindex = middle;
}
else {
if (searchindex < 0) {
high = middle - 1;
}
else {
low = middle + 1;
}
middle = parseInt((high + low + 1) / 2, 10);
}
}
while (low <= high && locationindex === -1);
return locationindex;
};
var array = [0, 0, 0, 3];
alert(arrayContains(array, 3));
答案 1 :(得分:0)
var array1 = [5, 12, 8, 130, 44];
var found = array1.find(function(element) {
return element > 10;
});
console.log(found);
// expected output: 12
您可以在数组中搜索 link