Sha256: 5807bd90f920446e1b0479ebd7f49403d139cbab5f6f8a5ed78e4247f7afe1ec

Contents?: true

Size: 523 Bytes

Versions: 10

Compression:

Stored size: 523 Bytes

Contents

# Beautiful Code, Chapter 6.
# The implementation of binary search that is tested.

# Return the index of an element in a sorted list. (or -1, if not present)
index = (list, target) ->
  [low, high] = [0, list.length]
  while low < high
    mid = (low + high) >> 1
    val = list[mid]
    return mid if val is target
    if val < target then low = mid + 1 else high = mid
  return -1

console.log 2 is index [10, 20, 30, 40, 50], 30
console.log 4 is index [-97, 35, 67, 88, 1200], 1200
console.log 0 is index [0, 45, 70], 0

Version data entries

10 entries across 10 versions & 2 rubygems

Version Path
spade-packager-0.1.0.1 packages/coffee-script/examples/beautiful_code/binary_search.coffee
spade-packager-0.1.0 packages/coffee-script/examples/beautiful_code/binary_search.coffee
spade-0.0.8.1 packages/coffee-script/examples/beautiful_code/binary_search.coffee
spade-0.0.7 packages/coffee-script/examples/beautiful_code/binary_search.coffee
spade-0.0.6 packages/coffee-script/examples/beautiful_code/binary_search.coffee
spade-0.0.5 packages/coffee-script/examples/beautiful_code/binary_search.coffee
spade-0.0.4 packages/coffee-script/examples/beautiful_code/binary_search.coffee
spade-0.0.3 packages/coffee-script/examples/beautiful_code/binary_search.coffee
spade-0.0.2 packages/coffee-script/examples/beautiful_code/binary_search.coffee
spade-0.0.1 packages/coffee-script/examples/beautiful_code/binary_search.coffee