as3 数组冒泡排序,针对数组里边的元素是纯数字。用双重for循环,将数组里边元素值小的和元素值大的进行兑换位置。
怎么做,贴出源码:
View Code
package { import flash.display.Sprite; public class Main extends Sprite { private var bubbleArr:Array = [95,25,36,12,88,69,365,250,280]; private var referArr:Array=[]; public function Main():void { referArr=bubbleArr.concat(); bubleSort(bubbleArr); trace(referArr); trace(referArr.sort()); } private function bubleSort(arr:Array) { var len:int = arr.length; for (var i:int=0; i<len; i++) { for (var j:int=0; j<len; j++) { if (arr[j] > arr[(j + 1)]) { var temp:int = arr[j]; arr[j] = arr[(j + 1)]; arr[(j + 1)] = temp; } } echo((i+1),arr); } } private function echo(num:int,arr:Array) { trace("正在进行第"+num+"次排序"+"\n"+"此刻数组是:"+arr+"\n"); } } }