• as3冒泡排序


     1 <?xml version="1.0" encoding="utf-8"?>
     2 <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
     3                xmlns:s="library://ns.adobe.com/flex/spark" 
     4                xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" creationComplete="init()">
     5     <fx:Declarations>
     6         <!-- Place non-visual elements (e.g., services, value objects) here -->
     7     </fx:Declarations>
     8     
     9     <fx:Script>
    10         <![CDATA[
    11             //冒泡排序 [5,7,9,8,1,3,6,2,4,5,0];
    12             [Bindable]
    13             private var arrNumber:Array=[5,7,9,8,1,3,6,2,4,5,0];
    14             [Bindable]
    15             private var arrNumberSort:Array;            
    16             
    17             private function bsort(arr:Array,sortType:String):Array
    18             {
    19                 var temp:String;
    20                 if(sortType.toLocaleLowerCase()=="descending")//降序
    21                 {
    22                     for(var i:uint=0;i<arr.length;i++)
    23                     {
    24                         for(var j:uint=arr.length-1;j>i;j--)
    25                         {
    26                             if(arr[j-1]<arr[j])
    27                             {
    28                                 temp=arr[j-1];
    29                                 arr[j-1]=arr[j];
    30                                 arr[j]=temp;
    31                             }
    32                         }
    33                     }
    34                 }
    35                 else if(sortType.toLocaleLowerCase()=="ascending")//升序
    36                 {
    37                     for(var m:uint=0;m<arr.length;m++)
    38                     {
    39                         for(var n:uint=arr.length-1;n>m;n--)
    40                         {
    41                             if(arr[n-1]>arr[n])
    42                             {
    43                                 temp=arr[n-1];
    44                                 arr[n-1]=arr[n];
    45                                 arr[n]=temp;
    46                             }
    47                         }
    48                     }
    49                 }
    50                 else
    51                 {
    52                     throw new Error("You have a typo when Calling the bsort() function, please use 'ascending' or 'descending' for sortType!");
    53                 }
    54                 return arr;
    55             }
    56             
    57             
    58             private function init():void
    59             {
    60                 arrNumberSort = bsort(arrNumber,"descending");
    61             }
    62         ]]>
    63     </fx:Script>
    64     <s:VGroup>
    65         <s:HGroup>
    66             <s:Label text="input array:"/><s:TextInput text="{arrNumber}"/>
    67         </s:HGroup>
    68         <s:HGroup>
    69             <s:Label text="input array sort:"/><s:TextInput text="{arrNumberSort}"/>
    70         </s:HGroup>
    71     </s:VGroup>    
    72 </s:Application>
  • 相关阅读:
    iOS-Core-Animation-Advanced-Techniques(一)
    vue 路由
    Vue 生产环境部署
    vue 单文件组件
    vue 插件
    Vue 混合
    vue 自定义指令
    vue render函数 函数组件化
    vue render函数
    vue 过渡状态
  • 原文地址:https://www.cnblogs.com/sange/p/2363798.html
Copyright © 2020-2023  润新知