as3 binary search

by cyberpunk on May 5th, 2010
No notes
Syntax: ActionScript 3
Show lines - Hide lines - Show in textbox - Download
public function binarySearch(keys:Array, target:int):int
{
	var high:int = keys.length;
	var low:int = -1;
 
	while (high - low > 1)
	{
		totalHops++;
 
		var probe:int = (low + high) >>> 1; // Bit operations helps to speed up the process
		if (keys[probe] > target)
		{
			high = probe;
		}
		else
		{
			low = probe;
		}
	}
 
	return (low == -1 || keys[low] !== target) ? -1 : low;
}

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS