[點(diǎn)晴永久免費(fèi)OA]asp下幾種常用排序算法
<%
Dim aData aData = Array(3,2,4,1,6,0) Call ResponseArray(aData, "原來(lái)順序") Call ResponseArray(selectSort(aData), "選擇排序") Call ResponseArray(QuickSort(aData), "快速排序") Call ResponseArray(insertSort(aData), "插入排序") Call ResponseArray(BubbleSort(aData), "冒泡排序") '選擇排序 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 '輸出數(shù)組 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 %> 該文章在 2022/6/21 23:10:04 編輯過(guò) |
關(guān)鍵字查詢
相關(guān)文章
正在查詢... |