SOURCE

function binary_search(arr, low, high, key) {
  if (low > high) {
    return - 1;
  }
  var mid = parseInt((high + low) / 2);
  if (arr[mid] == key) {
    return mid;
  } else if (arr[mid] > key) {
    high = mid - 1;
    return binary_search(arr, low, high, key);
  } else if (arr[mid] < key) {
    low = mid + 1;
    return binary_search(arr, low, high, key);
  }
};
var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 23, 44, 86];
var result = binary_search(arr, 0, 13, 10);
alert(result); // 9 返回目标元素的索引值 

            
console 命令行工具 X clear

                    
>
console