Quantcast
Channel: User Chris W. Rea - Stack Overflow
Viewing all articles
Browse latest Browse all 42

Answer by Chris W. Rea for Binary search, from java to Actionscript

$
0
0

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.


Viewing all articles
Browse latest Browse all 42

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>