Thread: binary_search
View Single Post
  #1  
Old 10-16-2009, 10:26 PM
Pelikano Pelikano is offline
Registered User
Pelikano's Avatar
Join Date: Oct 2008
Posts: 1,133
Pelikano has a little shameless behaviour in the past
binary_search

Should be self explaining...

PHP Code:
temp.array = {12345678910};
binary_search(array, 2); // returns true
binary_search(array, 11); // returns false

function binary_search(array, value) {
  
temp.volume = array.size();
  
temp.middleElement int(volume/2);
  
temp.start = (array[middleElement] < value?middleElement:0);
  
temp.end = (start==0?middleElement+1:volume);
  for (
startendi++) {
    if (array[
i] == value) {
      return 
true;
    }
  }
  return 
false;

Reply With Quote