我有字符串数组。我必须通过二进制搜索算法在字符串数组中找到一个字符字符串。如果存在这个字符串,则函数必须返回位置并返回真,否则该函数必须返回在数组中插入字符串的位置和假。我在某个地方有错误,但我不知道在哪里((
例子:
bool Binary_search ( char * arr_strings[], int & position, const char * search_string )
{
int start = 0 ;
int end = 10 - 1; // arr_strings [10]
int for_compare;
int middle;
while ( start <= end )
{
middle = ( start + end ) / 2;
for_compare = strcmp ( arr_strings[middle], search_string );
if ( for_compare > 0 )
{
start = middle + 1;
}
else if ( for_compare < 0 )
{
end = middle - 1;
}
else
{
// if search_string is found in array, then function return position in array of strings and return true
position = middle;
return true;
}
}
// if search_string is not found in array, then function must return position for insert string and return false
position = middle;
return false;
}