programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Liutauras Vilda
• Tim Cooke
• Jeanne Boyarsky
• Paul Clapham
Sheriffs:
• Devaka Cooray
• Ron McLeod
• paul wheaton
Saloon Keepers:
• Tim Moores
• Piet Souris
• Tim Holloway
• Stephan van Hulst
• Carey Brown
Bartenders:
• Al Hobbs
• Frits Walraven
• Scott Selikoff

# binarysearch() method doubt

Ranch Hand
Posts: 243
• Number of slices to send:
Optional 'thank-you' note:
Hi,
I got confused in binary search index how it is calculating and returning index.
Can any one explain me with examples.

Ranch Hand
Posts: 103
• Number of slices to send:
Optional 'thank-you' note:
To tell in short binary search is done by sorting the given set of objects/things and then dividing the sorted set of objects/ things into two and then comparing the value with the middle one. If the middle one is bigger then you know that searching has to be done in first half otherwise the second half. Like this you go on dividing the sets and so decreasing the group or set of objects. At the end you will get the required thing.

lowercase baba
Posts: 13081
67
• Number of slices to send:
Optional 'thank-you' note:
think of it like the guessing game. I've picked a number between 1 and 100. try and guess it in as few as possible, and i'll tell you higher or lower each time.

well, your going to start in the middle. (1+100)/2 = 50 (more or less - we'll always round down).

i tell you 'higher'.

You now know the number is between 50 and 100, so you guess in the middle of that... (50+100)/2 = 75.

i tell you lower. so now you guess (50+75)/2 = 62ish.

62 is too low.

so, (62+75)/2 = 68. too high.

(62+68)/2 = 65. too high.

(62+65)/2 = 63ish.

63 is correct!!!
[ September 13, 2007: Message edited by: Fred Rosenberger ]

 Mo-om! You're embarassing me! Can you just read a tiny ad like a normal person? the value of filler advertising in 2021 https://coderanch.com/t/730886/filler-advertising