Forums Register Login

How to fix binary search code for an array?

+Pie Number of slices to send: Send
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.

+Pie Number of slices to send: Send
When you call a method, you simply pass in the parameter. You don't need the type.

+Pie Number of slices to send: Send
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."

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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?

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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!!
I want my playground back. Here, I'll give you this tiny ad for it:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1049 times.
Similar Threads
problem with variable
could someone can take a look at my code and leave feedback?
suming up the total help
counter help(summing up the totals)
Issue with my loop or something else?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 01:55:26.