The following is what i have so far. The problem is how do i find the insertion spot. I did some hand tracing on paper and what i observed was eventually the lowerbound and upperbound equal and the insertion spot is always a index higher than the index of lower and upper bounds when they are equal.
I know there are many solutions online but i am really trying to understand this on my own since i only learn and remember things when i learn on my own rather than trying to figure out how others came up with a solution.
If someone can guide me it would be great. Also once i get the insertion spot i can do the rest which is moving all the values a index lower to make spot for the insertion value.
public void insert(long value) {
int lowerBound = 0;
int upperBound = nElems - 1;
int curIn;
while (true) {
curIn = (lowerBound + upperBound) / 2;
if (a[curIn] < value)
lowerBound = curIn + 1;
else
upperBound = curIn - 1;
}
while (true)with nobreak?eventually the lowerbound and upperbound equalWhy not add a check to see whether the two are equal, then, before the other checks in the loop? If you know your base case, check for it. :)