-->
当前位置:首页 > 题库

PROGRAMMING:Rewriting binary search algorithm

Luz5年前 (2021-05-10)题库476
Title Source: computer algorithm design and analysis, Wang Xiaodong
Let a [0: n-1] be an ordered array, rewrite * * binary search algorithm * * so that when x is not in the array, the maximum element position I less than X and the minimum element position J greater than X are returned. When the search element is in the array, I and j are the same, which are the positions of X in the array.
###Input format:
There are two lines of input:
The first line is n value and x value;
The second line is a non descending sequence of n different integers separated by spaces.
###Output format:
Outputs the maximum subscript i of the largest element less than X and the minimum subscript j of the smallest element greater than x. When the search element is in the array, I and j are the same.
Prompt: if x is less than all values, then output: - 10
If x is greater than all the values, then output the value of n-1 and the value of n
###Input example:
Here is a set of inputs. For example:
```in
6 5
2 4 6 8 10 12
```
###Output example:
The corresponding output is given here. For example:
```out
1 2
```







answer:If there is no answer, please comment