bool binary_search (int target, int array[])
{
int end = length-1;
int start = 0;
// 7 0 14
// 11 8 14
// 9 10 + 8 =
// 8 + 8
int middle = (start+ end)/2;
do
{ //also changed end by your request
int end = length-1;
int start = length;
// 7 0 14
// 11 8 14
// 9 10 + 8 =
// 8 + 8
int middle = (start+ end)/2;
//
else if array[middle] == array[target])
{
return true;
} //11 7
//
else if ( array[target] > array[middle] )
{ //8
int start = middle+1;
// 8 13
if (array[middle] == array[target])
{
return true;
}
}
else if(array[target] < array[middle])
{
//10
}
}
while (end - start > 0 )
return false;
}
// This was the code I was going to post.My ide was messy and got the wrong code. The while loop was wrong though but I corrected it from your suggestion.
bool binary_search (int target, int array[],int length) {
do
{ int end = length-1;
int start = 0;
// 7 0 14
// 11 8 14
// 9 10 + 8 =
// 8 + 8
int middle = (start+ end)/2;
if length[middle] == length[target])
{
return true;
} //11 7
//
if ( length[target] > length[middle] )
{ //8
int start = middle+1;
// 8 13
if (length[middle] == length[target])
{
return true;
}
}
else if(length[target] < length[middle])
{
//10
end = middle - 1;
{
return false;
}
}
}
while ( array[end_num]- array[start_num] > 0)//notice error here because pointed it out.
return false;
}