• 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
  • 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
  • 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: 13081
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 ]
 
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
reply
    Bookmark Topic Watch Topic
  • New Topic