1. Array.prototype.binarySearch = function(item) {
    2. let low = 0;
    3. let high = this.length - 1;
    4. while(low <= high) {
    5. const mid = Math.floor((low + high) / 2);
    6. const element = this[mid];
    7. if(element < item) {
    8. low = mid + 1
    9. } else if (element > item) {
    10. high = mid - 1
    11. } else {
    12. return mid
    13. }
    14. }
    15. return -1
    16. }
    17. const res = [1, 2, 3, 4, 5].binarySearch(3);
    18. console.log(res);

    时间复杂度:O(logN)