常用的几种asp排序方法介绍


以下代码为常用的几种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), "冒泡排序")


'选择排序
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
%>

[本日志由 随然 于 2009-01-13 10:25 PM 编辑]
上一篇: ASP读取记录总数的方法
下一篇: ASP从字符串中提取数字或者字符串转数字型的方法
文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags: ASP 网页
相关日志:
评论: 3781 | 引用: 0 | 查看次数: 59941
发表评论
昵 称:
密 码: 游客发言不需要密码.
邮 箱: 邮件地址支持Gravatar头像,邮箱地址不会公开.
网 址: 输入网址便于回访.
内 容:
验证码:
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.
字数限制 1 字 | UBB代码 关闭 | [img]标签 关闭