Asp中的几种常见算法

<%
Dim B()
count1
=10
ReDim B(count1)
For count=0 To count1
    
Randomize   '初始化随机数生成器。
    B(count)= Int((1000 * Rnd+ 1)   ' 产生 1 到 6 之间的随机数。
   
 
Next

'输出数组中的元素
Function ShowArray(Arr)
For Each i In Arr
response.write i
&"<br>"
Next 
End Function
'=========================================
'
冒泡排序
'
Arr------等待排序的数组
'
=========================================
Function BubbleSort(Arr)

    
Dim PreVlue,NextValue,TempValue,SortFlag,Pass
    
Dim StartTime,EndTime
    Pass
=1
    SortFlag
=False

    
Do While Not SortFlag

    SortFlag
=true
        
For i=0 To UBound(Arr)-Pass

            
If  Arr(i)<Arr(i+1Then
                    
                TempValue
=Arr(i)
                Arr(i)
=Arr(i+1)
                Arr(i
+1)=TempValue
                SortFlag
=False
            
End If 
        
Next 
        Pass
=Pass+1
    
Loop
    EndTime
=timer

'BubbleSort=Arr
End Function

'=========================================
'
快速排序
'
=========================================
Function QuickSort(vData, Low, Hi)
    
' ---------------------------------------------------------
'
 Test to see if an array was passed
'
 ---------------------------------------------------------
If Not IsArray(vData) Then Exit Function

' ---------------------------------------------------------
'
 Define local variables
'
  ---------------------------------------------------------
  Dim lTmpLow 
  
Dim lTmpHi 
  
Dim lTmpMid 
  
Dim vTempVal 
  
Dim vTmpHold 
  
' ---------------------------------------------------------
'
 Initialize local variables
'
 ---------------------------------------------------------
  lTmpLow = Low
  lTmpHi 
= Hi

' ---------------------------------------------------------
'
 Leave if there is nothing to sort
'
 ---------------------------------------------------------
If Hi <= Low Then Exit Function

' ---------------------------------------------------------
'
 Find the middle to start comparing values
'
 ---------------------------------------------------------
  lTmpMid = (Low + Hi)  2
      
' ---------------------------------------------------------
'
 Move the item in the middle of the array to the
'
 temporary holding area as a point of reference while
'
 sorting.  This will change each time we make a recursive
'
 call to this routine.
'
 ---------------------------------------------------------
  vTempVal = vData(lTmpMid)
      
' ---------------------------------------------------------
'
 Loop until we eventually meet in the middle
'
 ---------------------------------------------------------
Do While (lTmpLow <= lTmpHi)

     
Do While (vData(lTmpLow) < vTempVal And lTmpLow < Hi)
          lTmpLow 
= lTmpLow + 1
     
Loop
     
Do While (vTempVal < vData(lTmpHi) And lTmpHi > Low)
           lTmpHi 
= lTmpHi - 1
     
Loop
            
     
' if the temp low end is less than or equal
     ' to the temp high end, then swap places
   If (lTmpLow <= lTmpHi) Then
         vTmpHold 
= vData(lTmpLow)      ' Move the Low value to Temp Hold
         vData(lTmpLow) = vData(lTmpHi)  ' Move the high value to the low
         vData(lTmpHi) = vTmpHold        ' move the Temp Hod to the High
         lTmpLow = lTmpLow + 1         ' Increment the temp low counter
         lTmpHi = lTmpHi - 1           ' Dcrement the temp high counter
     End If
  
Loop
' ---------------------------------------------------------
'
 If the minimum number of elements in the array is
'
 less than the temp high end, then make a recursive
'
 call to this routine.  I always sort the low end
'
 of the array first.
'
 ---------------------------------------------------------


  
If (Low < lTmpHi) Then
      QuickSort vData, Low, lTmpHi
  
End If
' ---------------------------------------------------------
'
 If the temp low end is less than the maximum number
'
 of elements in the array, then make a recursive call
'
 to this routine.  The high end is always sorted last.
'
 ---------------------------------------------------------

  
If (lTmpLow < Hi) Then
       QuickSort vData,lTmpLow, Hi
End If


End Function
'=========================================
'
直接插入式排序
'
Arr------等待排序的数组
'
=========================================
Function InsertSort(Arr)
    
Dim i,j,x,TempValue    
    
For i=1 To UBound(Arr)
     TempValue
=Arr(i)
     j
=i-1
     X
=Arr(i)
     
Do While X<Arr(j)
        Arr(j
+1)=Arr(j)
        j
=j-1
        
If j<0 Then Exit Do  
     
Loop 
     Arr(j
+1)=TempValue
    
Next    
End Function

'=========================================
'
简单选择排序
'
Arr------等待排序的数组
'
=========================================
Function SelectSort(Arr)
    
Dim i,j,K,TempValue    
    
For i=0 To UBound(Arr)-1
    K
=i
        
For j=i+1 To UBound(Arr)
            
If Arr(j)<Arr(K) Then 
                K
=j
            
End If 
        
Next 
    
If K<>Then 
        TempValue
=Arr(K)
        Arr(k)
=Arr(i)
        Arr(i)
=TempValue
    
End If 
    
Next    
End Function
  
slow
=lbound(B)
shigh
=ubound(B)

  
'开始快速排序
  StartTime1=timer
'c=quicksort(B,slow,shigh)
c=BubbleSort (B)
'c=InsertSort (B)
'
c=SelectSort(B)
     EndTime1=timer
    Response.write Endtime1
&"<br>"&starttime1&"<br>排序耗时:<font color=red>"&FormatNumber((endtime1-starttime1)*1000,3)&"</font>毫秒!<br>" 



ShowArray(B)


%
>
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值