2010年10月18日 星期一

遞迴版插入排序

'遞迴版插入排序
Public Class Form1
    Dim a() = {9, 1, 8, 7, 6, 5, 4, 10, 3, 2}
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim str1 = ""
        Call rsort(1, a)
        For i = 0 To a.Length - 1
            str1 = str1 & a(i) & Space(3)
        Next
        MsgBox(str1)
        End
    End Sub
    Sub rsort(ByVal n, ByRef a)
        If n = a.length Then
            Return
        Else
            Dim p = a(n)
            Dim j = 0
            While a(j) < p
                j = j + 1
            End While
            For k = n To j + 1 Step -1
                a(k) = a(k - 1)
            Next
            a(j) = p
            rsort(n + 1, a)
        End If
    End Sub
End Class

沒有留言: