Connecting Tech Pros Worldwide Help | Site Map

Binary Search Algorithm

  #1  
Old March 31st, 2009, 04:14 AM
Kelicula's Avatar
Expert
 
Join Date: Jul 2007
Posts: 169
Why?:
One commonly used algorithm is the binary search.
If you don't already know it, you should read on.
Very helpful. Saves much CPU. Reduces computations exponentially.

When searching though a lot of information to find a match, the first idea that comes to mind is the linear search. Loop through all the values looking for a match. If you find it, return the location, or value and end the search.
However this becomes highly inefficient when the values to be searched become very large. It's a O(N) algorithm. At worse case you may have to search ALL the values to find a match, or even worse find that it's not there!

How?:
If the list of items can be sorted numerically or ASCII. Use a binary search!

First, sort the list.Then compare the value in the very center (round up or down in case of uneven list elements) to your search term. If it is "less than" your term, you know that it must be lower than that point. So you can eliminate the upper range. If the term has a greater value than your center point value, eliminate the lower range.

Thus reducing the amount of items to be searched by half.

Apply this technique to the remaining items continually and reduce the search by half each iteration.

Thus running at the much faster O(log N) algorithm.

Example:

Warning: uses "recursion", remember to consider function call loads on CPU. In some languages this can actually be slower than linear. See non recursive below.
(taken from Wikipedia)

Expand|Select|Wrap|Line Numbers
  1. BinarySearch(A[0..N-1], value, low, high) {
  2.        if (high < low)
  3.            return -1 // not found
  4.        mid = low + ((high - low) / 2)  // Note: not (low + high) / 2 !!
  5.        if (A[mid] > value)
  6.            return BinarySearch(A, value, low, mid-1)
  7.        else if (A[mid] < value)
  8.            return BinarySearch(A, value, mid+1, high)
  9.        else
  10.            return mid // found
  11.    }
  12.  
There is an optimization here.
Sometimes when dealing with large numbers, or floating point high precision.
mid = (low + high)/2
can overflow and hog memory, resulting in unreliable results.
mid = low + ((high - low)/2)
fixes it. :)
See here...

Non Recursive:
Expand|Select|Wrap|Line Numbers
  1. low = 0
  2.        high = N
  3.        while (low < high) {
  4.            mid = low + ((high - low) / 2)  // Note: not (low + high) / 2 !!
  5.            if (A[mid] < value)
  6.                low = mid + 1; 
  7.            else
  8.                 //can't be high = mid-1: here A[mid] >= value,
  9.                 //so high can't be < mid if A[mid] == value
  10.                 high = mid; 
  11.        }
  12.        // high == low, using high or low depends on taste 
  13.        if ((low < N) && (A[low] == value))
  14.            return low // found
  15.        else
  16.            return -1 // not found      
  17.  

There are MANY sources for this most elementary algorithm. Just type "binary search" into Google.

Happy coding....



  #2  
Old April 9th, 2009, 08:33 PM
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,858

re: Binary Search Algorithm


I'd never thought of doing it like that.

Thanks a bunch :D
  #3  
Old June 16th, 2009, 03:14 PM
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,634

re: Binary Search Algorithm


Also see Fibonacci Search for a slightly better search than the binary search method.

kind regards,

Jos
  #4  
Old June 16th, 2009, 05:20 PM
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,858

re: Binary Search Algorithm


While we're talking about search algorithms, the boyer-moore algorithm is interesting.
  #5  
Old June 16th, 2009, 10:31 PM
Kelicula's Avatar
Expert
 
Join Date: Jul 2007
Posts: 169

re: Binary Search Algorithm


Nice, I had never heard of the Fibonacci search myself.

Very interesting...
  #6  
Old September 11th, 2009, 11:58 PM
Newbie
 
Join Date: Sep 2009
Location: portland OR
Posts: 7

re: Binary Search Algorithm


in researching the Fibonacci search code i also noticed the golden ratio search.
what do you think about this alternative?
Reply


Similar Threads
Thread Thread Starter Forum Replies Last Post
Binary Search Atos insights 0 June 15th, 2008 06:35 AM
Help understand probems - Binary Search and Sequenital Search Timmy answers 2 July 7th, 2007 06:45 PM
Binary Search Tree Defected answers 11 May 31st, 2007 05:55 PM
single link list , binary search free2cric@yahoo.com answers 10 March 3rd, 2006 04:25 AM
Binary Search Tree Ravi answers 5 July 22nd, 2005 10:47 AM