asp下幾種常用排序演算法

來源:互聯網
上載者:User

<%

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), "冒泡排序")

'選擇排序
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

'輸出數組
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
%>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.