• [转载]常用内部排序算法--用VB6实现


    转自(http://www.cnblogs.com/IDqq/archive/2006/04/04/366688.html)

    1 冒泡排序
    冒泡排序虽然性能不高,但好歹是一种稳定的排序。在要求排序结果稳定,或者性能问题不很突出(比如给只含少量元素的小数组排序)的时候,用起来还是比较方便。下面给出一个较为通用的实现。

    ' 升序冒泡
     Private Sub Sort(ByRef ary As Variant)
         Dim i               As Long
         Dim j               As Long
         Dim vTmp            As Variant
     
         For i = 0 To UBound(ary) - 1
             For j = i + 1 To UBound(ary)
                 If ary(i) > ary(j) Then
                    vTmp = ary(i)
                    ary(i) = ary(j)
                    ary(j) = vTmp
                End If
            Next
        Next
    End Sub

    2 快速排序
    快排可以说是应用最为广泛的一种排序了。快排的平均排序性能最好,但不是稳定的排序。

    ' 升序快排
     Private Sub QuickSort(ByRef ary As Variant)
         Call QSort(ary, 0, UBound(ary))
     End Sub
     
     ' 子函数:对子数组递归排序
     Private Sub QSort(ByRef ary As Variant, ByVal lLow As Long, ByVal lHigh As Long)
         Dim lPivot          As Long
     
        If lLow < lHigh Then
            lPivot = Part(ary, lLow, lHigh)
            Call QSort(ary, lLow, lPivot - 1)
            Call QSort(ary, lPivot + 1, lHigh)
        End If
    End Sub
    
    ' 子函数:选定ary(lLow)为枢轴,把数组分成比枢轴大/小的两部分
    Private Function Part(ByRef ary As Variant, ByVal lLow As Long, ByVal lHigh As Long) As Long
        Dim vPivot          As Variant
    
        vPivot = ary(lLow)
        Do While lLow < lHigh
            Do While lLow < lHigh And ary(lHigh) >= vPivot
                lHigh = lHigh - 1
            Loop
            If lLow = lHigh Then Exit Do
            ary(lLow) = ary(lHigh)
            Do While lLow < lHigh And ary(lLow) <= vPivot
                lLow = lLow + 1
            Loop
            If lLow = lHigh Then Exit Do
            ary(lHigh) = ary(lLow)
        Loop
        ary(lLow) = vPivot
        Part = lLow
    End Function

    3 用法示例

    Dim ary As Variant
    Dim byt As Byte()
    ary = Array(1, 3, 2, 5, 12, 8, 7, 9)
    Sort ary
    byt = StrConv("AZCDI", vbFromUnicode)
    Sort byt
    ary = Array(8, 1, 3, 2, 5, 12, 7, 9)
    QuickSort ary
  • 相关阅读:
    #region...#endregion: C# syntax
    Regular Expression in C#
    北京邮电大学网络与交换技术国家重点实验室
    C#: 得到系统中的环境变量(源代码)
    Contrasting C# and Java Syntax(摘录)
    Using ZipLib to create a Zip File in C#
    SQL Server DO's and DONT's 摘录
    SQL View 的使用语法与原则
    时隔两年
    VS 里的快捷键定制
  • 原文地址:https://www.cnblogs.com/Spacecup/p/3865037.html
Copyright © 2020-2023  润新知