difference between
static int binarySearch(Object[], key)
and
static int binarySearch(primitive[], key)
Explain with code?
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
VulpesPosted Mar 24, 2014, 11:05 AM
In both cases, the array needs to be sorted first before applying the binarySearch method otherwise the results will be undefined. However, this may not be possible with an Object[] as it could contain both strings and numbers which are not comparable with one another. So, in that event, you'd get an undefined result.
Here's an example:
When I compiled and ran this, the output was:
The last line is, of course, wrong since 7 is at index 3 in the (unsorted) array.
Abhijeet SinghPosted Mar 24, 2014, 1:36 PM