数组Visual Basic中的排序列表

时间:2015-07-09 14:47:13

标签: vb.net visual-studio-2010 visual-studio sorting

我有一个程序可以创建一个包含0到1,000个25个随机数的列表。我必须按钮第一个按钮将加载带有随机数字的列表框,第二个按钮将按照从最小到最大的数字列表排序,这是我实现冒泡排序代码的地方。现在另一个应该保存已排序数字的列表框不能正常工作它只显示一个数字而不是全部数字。

这是我的代码:

Option Strict On
Public Class Form1

Dim rn As Random = New Random
Dim Clicked As Long = 0
Dim numbers, sort As Long


Private Sub GenerateBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GenerateBtn.Click
    Clicked += 1

    For x = 0 To 25
        numbers = rn.Next(0, 1000)
        RandomBox.Items.Add(numbers)
        If Clicked >= 2 Then
            RandomBox.Items.Clear()
            Clicked = 1
        End If
    Next
End Sub


Private Sub SortBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SortBtn.Click
    Dim Sorted() As Long = {numbers}
    Dim Swapped As Boolean
    Dim endOfArray As Integer = Sorted.Length - 1
    Dim Tmp As Byte

    While (Swapped)
        Swapped = False
        For I = 0 To endOfArray - 1
            If Sorted(I) > Sorted(I + 1) Then
                Tmp = CByte(Sorted(I))
                Sorted(I) = Sorted(I + 1)
                Sorted(I + 1) = Tmp
                Swapped = True
            End If
            endOfArray = endOfArray - 1
        Next
    End While

    SortBox.Items.Clear()

    For I = 0 To Sorted.Count - 1
        SortBox.Items.Add(Sorted(I))
    Next

End Sub
End Class

2 个答案:

答案 0 :(得分:1)

改变你的:

Dim Sorted() As Long = {numbers}

Sorted(x) = numbers

编辑:因为您更改了代码。您需要放回加载已排序数组的行。

For x = 0 To 25
    numbers = rn.Next(0, 1000)
    RandomBox.Items.Add(numbers)
    Sorted(x) = numbers
    If Clicked >= 2 Then
        RandomBox.Items.Clear()
        Clicked = 1
    End If
Next

并删除:

Dim Sorted() As Long = {numbers}

从第二部分开始,像你一样把这个声明放回到开头:

Dim Sorted(26) as Long

您拥有的方式只会显示最新的随机数。它不是任何数组而是单个实体。因此,只有最新版本才会添加到数组中。您需要在创建每个数字时将每个数字加载到数组中。因此,(x)将其加载到位置x。

答案 1 :(得分:0)

您在项目中根本没有使用任何数组 ...您使用ListBox作为存储介质并且这是一种非常糟糕的做法。< / p>

我建议你这样设置:

Public Class Form1

    Private rn As New Random
    Private numbers(24) As Integer ' 0 to 24 = 25 length

    Private Sub GenerateBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GenerateBtn.Click
        For x As Integer = 0 To numbers.Length - 1
            numbers(x) = rn.Next(0, 1000)
        Next

        ' reset the listbox datasource to view the random numbers
        RandomBox.DataSource = Nothing
        RandomBox.DataSource = numbers

        ' empty out the sorted listbox
        SortBox.DataSource = Nothing
    End Sub

    Private Sub SortBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SortBtn.Click
        ' make a COPY of the original array that we will sort:
        Dim sorted(numbers.Length - 1) As Integer
        Array.Copy(numbers, sorted, numbers.Length)

        Dim Swapped As Boolean = True
        Dim endOfArray As Integer = Sorted.Length - 1
        Dim Tmp As Integer

        While (Swapped)
            Swapped = False
            For I As Integer = 0 To endOfArray - 1
                If sorted(I) > sorted(I + 1) Then
                    Tmp = sorted(I)
                    sorted(I) = sorted(I + 1)
                    sorted(I + 1) = Tmp
                    Swapped = True
                End If
            Next
            endOfArray = endOfArray - 1
        End While

        ' reset the listbox datasource to view the sorted numbers
        SortBox.DataSource = Nothing
        SortBox.DataSource = sorted
    End Sub

End Class

另外,请注意您在endOfArray循环内递减for 。你应该只在每次通过后递减;所以在for循环之外,但在while循环内。

此外,您使用的是Tmp类型为Byte的变量,但生成的数字介于0到999(含)之间。 Byte类型只能包含0到255之间的值(包括)。

您的冒泡排序实施非常接近来纠正!