<% Dim B() count1=10 ReDim B(count1) For count=0To count1 Randomize'初始化随机数生成器。 B(count)=Int((1000*Rnd) +1) ' 产生 1 到 6 之间的随机数。 Next '输出数组中的元素 Function ShowArray(Arr) ForEach 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 DoWhileNot SortFlag SortFlag=true For i=0ToUBound(Arr)-Pass If Arr(i)<Arr(i+1) Then TempValue=Arr(i) Arr(i)=Arr(i+1) Arr(i+1)=TempValue SortFlag=False EndIf Next Pass=Pass+1 Loop EndTime=timer 'BubbleSort=Arr End Function '========================================= '快速排序 '========================================= Function QuickSort(vData, Low, Hi) ' --------------------------------------------------------- ' Test to see if an array was passed ' --------------------------------------------------------- IfNotIsArray(vData) ThenExitFunction ' --------------------------------------------------------- ' 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 ThenExitFunction ' --------------------------------------------------------- ' 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 ' --------------------------------------------------------- DoWhile (lTmpLow <= lTmpHi) DoWhile (vData(lTmpLow) < vTempVal And lTmpLow < Hi) lTmpLow = lTmpLow +1 Loop DoWhile (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 EndIf 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 EndIf ' --------------------------------------------------------- ' 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 EndIf End Function '========================================= '直接插入式排序 'Arr------等待排序的数组 '========================================= Function InsertSort(Arr) Dim i,j,x,TempValue For i=1ToUBound(Arr) TempValue=Arr(i) j=i-1 X=Arr(i) DoWhile X<Arr(j) Arr(j+1)=Arr(j) j=j-1 If j<0ThenExitDo Loop Arr(j+1)=TempValue Next End Function '========================================= '简单选择排序 'Arr------等待排序的数组 '========================================= Function SelectSort(Arr) Dim i,j,K,TempValue For i=0ToUBound(Arr)-1 K=i For j=i+1ToUBound(Arr) If Arr(j)<Arr(K) Then K=j EndIf Next If K<>i Then TempValue=Arr(K) Arr(k)=Arr(i) Arr(i)=TempValue EndIf 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) %>