1

如果我在 AS3 中有这样的点数组:

(x=584.1, y=279.4),(x=584.1, y=280.4),(x=584.1, y=281.4),(x=584.1, y=282.4),(x=584.1, y=283.4),(x=584.1, y=284.4),(x=584.1, y=285.4),(x=584.1, y=286.4),(x=584.1, y=287.4),(x=585.1, y=287.45),(x=586.1, y=287.45),(x=584.1, y=288.4),(x=585.1, y=288.45),(x=586.1, y=288.45),(x=587.1, y=288.5),(x=588.1, y=288.55),(x=584.1, y=289.4),(x=585.1, y=289.45),(x=586.1, y=289.45),(x=587.1, y=289.5),(x=588.1, y=289.55),(x=584.1, y=290.4),(x=585.1, y=290.45),(x=586.1, y=290.45),(x=587.1, y=290.5),(x=588.1, y=290.55),(x=584.1, y=291.4),(x=585.1, y=291.45),(x=586.1, y=291.45),(x=587.1, y=291.5),(x=588.1, y=291.55),(x=584.1, y=292.4),(x=585.1, y=292.45),(x=586.1, y=292.45),(x=587.1, y=292.5),(x=588.1, y=292.55),(x=584.1, y=293.4),(x=585.1, y=293.45),(x=586.1, y=293.45),(x=587.1, y=293.5),(x=588.1, y=293.55),(x=584.1, y=294.4),(x=585.1, y=294.45),(x=586.1, y=294.45),(x=587.1, y=294.5),(x=588.1, y=294.55),(x=584.1, y=295.4),(x=585.1, y=295.45),(x=586.1, y=295.45),(x=587.1, y=295.5),(x=588.1, y=295.55)

找到最高和最低 y 值的有效方法是什么?

4

3 回答 3

1

如果您只对 n > 2 的任何随机输入数据的最高和最低感兴趣,我很确定这是最快的。

var input:Array = [ {x:584.1, y:279.4}, {x:584.1, y:280.4}, {x:584.1, y:281.4}, ...];

public function MinMaxValues() {
    var len:Number = input.length;
    var min:Number = Number.MAX_VALUE;
    var max:Number = Number.MIN_VALUE;

    var check:Number;
    for (var i:int = 0; i < len; i++) {
        check = input[i]["y"];

        if (check < min) {
            min = check;
        } 

        if (check > max) {
            max = check;
        }
    }
    trace("minimum value of [" + len + "] items is::" + min);
    trace("maximum value of [" + len + "] items is::" + max);

}

于 2012-12-18T08:32:10.210 回答
1

[编辑] 有时 Array.sortOn 是最快的。如果你的数组需要排序,就使用native函数,否则,使用大牛的代码。

myArray.sortOn('y', Array.NUMERIC);

我用排序和丹尼尔的代码进行了基准测试。在某些情况下,sortOn/copy 比无数组副本更快。在某些情况下不是。无论哪种方式,数组复制/排序都会给出不一致的结果,因为它会消耗更多的内存。结果和基准如下。

(在调试播放器中array.sortOn总是会输)

package {

    import flash.display.*;
    import flash.geom.*;
    import flash.text.*;
    import flash.utils.*;

    final public class ArraySortTest extends Sprite {

        /**
         *  @private
         */
        private const arr:Array         = [];

        private const text:TextField    = new TextField();

        public function ArraySortTest():void {

            text.width          = stage.stageWidth;
            text.height         = stage.stageHeight;
            addChild(text);

            var length:int      = 1000;
            var iterations:int  = 5000;
            while (length--) {
                arr.push(new Point(Math.random() * 500, Math.random() * 500));
            }

            var i:int, start:int, result:Point = new Point();

            start   = getTimer();
            i       = iterations;

            while (i--) {
                getMinMax(arr, 'y', result);
            }

            text.appendText('nosort: ' + String(getTimer() - start) + result + '\n');

            start   = getTimer();
            i       = iterations;
            while (i--) {
                getMinMaxSorted(arr, 'y', result);
            }
            text.appendText('sorted: ' + String(getTimer() - start) + result + '\n');


            start   = getTimer();
            i       = iterations;
            while (i--) {
                getMinMaxSortedConcat(arr, 'y', result);
            }
            text.appendText('sorted concat: ' + String(getTimer() - start) + result + '\n');

        }

        private function getMinMaxSortedConcat(input:Array, key:String, result:Point):void {

            input.concat().sortOn('y', Array.NUMERIC);

            result.x = input[0][key];
            result.y = input[int(input.length - 1)][key];
        }

        private function getMinMaxSorted(input:Array, key:String, result:Point):void {

            input.sortOn('y', Array.NUMERIC);

            result.x = input[0][key];
            result.y = input[int(input.length - 1)][key];
        }

        private function getMinMax(input:Array, key:String, result:Point):void {

            var len:Number = input.length;
            var min:Number = Number.MAX_VALUE;
            var max:Number = Number.MIN_VALUE;

            var check:Number;
            for (var i:int = 0; i < len; i++) {
                check = input[i][key];

                if (check < min) {
                    min = check;
                } else if (check > max) {
                    max = check;
                }
            }

            result.x = min;
            result.y = max;
        }
    }
}

结果:

100 elements, 5000 iterations each
nosort: 124(x=3.739513223990798, y=495.2090959995985)
sorted: 109(x=3.739513223990798, y=495.2090959995985)
sorted concat: 115(x=3.739513223990798, y=495.2090959995985)

1000 elements, 5000 iterations each
nosort: 1263(x=0.13151345774531364, y=499.65104297734797)
sorted: 1181(x=0.13151345774531364, y=499.65104297734797)
sorted concat: 1234(x=0.13151345774531364, y=499.65104297734797)

1000 elements, 10000 iterations
nosort: 2474(x=0.18377462401986122, y=499.73958847112954)
sorted: 2371(x=0.18377462401986122, y=499.73958847112954)
sorted concat: 2454(x=0.18377462401986122, y=499.73958847112954)

10000 elements, 1000 iterations
nosort: 2487(x=0.003137858584523201, y=499.816557392478)
sorted: 2961(x=0.003137858584523201, y=499.816557392478)
sorted concat: 3157(x=0.003137858584523201, y=499.816557392478)
于 2012-12-18T06:43:37.293 回答
0

Just that, traverse the array, grab first point's Y value as current min and max, and compare all the others. I think there will be no better way, unless you will provide additional info on how is the data organized in that array.

于 2012-12-18T06:34:59.867 回答