分享你我的心得.
共乘一片美好网络.

asp 数组排序

<% Dim aData aData = Array(3,2,4,1,6,0) Call ResponseArray(aData, “原来顺序”) Call ResponseArray(SelectSort(aData), “选择排序”) Call ResponseArray(QuickSort(aData), “快速排序”) Call ResponseArray(InsertSort(aData), “插入排序”) Call ResponseArray(BubbleSort(aData), “冒泡排序”) Call ResponseArray(ReQuickSort(aData), “反序排序”)

Response.Write “<b>最 大 值:</b>” & Max(aData) & “<hr>” Response.Write “<b>最 小 值:</b>” & Min(aData) & “<hr>” ‘=================================== ‘选择排序 ‘=================================== Function SelectSort(a_Data) Dim i, j, k Dim bound, t bound = UBound(a_Data)

For i = 0 To bound-1 k = i For j = i+1 To bound If a_Data(k) > a_Data(j) Then k = j End If Next t = a_Data(i) a_Data(i) = a_Data(k) a_Data(k) = t Next ‘此内容来源于 SelectSort = a_Data End Function ‘=================================== ‘快速排序 ‘=================================== Function QuickSort(a_Data) Dim i, j Dim bound, t bound = UBound(a_Data)

For i = 0 To bound-1 For j = i+1 To bound If a_Data(i) > a_Data(j) Then t = a_Data(i) a_Data(i) = a_Data(j) a_Data(j) = t End If Next Next QuickSort = a_Data End Function ‘=================================== ‘冒泡排序 ‘=================================== Function BubbleSort(a_Data) Dim bound bound = UBound(a_Data) Dim bSorted, i, t bSorted = False

Do While bound > 0 And bSorted = False

bSorted = True For i = 0 To bound-1 If a_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 End If Next bound = bound – 1 Loop

BubbleSort = a_Data End Function ‘=================================== ‘插入排序 ‘=================================== Function InsertSort(a_Data) Dim bound bound = UBound(a_Data) Dim i, j, t

For i = 1 To bound t = a_Data(i) j = i Do While t<a_Data(j-1) And j>0 a_Data(j) = a_Data(j-1) j = j – 1 Loop a_Data(j) = t Next InsertSort = a_Data End Function ‘=================================== ‘快速排序-反序排列 ‘=================================== Function ReQuickSort(a_Data) Dim i,Bound,TempArr a_Data = QuickSort(a_Data) TempArr = QuickSort(a_Data) Bound = Ubound(a_Data) For i = 0 To Bound a_Data(i) = TempArr(Bound-i) Next ReQuickSort = a_Data End Function ‘=================================== ‘输出数组 ‘=================================== Sub ResponseArray(a_Data, str) Dim s s = “” Response.Write “<b>” & str & “:</b>” For i = 0 To UBound(a_Data) s = s & a_Data(i) & “,” Next s = Left(s, Len(s)-1) Response.Write s Response.Write “<hr>” End Sub ‘=================================== ‘求数组最大值 ‘=================================== Function Max(a_Data) Dim i,j,Bound,Temp a_Data = QuickSort(a_Data) Bound = Ubound(a_Data) For i = 0 To Bound For j = i+1 To Bound If a_Data(j) > a_Data(i) Then Temp = a_Data(i) a_Data(i) = a_Data(j) a_Data(j) = Temp End If Next Next Max = a_Data(0) End Function ‘=================================== ‘求数组最小值 ‘=================================== Function Min(a_Data) Dim i,j,Bound,Temp a_Data = QuickSort(a_Data) Bound = Ubound(a_Data) For i = 0 To Bound For j = i+1 To Bound If a_Data(j) > a_Data(i) Then Temp = a_Data(i) a_Data(i) = a_Data(j) a_Data(j) = Temp End If Next Next Min = a_Data(Bound) End Function

%>

赞(0)
未经允许不得转载:小叶白龙博客 » asp 数组排序

评论 抢沙发

登录

找回密码

注册