low = 3;
high = 5;
int mid = 3/2 + 5/2 is actually 1 + 2 = 3 because division returns integers. And that gives the wrong result.
int mid =(low + high) / 2 = (3+5) / 2 = 8 / 2 = 4. This is the correct mid.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
why dont we just have int mid = low/2 + high/2;
Because that's wrong :D
low = 3;
high = 5;
int mid = 3/2 + 5/2 is actually 1 + 2 = 3 because division returns integers. And that gives the wrong result.
int mid =(low + high) / 2 = (3+5) / 2 = 8 / 2 = 4. This is the correct mid.