Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This binarySearch function performs a binary search on a array of strings srray,

ID: 3581351 • Letter: T

Question

This binarySearch function performs a binary search on a array of strings srray, which has a maximum called size for its maximum number of elements, and is searched for the string stored in the parameter passed called value. It returns an integer with contains the position. Given the varaibles defined, complete the logic for this binary search to perform the necessary aperation.

The while statement is the start of the logic:

int binarySearch(string values[], int size, string value)
{
bool found = false; // Flag, initialized to false.

int first = 0; // To hold the first array element
int middle; // To hold the mid point of search
int last = size - 1; // To hold the last array element
int position = -1; // To hold the position of search value
// CODE THE WHILE CONDITION AND THE LOGIC INSIDE THE WHILE CONSTRUCT...

while ( )
{






  
  



  

  



  


}

return position;
}
return position;
}

Explanation / Answer


int binarySearch(string values[], int size, string value)
{
bool found = false; // Flag, initialized to false.

int first = 0; // To hold the first array element
int middle; // To hold the mid point of search
int last = size - 1; // To hold the last array element
int position = -1; // To hold the position of search value
// CODE THE WHILE CONDITION AND THE LOGIC INSIDE THE WHILE CONSTRUCT...
while(first <= last)
{
   middle = (first + last)/2;

   if(values[middle] == value){
       position = middle;
       break;
   }
   else if(values[middle] > value)
       last = middle-1;
   else
       start = middle + 1;
   
}
  
return position;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote