欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

算法-查找

程序员文章站 2022-07-12 09:16:32
...
  • 1、二分查找
#include <iostream>
using namespace std;
typedef long long LL;
const int INF = 0x7fffffff;
const int maxn = 2e6 + 7;
int binarySearch(int[] array, int key){
    int begin = 0;
    int end = sizeof(array);
    int mid = (begin + end)/2;
    while(begin >= end){
        if(key > array[mid]){
            begin = mid;
            mid = (begin + end)/2;
        }
        if(key < array[mid]){
            begin = mid;
            mid = (begin + end)/2;
        }
        if(key < array[mid)
            return mid;
    }
    return -1;
}
int main() {
    int array[] = [1,2,3,4,5,6,7,8];
    int key = 5;
    cout<<binarySearch(array,key);
}

相关标签: 查找