# binary_search.r
# 来源 / Source: https://rosettacode.org/wiki/Binary_search
# 许可 / License: Rosetta Code 内容按 GNU FDL 1.3 发布（页面未另标许可）
# 说明 / Note: 迭代版 + 同页的调用示例 / iterative version + the usage lines from the same page
# ─────────────────────────────────────────────────────────────
IterBinSearch <- function(A, value) {
  low = 1
  high = length(A)
  i = 0
  while ( low <= high ) {
    mid <- floor((low + high)/2)
    if ( A[mid] > value )
      high <- mid - 1
    else if ( A[mid] < value )
      low <- mid + 1
    else
      return(mid)
  }
  NULL
}

a <- 1:100
IterBinSearch(a, 50)
IterBinSearch(a, 101)
