|
-
February 19th, 2009, 08:59 AM
#4
Re: Binary Search Problem
Well, apparently you didn't debug good enough. dlorde has given you the answer but you refuse to accept it. What happens is this:
mid = 0 + 40 / 2 => 20
20 <= 37 --> true
low = 20
mid = 20 + 40 / 2 --> 30
30 <= 37 --> true
low = 30
mid = 30 + 40 / 2 --> 35
35 <= 37 --> true
low = 35
mid = 35 + 40 / 2 --> 37 (rounded of couse because of int)
37 <= 37 --> true
low = 37
mid = 37 + 40 / 2 --> 38
38 <= 37 --> false
high = 38
mid = 37 + 38 / 2 --> 37
37 <= 37 --> true
low = 37 (which it already did...)
mid = 37 + 38 / 2 --> 37
And here is where you get your infinite recursion. Mid will always be 37, low will always be 37, high will always be 38. Therefore your loop will never end.
So, before you post ignorant comments to the guy who has help the most people on this Java board for the last 10+ years you should try to do as he suggests. So clearly it is YOU who did not read HIS post.
You NEVER check to see if the number is exactly what you are looking for so you can terminate the loop. Which is exactly what dlorde said. All you check is for low > high which never happens in this case.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|