查找算法-二分查找

概念

二分查找的思路是很簡單的,前提是這組數據是有順序的。 思路是從中間找一個數,判斷大小,如果數比中間數大,說明在中間數到結尾的數中,如果小於,則說明在開始和中間數之間,經過多次相同操作,就可以得到我們想查找的數

時間複雜度

時間複雜度就是 O(logn)

代碼實現

  1. 非遞歸的實現
  const testArr = []
    let i = 0
    while (i < 10000) {
      testArr.push(i)
      i++
    }
    console.log(testArr)
    let a = bsearch(testArr, testArr.length, 5554)
    console.log(a)

    function bsearch(arr, n, value) {
      let low = 0
      let hight = n - 1
      let num = 0

      while (low <= hight) {
        num++
        console.log(num)
        let mid = parseInt((low + hight) / 2)
        if (arr[mid] === value) {
          return mid
        } else if (arr[mid] < value) {
          low = mid + 1
        } else {
          hight = mid - 1
        }
      }

      return -1
    }
  1. 遞歸實現
   const testArr = []
    let i = 0
    while (i < 10000) {
      testArr.push(i)
      i++
    }
    console.log(testArr)
    let a = bsearch(testArr, testArr.length, 5554)
    console.log(a)

    function bsearch(arr, n, value) {
      return bsearchInternally(arr, 0, n - 1, value)
    }

    function bsearchInternally(a, low, hight, value) {
      if (low > hight) {
        return -1
      }
      let mid = parseInt((low + hight) / 2)
      if (a[mid] === value) {
        return mid
      } else if (a[mid] < value) {
        return bsearchInternally(a, mid + 1, hight, value)
      } else {
        return bsearchInternally(a, 0, hight - 1, value)
      }
    }

二分查找應用場景的侷限性

  1. 二分查找依賴的是順序表結構,簡單點說就是數組。
  2. 二分查找針對的是有序數據
  3. 數據量太小不適合二分查找
  4. 數據量太大也不適合二分查找 (數組需要連續的存儲空間,太大的數據需要開闢很大的內存)

變形問題

1. 查找第一個等於給定值的元素
2. 查找最後一個等於給定值的元素
3. 查找第一個大於等於給定值的元素
4. 查找最後一個小於等於給定值的元素
  1. 查找第一個等於給定值的元素

 const testArr = []
    let i = 0
    while (i < 10000) {
      if (i === 5554) {
        testArr.push(i)
        testArr.push(i)
        testArr.push(i)
        testArr.push(i)
        testArr.push(i)
        testArr.push(i)
      } else {
        testArr.push(i)
      }
      i++
    }
    console.log(testArr)
    let a = bsearch(testArr, testArr.length, 5554)
    console.log(a)

    function bsearch(a, n, value) {
      let low = 0;
      let high = n - 1;
      while (low <= high) {
        let mid = low + ((high - low) >> 1);
        if (a[mid] > value) {
          high = mid - 1;
        } else if (a[mid] < value) {
          low = mid + 1;
        } else {
          // 等於的時候再做一次處理
          if ((mid == 0) || (a[mid - 1] != value)) return mid;
          else high = mid - 1;
        }
      }
      return -1;
    }

  1. 查找最後一個等於給定值的元素
const testArr = []
    let i = 0
    while (i < 10000) {
      if (i === 5554) {
        testArr.push(i)
        testArr.push(i)
        testArr.push(i)
        testArr.push(i)
        testArr.push(i)
        testArr.push(i)
      } else {
        testArr.push(i)
      }
      i++
    }
    console.log(testArr)
    let a = bsearch(testArr, testArr.length, 5554)
    console.log(a)

    function bsearch(a, n, value) {
      let low = 0;
      let high = n - 1;
      while (low <= high) {
        let mid = low + ((high - low) >> 1);
        if (a[mid] > value) {
          high = mid - 1;
        } else if (a[mid] < value) {
          low = mid + 1;
        } else {
          // 等於的時候再做一次處理
          if ((mid == n - 1) || (a[mid + 1] != value)) {
            return mid;
          } else {
            low = mid + 1;
          }
        }
      }
      return -1;
    }
    
  1. 查找第一個大於等於給定值的元素

    const testArr = []
    let i = 0
    while (i < 10000) {
      if (i === 5554) {
        testArr.push(i)
        testArr.push(i)
        testArr.push(i)
        testArr.push(i)
        testArr.push(i)
        testArr.push(i)
      } else {
        testArr.push(i)
      }
      i++
    }
    console.log(testArr)
    let a = bsearch(testArr, testArr.length, 5554)
    console.log(a)

    function bsearch(a, n, value) {
      let low = 0;
      let high = n - 1;
      while (low <= high) {
        let mid = low + ((high - low) >> 1);
        if (a[mid] >= value) {
          if ((mid == 0) || (a[mid - 1] < value)) return mid; else high = mid - 1;
        } else {
          low = mid + 1;
        }
      }
      return -1;
    }

  1. 查找最後一個小於等於給定值的元素
 const testArr = []
    let i = 0
    while (i < 10000) {
      if (i === 5554) {
        testArr.push(i)
        testArr.push(i)
        testArr.push(i)
        testArr.push(i)
        testArr.push(i)
        testArr.push(i)
      } else {
        testArr.push(i)
      }
      i++
    }
    console.log(testArr)
    let a = bsearch(testArr, testArr.length, 5554)
    console.log(a)

    function bsearch(a, n, value) {
      let low = 0;
      let high = n - 1;
      while (low <= high) {
        let mid = low + ((high - low) >> 1);
        if (a[mid] > value) {
          high = mid - 1;
        } else {
          if ((mid == n - 1) || (a[mid + 1] > value)) return mid; else low = mid + 1;
        }
      }
      return -1;
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章