Here's a functional AS3 version:
public static function find(keys:Array, target:String):int { var high:int = keys.length; var low:int = -1; while (high - low > 1) { var probe:int = (low + high) / 2; if (keys[probe] > target) high = probe; else low = probe; } if (low == -1 || keys[low] !== target) return -1; else return low; }
BTW, I would recommend you rename the function to be more meaningful, like binarySearch()
, which indicates to the caller the array had better be sorted. A name like find()
does not imply such.