·您当前的位置:首页 > 技术教程 > AS2与AS3技术 >

[AS3]AS3的一种排序算法源代码

时间:2015-09-08 11:26酷播
[AS3]AS3的一种排序算法源代码

[AS3]AS3的一种排序算法源代码

  1. package   
  2. {   
  3.        
  4.     public class BubbleSort   
  5.     {   
  6.            
  7.         public function BubbleSort()   
  8.         {   
  9.             var arr:Array = [2, 7, 9, 1, 5, 0, 9, 4];   
  10.             trace(sort2(arr, true));   
  11.             trace(sort2(arr, false));   
  12.         }   
  13.            
  14.         /**  
  15.          * AS3冒泡排序算法  
  16.          * @param arr 需要排序的数组  
  17.          * @param isAsc 是否升序  
  18.          * @return 返回排序后的数组  
  19.          */   
  20.         public function sort(arr:Array, isAsc:Boolean = true):Array   
  21.         {   
  22.             if(arr == null)   
  23.             {   
  24.                 throw new Error("Array is null.");   
  25.             }   
  26.             var tmp:*;   
  27.             if(isAsc)   
  28.             {   
  29.                 for(var i:int = 0; i < arr.length; i++)   
  30.                 {   
  31.                     for(var j:int = arr.length - 1; j > i; j--)   
  32.                     {   
  33.                         if(arr[j] < arr[j - 1])   
  34.                         {   
  35.                             tmp = arr[j];   
  36.                             arr[j] = arr[j - 1];   
  37.                             arr[j - 1] = tmp;   
  38.                         }   
  39.                     }   
  40.                 }   
  41.             }   
  42.             else   
  43.             {   
  44.                 for(i = 0; i < arr.length; i++)   
  45.                 {   
  46.                     for(j = arr.length - 1; j > i; j--)   
  47.                     {   
  48.                         if(arr[j] > arr[j - 1])   
  49.                         {   
  50.                             tmp = arr[j];   
  51.                             arr[j] = arr[j - 1];   
  52.                             arr[j - 1] = tmp;   
  53.                         }   
  54.                     }   
  55.                 }   
  56.             }   
  57.             return arr;   
  58.         }   
  59.            
  60.         // 另一种写法   
  61.         public function sort2(arr:Array, isAsc:Boolean = true):Array   
  62.         {   
  63.             if(arr == null)   
  64.             {   
  65.                 throw new Error("Array is null.");   
  66.             }   
  67.             var tmp:*;   
  68.             if(isAsc)   
  69.             {   
  70.                 for(var i:int = 0; i < arr.length; i++)   
  71.                 {   
  72.                     for(var j:int = i; j < arr.length; j++)   
  73.                     {   
  74.                         if(arr[i] > arr[j])   
  75.                         {   
  76.                             tmp = arr[i];   
  77.                             arr[i] = arr[j];   
  78.                             arr[j] = tmp;   
  79.                         }   
  80.                     }   
  81.                 }   
  82.             }   
  83.             else   
  84.             {   
  85.                 for(i = 0; i < arr.length; i++)   
  86.                 {   
  87.                     for(j = i; j < arr.length; j++)   
  88.                     {   
  89.                         if(arr[i] < arr[j])   
  90.                         {   
  91.                             tmp = arr[i];   
  92.                             arr[i] = arr[j];   
  93.                             arr[j] = tmp;   
  94.                         }   
  95.                     }   
  96.                 }   
  97.             }   
  98.             return arr;   
  99.         }   
  100.        
  101.     }   
  102. }   

 

热门文章推荐

请稍候...

保利威视云平台-轻松实现点播直播视频应用

酷播云数据统计分析跨平台播放器