You do not need the loop on i
.
Your loop would be on begin
and end
, such as while( begin < end )
Depending on how values[middle]
compare with values[begin]
and values[end]
, you have to adjust begin
(begin = middle + 1;
) or end
(end = middle - 1;
). Beware of the boundary cases!