Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Binary Search  RSS feed

 
leon matthew
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it faster to do a binary search recursively than to just code a full search?
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by leon matthew:
Is it faster to do a binary search recursively than to just code a full search?
That depends on the number of elements you are searching through; once the number exceeds a certain threshold binary search will aways be faster. By the way, before you start hand-coding it look into TreeSet, TreeMap, Collections.binarySearch(), and Arrays.binarySearch(), all of which can be used to implement binary (n*log(n)) searches.
- Peter
 
Brian Daniel
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I typically do a binary search until I get to the last 5-10 elements, at which time I do an iterative search.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!