Didn"t forge 2018-10-30 05:48 采纳率: 25%
浏览 903

关于用 c 语言实现二进制搜索算法的一个查询

In Binary Search Algorithm,

in general

if   mid_value > search_element we set high = mid_pos-1 ;
else mid_value < search_element we set  low = mid_pos+1 ;

But I've just modified the algorithm like these

if   mid_value > search_element we set high = mid_pos ;
else mid_value < search_element we set  low = mid_pos ;

But my teacher told me that the standard algorithm for binary search is the first one and what you have written is also a search algorithm but it's not an algorithm for binary search. Is he correct?.

转载于:https://stackoverflow.com/questions/53058133/a-query-regarding-the-algorithm-of-binary-search-using-c

  • 写回答

2条回答 默认 最新

  • 衫裤跑路 2018-10-30 06:02
    关注

    I think you picked it up wrongly .

    The basic Binary Search Algorithm's workflow:

    Procedure binary_search
       A ← sorted array
       n ← size of array
       x ← value to be searched
    
       Set lowerBound = 1
       Set upperBound = n 
    
       while x not found
          if upperBound < lowerBound 
             EXIT: x does not exists.
    
          set midPoint = lowerBound + ( upperBound - lowerBound ) / 2
    
          if A[midPoint] < x
             set lowerBound = midPoint + 1
    
          if A[midPoint] > x
             set upperBound = midPoint - 1 
    
          if A[midPoint] = x 
             EXIT: x found at location midPoint
       end while
    
    end procedure
    

    Here you can see what actually midPoint = lowerBound + ( upperBound - lowerBound ) / 2 , lowerBound = midPoint + 1 and upperBound = midPoint - 1 does .

    评论

报告相同问题?

悬赏问题

  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程