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

PROGRAMMING:binary search

Luz5年前 (2021-05-10)题库453
There is an ordered integer array array with elements of 1,2,5,6,7,12,15,67,88,99100200. Enter an element a and use the binary search method to find the location of element a.
Dichotomy search is suitable for large amount of data, but the data needs to be arranged in order first.
The main idea is as follows: (let the array interval be array [low, high])
Low = 0 can be set at the beginning of the algorithm; High = n-1 (n is the number of one-dimensional array elements)
(1) Determine the middle position mid of the interval
(2) Compare the searched value a with array [mid]. If it is equal, mid is the search position; If low > high, the algorithm ends.
Otherwise, determine the new search area and continue the binary search.
The region is determined as follows: array [mid] > A. according to the order of the array, a may be array [low,..., mid-1] in the new interval
The search interval of array [mid] < A is array [mid + 1,..., high].
###Input format:
Enter the element a you want to find
###Output format:
If found, output found Find the location and the number of times to find, if not found, output not found!.
###Input example:
Here is a set of inputs. For example:
```in
twelve
```
###Output example:
The corresponding output is given here. For example:
```out
found! 5 1
```
###Input example:
Here is a set of inputs. For example:
```in
thirteen
```
###Output example:
The corresponding output is given here. For example:
```out
not found!
```







answer:If there is no answer, please comment