1. 程式人生 > 實用技巧 >6.二分查詢

6.二分查詢

模板實現

二分查詢的情況如圖所示

模板如下:

while (left <= right) {
    mid = (left + right) / 2;
    if (key ? arr[mid]) {
        right = mid - 1;
    } else {
        left = mid + 1;
    }
}
return ?;

只需要記住比較符號和返回值即可:
根據要求值的位置,可以先確定比較符號,再確定返回值。
比較符號:左<=, 右<;(左右方向)
返回值: 左right, 右left;(位置,這個最好確定)

#include <bits/stdc++.h>
using namespace std;

//二分查詢
int binarySearch(int arr[], int len, int key)
{
    int left = 0;
    int right = len - 1;
    int mid;

    while (left <= right) {
        mid = (left + right) / 2;
        if (key < arr[mid]) {//key在左邊
            right = mid - 1;
        } else if (arr[mid] < key) {//key在右邊
            left = mid + 1;
        } else {
            return mid;
        }
    }
    return -1;
}

//1 查詢最後一個小於key的元素
int findLastLess(int arr[], int len, int key)
{
    int left = 0;
    int right = len - 1;
    int mid;

    while (left <= right) {
        mid = (left + right) / 2;
        if (key <= arr[mid]) {
            right = mid - 1;
        } else {
            left = mid + 1;
        }
    }
    return right;
}

//2 查詢第一個大於等於key的元素
int findFirstGreaterEqual(int arr[], int len, int key)
{
    int left = 0;
    int right = len - 1;
    int mid;

    while (left <= right) {
        mid = (left + right) / 2;
        if (key <= arr[mid]) {
            right = mid - 1;
        }
        else {
            left = mid + 1;
        }
    }
    return left;
}

//3 查詢最後一個小於等於key的元素
int findLastLessEqual(int arr[], int len, int key)
{
    int left = 0;
    int right = len - 1;
    int mid;

    while (left <= right) {
        mid = (left + right) / 2;
        if (key < arr[mid]) {
            right = mid - 1;
        } else {
            left = mid + 1;
        }
    }
    return right;
}

//4 查詢第一個大於key的元素
int findFirstGreater(int arr[], int len, int key)
{
    int left = 0;
    int right = len - 1;
    int mid;

    while (left <= right) {
        mid = (left + right) / 2;
        if (key < arr[mid]) {
            right = mid - 1;
        }
        else {
            left = mid + 1;
        }
    }
    return left;
}

//5 查詢第一個與key相等的元素
int findFirstEqual(int arr[], int len, int key)
{
    int left = 0;
    int right = len - 1;
    int mid;

    while (left <= right) {
        mid = (left + right) / 2;
        if (key <= arr[mid]) {
            right = mid - 1;
        } else {//arr[mid] < key
            left = mid + 1;
        }
    }
    //arr[right] < key <= arr[left]
    //right是最後一個小於key的
    //left是第一個大於等於key的
    if (left < len && arr[left] == key) {
        return left;
    }
    return -1;
}

//6 查詢最後一個與key相等的元素
int findLastEqual(int arr[], int len, int key)
{
    int left = 0;
    int right = len - 1;
    int mid;

    while (left <= right) {
        mid = (left + right) / 2;
        if (key < arr[mid]) {
            right = mid - 1;
        } else {//arr[mid] <= key
            left = mid + 1;
        }
    }
    //arr[right] <= key < arr[left]
    //right是最後一個小於等於key的
    //left是第一個大於key的
    if (right >= 0 && arr[right] == key) {
        return right;
    }
    return -1;
}


void test()
{
//  int a[] = {0, 1, 2, 3, 4, 5, 6};
    int a[] = {0, 1, 2, 2, 2, 5, 6};
    int len = 7;
    int key = 2;

    int index;

    int i;
    for (i = 0; i < len; ++i) {
        printf("%d ", i);
    }
    printf("\n");
    for (i = 0; i < len; ++i) {
        printf("%d ", a[i]);
    }
    printf("\n");

//    index = binarySearch(a, 10, 2);
    printf("%d binarySearch\n", binarySearch(a, len, key));
    printf("%d findLastLess\n", findLastLess(a, len, key));
    printf("%d findFirstGreaterEqual\n", findFirstGreaterEqual(a, len, key));
    printf("%d findLastLessEqual\n", findLastLessEqual(a, len, key));
    printf("%d findFirstGreater\n", findFirstGreater(a, len, key));
    printf("%d findFirstEqual\n", findFirstEqual(a, len, key));
    printf("%d findLastEqual\n", findLastEqual(a, len, key));
}

int main()
{
    test();
    return 0;
}

參考連結:
二分查詢的總結,這個連結絕了