我试图将以下java二进制搜索例程转换为as3.我假设'compareTo'是一个内置的java方法,而'>>>是一种按位运算.
任何熟悉actionscript 3和Java的人都能帮忙解决这个问题吗?
package binary; public class Finder { public static int find( String[ ] keys, String target) { int high = keys.length; int low = -1; while (high - low>1) { int probe = (low + high)>>> 1; if (keys[probe].compareTo(target) > 0) high = probe; else low = probe; } if (low==-1 || keys[low].compareTo(target) !=0) return -1; else return low; } }
Chris W. Rea.. 6
这是一个功能性的AS3版本:
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; }
顺便说一句,我建议你重命名函数更有意义,比如binarySearch()
,这表明调用者最好对数组进行排序.一个名字find()
并不意味着这样.
这是一个功能性的AS3版本:
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; }
顺便说一句,我建议你重命名函数更有意义,比如binarySearch()
,这表明调用者最好对数组进行排序.一个名字find()
并不意味着这样.