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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
VulpesRank #55 · 29.2K reputationPosted Mar 24, 2014, 7:05 AM EDT
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 SinghRank #415 · 6K reputationPosted Mar 24, 2014, 9:36 AM EDT