zoukankan      html  css  js  c++  java
  • 算法之二分查找

    二分查找思想:

    1、数组从小到大排序;2、查找的key每次和中间数比较,如果key小于mid ,查找mid左侧的数组部分;如果key大于mid,则查找mid右侧的数组部分;如果相等,则直接返回mid

    //非递归查找
    int BinarySearch(int *array, int aSize, int key)
    {
        if ( array == NULL || aSize == 0 )
            return -1;
        int low = 0;
        int high = aSize - 1;
        int mid = 0;

       while ( low <= high )
        {
    mid = (low + high )/2;

    if ( array[mid] < key)
    low = mid + 1;
    else if ( array[mid] > key )
    high = mid - 1;
    else
    return mid;
    }
    return -1;
    }
    //递归
    int BinarySearchRecursive(int *array, int low, int high, int key)
    {
    if ( low > high )
    return -1;
    int mid = ( low + high )/2;

    if ( array[mid] == key )
    return mid;
    else if ( array[mid] < key )
    return BinarySearchRecursive(array, mid+1, high, key);
    else
    return BinarySearchRecursive(array, low, mid-1, key);
    }

    int main()
    {
    int array[10];
    for (int i=0; i<10; i++)
    array[i] = i;

    cout<<"No recursive:"<<endl;
    cout<<"position:"<<BinarySearch(array, 10, 6)<<endl;
    cout<<"recursive:"<<endl;
    cout<<"position:"<<BinarySearchRecursive(array, 0, 9, 6)<<endl;

    return 0;
    }

  • 相关阅读:
    list集合对象日期排序
    Mongodb模糊,or,and查询和日期查询
    单例模式
    代理模式
    抽象工厂模式
    java 除数运算获取两位小数
    html5 canvas 使用总结
    @MockBean 注解后 bean成员对象为 null?
    Java8 BiFunction 简单用用
    如何正确安装Ubuntu
  • 原文地址:https://www.cnblogs.com/zyber/p/7349574.html
Copyright © 2011-2022 走看看