常用的几种asp排序方法介绍

80酷酷网    80kuku.com

  排序 

<%

DimaData
aData=Array(3,2,4,1,6,0)

CallResponseArray(aData,"原来顺序")
CallResponseArray(SelectSort(aData),"选择排序")
CallResponseArray(QuickSort(aData),"快速排序")
CallResponseArray(InsertSort(aData),"插入排序")
CallResponseArray(BubbleSort(aData),"冒泡排序")


'选择排序
FunctionSelectSort(a_Data)
   Dimi,j,k
   Dimbound,t
   bound=UBound(a_Data)

   Fori=0Tobound-1
       k=i
       Forj=i+1Tobound
           Ifa_Data(k)>a_Data(j)Then
               k=j
           EndIf
       Next
       t=a_Data(i)
       a_Data(i)=a_Data(k)
       a_Data(k)=t
   Next

   SelectSort=a_Data
EndFunction


'快速排序
FunctionQuickSort(a_Data)
   Dimi,j
   Dimbound,t
   bound=UBound(a_Data)

   Fori=0Tobound-1
       Forj=i+1Tobound
           Ifa_Data(i)>a_Data(j)Then
               t=a_Data(i)
               a_Data(i)=a_Data(j)
               a_Data(j)=t
           EndIf
       Next
   Next

   QuickSort=a_Data
EndFunction


'冒泡排序
FunctionBubbleSort(a_Data)
   Dimbound
   bound=UBound(a_Data)
   DimbSorted,i,t
   bSorted=False
   
   DoWhilebound>0AndbSorted=False
       
       bSorted=True
       Fori=0Tobound-1
           Ifa_Data(i)>a_Data(i+1)Then
               t=a_Data(i)
               a_Data(i)=a_Data(i+1)
               a_Data(i+1)=t
               bSorted=False
           EndIf
       Next
       bound=bound-1
   Loop
   
   BubbleSort=a_Data
EndFunction


'插入排序
FunctionInsertSort(a_Data)
   Dimbound
   bound=UBound(a_Data)
   Dimi,j,t

   Fori=1Tobound
       t=a_Data(i)
       j=i
       DoWhilet<a_Data(j-1)Andj>0
           a_Data(j)=a_Data(j-1)
           j=j-1
       Loop
       a_Data(j)=t
   Next
       
   InsertSort=a_Data
EndFunction

'输出数组
SubResponseArray(a_Data,str)
   Dims
   s=""
   Response.Write"<b>"&str&":</b>"
   Fori=0ToUBound(a_Data)
       s=s&a_Data(i)&","
   Next
   s=Left(s,Len(s)-1)
   Response.Writes
   Response.Write"<hr>"
EndSub

%>

分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: