在Visual Basic中快速排序

时间:2017-02-15 10:09:21

标签: visual-studio quicksort

我试图在VB2015中进行快速排序,但是当我运行它时,值不会完全排序(但它几乎排序)。我很确定这个问题与两个重复的行有关。

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles   Button1.Click
    numbers = TextBox1.Text.Split()
    Dim tempstring As String
    Form2.Show()
    tempstring = ""
    quicksort(numbers, numbers.Length() - 1, 0)
    For Each a As String In numbers
        tempstring = tempstring + a + " "
    Next
    TextBox2.Text = tempstring
    Form2.Show()
    Form2.Chart1.Series(0).Points.DataBindY(numbers)
End Sub

Public Sub quicksort(list As Array, high As Integer, low As Integer)
    MessageBox.Show(Str(high) + " " + Str(low))
    ListView1.Items.Add(Str(high) + " " + Str(low))
    Dim i As Integer
    Dim pivot As Integer
    'pivot = (high + low) / 2
    pivot = high
    If high > low + 1 And low >= 0 Then
        i = low
        For c = low + 1 To high
            If Int(list(c)) <= Int(list(pivot)) Then
                swap(list, c, i)
                i = i + 1
            End If
        Next
        quicksort(numbers, i - 2, low)
        quicksort(numbers, high, i)
    End If
End Sub
Public Sub swap(list As Array, x As Integer, y As Integer)
    Dim temp As Integer
    temp = list(x)
    list(x) = list(y)
    list(y) = temp
    Form2.Chart1.Series(0).Points.DataBindY(numbers)
    'pause()
End Sub

1 个答案:

答案 0 :(得分:0)

我知道这很旧,但是可能有人碰到了。您的SWAP子程序需要传递参数ByRef,否则交换仅在子程序的变量内进行,而不在QuickSort例程内进行。