关于javascript:JS-二分法定位左右边界

46次阅读

共计 1302 个字符,预计需要花费 4 分钟才能阅读完成。

递归查找

function erfen_digui(arr, val, left = 0, right = arr.length - 1) {if (left > right) {return -1;}
        let cent = Math.floor((right + left) / 2);
        if (arr[cent] === val) {return ` 最终查找后果下标为 ${cent}`;
        } else if (arr[cent] > val) {right = cent - 1;} else {left = cent + 1;}
        return erfen_digui(arr, val, left, right);
      }

非递归形式

function erfen_feidigui(arr, val) {
        let left = 0,
          right = arr.length - 1;
        while (left <= right) {let cent = left + Math.floor((right - left) / 2);
          if (arr[cent] === val) {return ` 最终查找后果下标为 ${cent}`;
          } else if (arr[cent] > val) {right = cent - 1;} else {left = cent + 1;}
        }
        return -1;
      }

定位左边界(查找第一个元素)

function erfen_digui(arr, val, left = 0, right = arr.length - 1) {if (left > right) {return -1;}
        let cent = Math.floor((right + left) / 2);
        if (arr[cent] === val) {
          /**************** 改变点 ********************/
          if (arr[cent - 1] === val) {right = cent - 1;} else {return ` 最终查找后果下标为 ${cent}`;
          }
          /*****************************************/
        } else if (arr[cent] > val) {right = cent - 1;} else {left = cent + 1;}
        return erfen_digui(arr, val, left, right);
      }

二分查找右边界(查找最初一个元素)

function erfen_digui(arr, val, left = 0, right = arr.length - 1) {if (left > right) {return -1;}
        let cent = Math.floor((right + left) / 2);
        if (arr[cent] === val) {
          /**************** 改变点 ********************/
          if (arr[cent + 1] === val) {left = cent + 1;} else {return ` 最终查找后果下标为 ${cent}`;
          }
          /*****************************************/
        } else if (arr[cent] > val) {right = cent - 1;} else {left = cent + 1;}
        return erfen_digui(arr, val, left, right);
      }

### 二分查找须要留神左右边界的状况

正文完
 0