• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to fix binary search code for an array?  RSS feed

 
Shey Gordon
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Edited to update work so far. Now I'm just getting two errors concerning line 38 where it has Arrays.sort(int roomList); and the errors state that ".class is expected" and so is a semicolon. What did I do wrong? Also, how might I tweak the code to display "Occupied" or "Unoccupied" depending on the room that was entered?

Also we're not allowed to make use of API method for binary search so that's out of the question.

 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you call a method, you simply pass in the parameter. You don't need the type.

 
Shey Gordon
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure why I made that mistake. I fixed a few more things, but it's still generating errors. One is for a bad operand type and the other two say "cannot return a value from method whose result type is void."

 
Russell Clapp
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the error you are having is on line 46. return mid; will not work. If this is the value, you should print occupied, then simply put return; to end the loop. Also when sorting your array try to account for the amount of 0's you will have. Your array set is 25. If the user puts less than 25 numbers then there will be zeros indexed and when put in order will be something like 0, 0, 0, 0, 0, 0, 0, ,0 ,0 ,0, 0, 0, 0, 0, 0, 1, 5, 9, 12, 36, 42, 50, etc... good luck.
 
Shey Gordon
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I made a few adjustments based on your advice. Now the error I'm receiving is related to line 43, or midValue = roomList[mid]; as incompatible types.

 
Russell Clapp
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's saying that because midValue is an int, and roomList is a String.

I am not an expert so I can't tell you how to fix this. But i can say you can write this program using all ints.
 
Russell Clapp
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also how you had the code at the bottom was right. it is not one or the other, occupied or unoccupied. if the numbers are not the same then the program has a little more figuring out to do. The way a binary search works is to divide in half, then either adjust the top or bottom half and try again until the number it is searching for is the mid or there are no more tries. so if looking for 7 out of 1 2 3 4 5 6 7 8 9 10, it will check the 5 first. This is because 0 (low) plus 10 (high) divided by 2 is 5 realizing that 7 is higher than 5, it will add 1 to 0, (1) and then add this to 10 (11). 11/2 is 5.5. 7 is still higher so low + 1 is 2. 2+10 = 12, 12/2 = 6. See? So just because the room is not occupied on the first try does not mean it is automatically unoccupied.
 
Shey Gordon
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I fixed it again. It compiles properly and it works up until the binary search part. Let's say I enter room 4 and then search for room 4... instead of showing "Occupied" it prints "Unoccupied" infinitely. In order to adjust it to where it searches correctly, exactly how would I write the code?

 
Sam Kaydee
Greenhorn
Posts: 12
Android Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please look into your binary search algorithm. What you have written now is


Now this will be AN INFINITE LOOP. You have asked the while loop to run if low<=high whereas you haven't modified any of their values inside the loop.So the condition will always remain true and the loop will keep on running.
 
Sam Kaydee
Greenhorn
Posts: 12
Android Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Binary Search, you have a sorted list.{suppose Ascending order}
1.Check the middle element and compare with your value.
2. If your value==mid then it is found.
3. If your value > middle element, you have to search the upper part of the list i.e now your lower bound will be mid and the upper bound will be array size.
4. Else your value is in the lower part of the list i.e your upper bound will change to mid.
5. Continue this until the element is found.


Now implement this in your case and you are done!!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!