Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   Code Gallery (https://forums.graalonline.com/forums/forumdisplay.php?f=179)
-   -   binary_search (https://forums.graalonline.com/forums/showthread.php?t=134256515)

Pelikano 10-16-2009 10:26 PM

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;



DustyPorViva 10-16-2009 10:29 PM

if (2 in array) true;
else false;

?

Pelikano 10-16-2009 10:32 PM

Quote:

Originally Posted by DustyPorViva (Post 1531038)
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.

coreys 10-17-2009 10:21 AM

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. :D

Twinny 10-17-2009 10:27 AM

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


All times are GMT +2. The time now is 06:57 PM.

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