Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binary Search Problems? [duplicate]

Possible Duplicate:
What are the pitfalls in implementing binary search?

I was perusing the wikipedia page for Binary Search and stumbled on a quote by Knuth below:

"Although the basic idea of binary search is comparatively straightforward, the details can be surprisingly tricky"

I recall implementing several Binary Searches as part of my Computer Science curriculum, but don't remember it being terribly tricky. However, this article states that 90% of surveyed professionals can't get one working after several hours. I'd like to assume this not because these are terrible programmers, but that there are fringe cases that naive implementations don't account for.

What are the details that Knuth is referring too? What are the common gotchas to be aware of if implementing a Binary Search algorithm?

Note I read that article by Bloch about the Programming Pearls bug(overflow of an int for midpoint). Is there anything else?

like image 871
nsfyn55 Avatar asked Jun 16 '11 12:06

nsfyn55


People also ask

Does binary search work if there are duplicates?

A binary search only gives you the position of the value you want, or the position of 1 of them if duplicated. To display all duplicates and indexes, you need to do a secondary search around the position returned by binary search routine.


1 Answers

Being in Java world in my day job, I remember this. I was pretty surprised when I first read it so this may be one of the things Donald was talking about.

like image 120
omermuhammed Avatar answered Sep 28 '22 17:09

omermuhammed