📜  使用伪代码实现对已排序整数数组的二进制搜索. - Python 代码示例

📅  最后修改于: 2022-03-11 14:45:41.764000             🧑  作者: Mango

代码示例2
# Here's the pseudocode for binary search, modified for searching in an array. The inputs are the array, which we call array; the number n of elements in array; and target, the number being searched for. The output is the index in array of target:

    1.Let min = 0 and max = n-1.
    2. Compute guess as the average of max and min, rounded down (so that it is an integer).
    3. If array[guess] equals target, then stop. You found it! Return guess.
    4. If the guess was too low, that is, array[guess] < target, then set min = guess + 1.
    5. Otherwise, the guess was too high. Set max = guess - 1.
    6. Go back to step 2.