Array.prototype.search = function(value) { var arr = this; function search(arr, value, start, end) { if (start > end) { return -1; } else { var middle = start + end >> 1; if (value > arr[middle]) { return search(arr, value, middle + 1, end) } else if (value < arr[middle]) { return search(arr, value, start, middle - 1); } else if (value === arr[middle]) { return middle; } else { return -1; } } } return search(arr, value, 0, arr.length - 1); }
Array.prototype.search = function(value) { var arr = this; function search(arr, value, start, end) { if (start > end) { return -1; } else { var middle = start + end >> 1; if (value > arr[middle]) { return search(arr, value, middle + 1, end) } else if (value < arr[middle]) { return search(arr, value, start, middle - 1); } else if (value === arr[middle]) { return middle; } else { return -1; } } } return search(arr, value, 0, arr.length - 1); }
时间: 2024-10-10 16:37:14