#include <iostream> using namespace std; // Binary search method, find the elements of an array, and return the index of the position, // must be an ordered array, int select_arr ( int arr [], int len , int arr_value) { while ( 1 ) { int left = 0 ; // left subscript of the array int right = len- 1 ; // right subscript of the array while (left <= right) { int mid = ( left + right) / 2 ; // define the middle index int mid_value = arr [mid]; // define the reference value of the intermediate value if (mid_value == arr_value) // if the reference value is exactly equal to the value to be found, then automatically return to the location you want { return mid; } // if The reference value is greater than the value to be searched, indicating that the value is on the left half, and the new search range is the middle value -1 digits, which is mid-1 else if (mid_value> arr_value) { right = mid- 1 ; } // if the reference value Less than the value to be searched, indicating that the value is on the right half, the new search range is the middle number +1 digits, which is mid + 1; else if (mid_value < arr_value) { left = mid + 1; } } return - 1 ; } } int main () { // define an int array and use binary search for int arr [ 10 ] = { 1 , 3 , 5 , 7 , 9 , 10 , 16 , 46 , 88 , 91 }; int weizhi = select_arr (arr, 10 , 16 ); return 0 ; }