バブルソート

by toilet7
http://www40.atwiki.jp/spellbound/pages/156.html
♥0 | Line 31 | Modified 2011-09-13 13:21:46 | MIT License
play

ActionScript3 source code

/**
 * Copyright toilet7 ( http://wonderfl.net/user/toilet7 )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/4XqV
 */

package {
    import flash.text.TextFormat;
    import flash.display.Sprite;
    import flash.text.TextField
    
    public class FlashTest extends Sprite {
        public function FlashTest() {
            // write as3 code here..
            
            //ソートする値
            var data:Array = [3, 1, 4, 2];
            
            for (var i:int = data.length - 1; i >= 1; i--)
            {
                for (var j:int = 0; j < i; j++)
                {
                    if (data[j] > data[j + 1])
                    {
                        //値の入れ替え                         
                         var temp:int = data[j];
                         data[j] = data[j + 1];
                         data[j + 1] = temp;   
                    }
                }
            }
            
            showResult(data);
        }
        
         private function showResult(...result:*):void
        {
             var tf:TextField = new TextField();
             tf.defaultTextFormat = new TextFormat("arial", 19);
             tf.text = result.toString();
             tf.autoSize = "left";
             addChild(tf);
        }      
    }
}