1 /** 2 * 冒泡排序 3 */ 4 private function bubbleSort():void{ 5 6 var arr:Array = [4, 5, 3, 13, 25, 2]; 7 var length:uint = arr.length; 8 for(var i:int = 0; i < length-1; i++){ 9 for(var j:int = 0; j < length-i-1; j++){ 10 if(arr[j] > arr[j+1]){ //升序 降序为: arr[j] < arr[j+1] 11 var temp:int = arr[j]; 12 arr[j] = arr[j+1]; 13 arr[j+1] = temp; 14 } 15 } 16 } 17 18 } 19 20 /** 21 * 选择排序 22 */ 23 private function selectSort():void{ 24 25 var arr:Array = [4, 5, 3, 13, 25, 2]; 26 var length:uint = arr.length; 27 for(var i:int = 0; i < length-1; i++){ 28 var mIndex:int = i; //当前剩余的数中最大或最小数的索引 29 for(var j:int = i+1; j < length; j++){ 30 if(arr[mIndex] > arr[j]){ //升序 降序为: arr[mIndex] < arr[j] 31 mIndex = j; 32 } 33 } 34 if(mIndex != i){ 35 var temp:int = arr[i]; 36 arr[i] = arr[mIndex]; 37 arr[mIndex] = temp; 38 } 39 } 40 41 }