• 【算法导论】二分查找


    1. 描述(查找算法):

      输入:n个数的一个序列 A = (a1, a2, a3,.....an)和一个值v

      输出:下表 i 使得 v=A[i] 或者 v 不在A中出现时,输出 NIL

      二分查找的前提是A必须是有序序列, 以下全部假设是A是非降序序列

    2. 图解

      

    3. 伪代码

      

    //用递归
    BINARY_SEARCH(A, v, low, high)
        if(low <= high)
            mid = (low+high)/2  //向下取整
            if v == A[mid]
                return mid;
            else if v < A[mid]
                return BINARY_SEARCH(A, v, low, mid-1)
            else if v > A[mid]
                return BINARY_SEARCH(A, v, mid+1, high)
      return NIL
    
    //用循环
    BINARY_SEARCH(A, v)
        low = 1
        high = A.length
        while low <= high
            mid = (low + high)/2 //向下取整
            if v == A[mid]
                return mid
            else if v < A[mid]
                high = mid -1
            else if v > A[mid]
                low = mid +1
      
    return NIL

    4. 代码实现

    java

    //用循环
        int binarySearch1(int[] A, int v){
            int low = 0;
            int high = A.length;
            while (low <= high) {
                int mid = (low + high)/2;
                if (v == A[mid])
                    return mid;
                else if (v < A[mid])
                    high = mid - 1;
                else if (v > A[mid])
                    low = mid + 1;
            return -1;
        }
    
        //用递归
        int binarySearch2(int[] A, int v,  int low, int high) {
            if (low <= high) {
                int mid = (low + high)/2;
                if (v == A[mid])
                    return mid;
                else if (v < A[mid])
                    return binarySearch2(A, v, low, mid - 1);
                else if (v > A[mid])
                    return binarySearch2(A, v, mid + 1, high);
            return -1;
        }

    python

    # 用循环
    def binary_search1(nums, v):
        low = 0
        high = len(nums)
        while low <= high:
            mid = int((low + high) / 2)
            if v == nums[mid]:
                return mid
            else:
                if v <= nums[mid]:
                    high = mid - 1
                else:
                    if v > nums[mid]:
                        low = mid + 1
        return -1
    
    
    # 用递归
    def binary_search2(nums, v, low, high):
        if low <= high:
            mid = int((low + high) / 2)
            if v == nums[mid]:
                return mid
            else:
                if v < nums[mid]:
                    return binary_search2(nums, v, low, mid - 1)
                else:
                    if v > nums[mid]:
                        return binary_search2(nums, v, mid + 1, high)
        return -1

    C语言

    //用循环 
     int binary_search1(int arr[], int v, int len)
     {
         int low, mid, high;
         low = 0, high = len-1;
         while (low <= high)
         {
             mid = (low + high) / 2;
             if (v == arr[mid])
                 return v;
             else if (v > arr[mid])
                 low = mid + 1;
             else if (v < arr[mid])
                 high = mid -1;
         }
         return -1;
     }
     
     //用递归 
     int binary_search2(int arr[], int v, int low, int high)
     {
         int mid;
         if (low <= high)
         {
             mid = (low + high) / 2;
             if (v == arr[mid])
                 return v;
             else if (v > arr[mid])
                 return binary_search2(arr, v, mid + 1, high);
             else if (v < arr[mid])
                 return binary_search2(arr, v, low, mid - 1);
         }
         return -1;
     }

        

  • 相关阅读:
    倍福TwinCAT(贝福Beckhoff)常见问题(FAQ)-如何把FBD功能块转换成ST语言
    揭秘一个操作灰色关键词牟取暴利的案例
    [原创汉化] 价值990美元的顶级专业数据恢复软件O&O DiskRecovery 11(技术员版)汉化绿色版
    list集合如何对里面的元素进行排序
    jquery datables ajax分页后的点击事件无效是怎么回事
    阿里云自动快照有什么用,如何设置?
    php后台管理员权限相关表结构
    服务器上装了安全狗后远程链接不上怎么解决
    Ehcache配置参数简介
    Spring+EhCache缓存实例
  • 原文地址:https://www.cnblogs.com/yeyeck/p/9562768.html
Copyright © 2020-2023  润新知