• Post Reply Bookmark Topic Watch Topic
  • New Topic
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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

binarysearch() method doubt

 
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic