c++ - Binary search for insert char string. Where is the bug? -
i have array of strings. must find 1 char string in array of strings binary search algoritm. if there 1 string function must return position , return true otherwise function must return position insert string in array , false. have somewhere bug, dont know ((
example:
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 found in array, function return position in array of strings , return true position = middle; return true; } } // if search_string not found in array, function must return position insert string , return false position = middle; return false; }
i think maybe should be:
if ( for_compare > 0 ) { end = middle - 1; } else if ( for_compare < 0 ) { start = middle + 1; }
Comments
Post a Comment