Graal Forums  

Go Back   Graal Forums > Development Forums > NPC Scripting > Code Gallery
FAQ Members List Calendar Today's Posts

Reply
 
Thread Tools Search this Thread Display Modes
  #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
  #2  
Old 10-16-2009, 10:29 PM
DustyPorViva DustyPorViva is offline
Will work for food. Maybe
DustyPorViva's Avatar
Join Date: Sep 2003
Location: Maryland, USA
Posts: 9,589
DustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond repute
Send a message via AIM to DustyPorViva Send a message via MSN to DustyPorViva
if (2 in array) true;
else false;

?
Reply With Quote
  #3  
Old 10-16-2009, 10:32 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
Quote:
Originally Posted by DustyPorViva View Post
if (2 in array) true;
else false;

?
binary_search() is pretty famous.

It's searches a numeric sorted array in an efficient way (usually only used on big arrays).

200'000 Elements -> only searches 100'000.

etc.
Reply With Quote
  #4  
Old 10-17-2009, 10:21 AM
coreys coreys is offline
N-Pulse Assistant Manager
coreys's Avatar
Join Date: Mar 2005
Posts: 2,180
coreys has a spectacular aura about
Send a message via AIM to coreys Send a message via MSN to coreys Send a message via Yahoo to coreys
Always nice to have around, although I can't think of...well, any use for it, in Graal.
But if the need comes up, then you've got use covered, my boy.
Reply With Quote
  #5  
Old 10-17-2009, 10:27 AM
Twinny Twinny is offline
My empire of dirt
Twinny's Avatar
Join Date: Mar 2006
Location: Australia
Posts: 2,422
Twinny is just really niceTwinny is just really nice
Send a message via AIM to Twinny
Admittedly I've never have ever thought of a need for this (my arrays are never ordered) but this is one the first algorithms I was taught =)

Binary Search will save you if you have a gigantic sorted array =D
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 05:15 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.