html 在数组javascript中搜索

gtlvzcf8  于 2023-03-16  发布在  Java
关注(0)|答案(3)|浏览(175)

我想在数组中查找元素问题是当数组的大小为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
}
goucqfw6

goucqfw61#

我有这个代码,我写了我的开源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));
yhxst69z

yhxst69z2#

var array1 = [5, 12, 8, 130, 44];

var found = array1.find(function(element) {
  return element > 10;
});

console.log(found);
// expected output: 12

如何在数组中搜索请参见link

ozxc1zmp

ozxc1zmp3#

请尝试以下操作:

const tab = [1,2,3,4]

console.log(binarySearch(tab,4))//3

function binarySearch(arr,item){

  for (let i = 0; i < arr.length; i++) {

    if(arr[i]===item){

      return i

    }
  }
}

相关问题