4
Contributors
13
Replies
14
Views
6 Years
Discussion Span
Last Post by herstein
0

The 4.5 at the end of the array means it's not sorted that can't help...

aah sorry, i forgot to mention that I have two boundaries and right now its set between 0 and 9. that is only data [0-9] are sorted and i only want to use binary search in those locations

0

OK.
Put a load of print statements into your code, printing the values of the key variables at each stage so you cen see where it's going wrong, eg at line 11 print left and right.

1

Suppose you are looking at a range of values between the indexes "left" and "right." You look at a value in the middle of the range and decide that you want to move left -- the to the left of the middle are closer to the target value that you're searching for. To do this, should you change the value in the "left" variable or the value in the "right" variable?

(Sometimes the most immediately intuitive answer is not the correct answer. ;-)

Votes + Comments
Very helpful and helps the OP think in the right direction without just giving the answer
1

Print statements can help. Using a debugger can help. With a debugger, you can step through the code as it executes and see where it's behavior diverges from what you expect it to do.

0

Print statements can help. Using a debugger can help. With a debugger, you can step through the code as it executes and see where it's behavior diverges from what you expect it to do.

Yes, I agree. But when working with beginners I tend to advise print because they are already familiar with it, plus mostly they are not using an IDE yet. Debuggers are better, but there are different debuggers in every IDE, and they all have significant learning curves.

0

alright thanks for the tip! now it appears that this method is not using the sorted array, but the unsorted one. why is that? once i've changed the memory locations shouldn't they hold throughout the programme? in my main method i execute the programme in such an order that the array is sorted before i run the binary search

0

Yes, the method will operate on whatever data array you have passed it.

ok but how do i make it run the new (sorted) array?

0

haha sorry i made a very silly mistake in my main method, thanks for all the help guys :)

0

With the changes to the way 'left' and 'right' are updated, I was quite surprised to find that it worked quite well for me. I threw a large number of test cases at it, and I found...

  1. It's not very consistent about handling values that are exactly at the mid-point between two given values in the sorted array.
  2. For some of those values, it goes into an infinate loop.

Have fun!

(And if you have any further questions, please feel free to ask. ;-)

0

With the changes to the way 'left' and 'right' are updated, I was quite surprised to find that it worked quite well for me. I threw a large number of test cases at it, and I found...

  1. It's not very consistent about handling values that are exactly at the mid-point between two given values in the sorted array.
  2. For some of those values, it goes into an infinate loop.

Have fun!

(And if you have any further questions, please feel free to ask. ;-)

oh yea that code is quite old now, i have changed it and i made a intentional mistakes there which i had changed to desperately see if i my logic was wrong :P

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.