【问题标题】:Swaping positions within an array vb.net在数组 vb.net 中交换位置
【发布时间】:2018-04-13 19:55:14
【问题描述】:

我正在尝试编写一个包含两个部分的程序,具体取决于按下了两个按钮中的哪一个。

第一部分是正在工作的位,用户按下标有“unsort”的第一个按钮,这会触发一个循环,该循环显示一个输入框,要求输入随机数 8 次。这 8 个数字存储在一个数组中。

但是,这是我正在努力解决的第二部分;第二个按钮被标记为排序,应该输出用户刚刚使用第一个按钮输入的数字,从小到大。我知道这里必须使用冒泡排序,并且还必须使用循环中的循环,但是我不明白这些循环的内容。自从我的原始帖子以来,我已经编辑了帖子以在我之前坚持的循环中包含一些代码,但是它仍然没有产生所需的输出(所有数字按顺序排列)而是只是以看似随机的方式输出数字顺序

代码贴在下面,带有注释:

Public Class BubbleSort1
    Dim Bubble(8) As Integer
    Dim UnsortedList As String
    Dim n As Integer
    Dim SortedList As String
    Dim temp As String


    Private Sub btnUnsort_Click(sender As Object, e As EventArgs) Handles btnUnsort.Click
        n = 8 ' number off values on array
        For i = 1 To n ' when i is between 1 and size of array
            Bubble(i) = InputBox("Enter Number") ' User inputs a number
            UnsortedList = UnsortedList & " " & Bubble(i) & vbNewLine ' number is added to the unsorted list variable
        Next i
        lblUnsort.Text = UnsortedList ' outputs the array
    End Sub

    Private Sub btnSort_Click(sender As Object, e As EventArgs) Handles btnSort.Click

        For i = 1 To n - 1 ' When i is between 1 and the array size - 1 (8-1):
            For j = 1 To n - 1 ' Second loop - when j is between 1 and the array size - 1 (8-1):
                If Bubble(j) > Bubble(j + 1) Then ' if bubble value j is greater than value j - 1:
                    temp = Bubble(j)
                    Bubble(j) = Bubble(j + 1) ' These lines are supost to order the numbers but aren'r currently doing so
                    Bubble(j + 1) = temp
                    SortedList = SortedList & Bubble(j) & vbNewLine ' Adding the number in order to a variable 
                End If
            Next j
        Next i

        lblSort.Text = SortedList ' outputting the ordered numbers

    End Sub
End Class

正如代码中所指出的,此代码中对数字进行排序的部分只是将它们按随机顺序排列,而不是实际对它们进行排序。

【问题讨论】:

  • 这将为您提供学校项目所需的信息。 en.wikipedia.org/wiki/Bubble_sort你放一些代码后我会帮你的。
  • 如果你有一杯红色的水(在上面贴上“Bubble(j)”的标签),一杯蓝色的水(在上面贴上“Bubble( j + 1)") 和一个标有“temp”的空玻璃杯,您将如何交换玻璃杯之间的有色水?
  • “明白这里必须使用冒泡排序”这是一个要求吗? .net 中已经内置了排序功能
  • @the_lotus 学校项目...老师想看看他们是否理解这个概念以及他们是否能够应用它
  • 是的,这是任务的要求

标签: arrays vb.net loops bubble-sort


【解决方案1】:

使用包含交换数组元素的更新代码,您正在构建显示排序数组的字符串太快:它将显示工作原理而不是最终结果。

您需要做的就是在数组有序后构建字符串:

Private Sub btnSort_Click(sender As Object, e As EventArgs) Handles btnSort.Click

    ' Bubble sort the array...
    For i = 1 To n - 1 ' When i is between 1 and the array size - 1 (8-1):
        For j = 1 To n - 1 ' Second loop - when j is between 1 and the array size - 1 (8-1):
            If Bubble(j) > Bubble(j + 1) Then ' if bubble value j is greater than value j - 1:
                temp = Bubble(j)
                Bubble(j) = Bubble(j + 1)
                Bubble(j + 1) = temp
            End If
        Next j
    Next i

    'lblSort.Text = String.Join(vbNewLine, Bubble.Skip(1)) ' an easy one-liner

    ' Create a string to show the sorted array...
    SortedList = "" ' clear it out in case it was used previously
    For i = 1 To n
        SortedList = SortedList & Bubble(i).ToString()
        If i < n Then ' only add a newline if it isn't the last element
            SortedList = SortedList & vbNewLine
        End If
    Next

    lblSort.Text = SortedList

End Sub

我把.ToString() 放在那里,期待您将输入字符串显式转换为数字;严格来说,&amp; 运算符会将其参数转换为字符串,但我更喜欢在代码中使其显而易见。


正如您的代码一样,存在从输入(数字字符串)到整数(数组元素的类型)的隐式转换。虽然这看起来很方便,但如果 VB 为您猜错了转换,则可能会出现问题。如果变量的类型不匹配,有一种方法可以告诉您:将Option Strict On 作为第一行,它甚至会为您提供关于需要采取哪些措施才能正确处理的建议。

【讨论】:

    【解决方案2】:

    如果要提示用户输入,则首先需要使用 NumericUpDown 等控件获取数值,或者需要使用 Integer.TryParse 将字符串值转换为整数值。另外,请记住,VB.Net 中的数组具有基于 0 的索引,因此它们从 0 开始,而不是从 1 开始。

    就冒泡排序算法而言,您需要像 ij 那样的嵌套循环,只有内部嵌套循环( j) 需要从数组的开头迭代到倒数第二项(0 到 n-2)。在嵌套循环内部,您将比较当前迭代的值是否大于(或小于取决于要交换的值)下一个值。如果是这样,那么您只需重新分配当前迭代索引处的值。

    这是我创建的一个控制台应用程序示例,它不会提示用户输入随机值,而是简单地获取一组随机值,​​然后执行冒泡排序:

    Private Function BubbleSort(ByVal values() As Integer) As Integer()
        'Declare placeholder variables to use in the iterations
        Dim temp As Integer
    
        For outterIndex As Integer = 0 To values.Length - 1
            For innerIndex As Integer = 0 To values.Length - 2
                If values(innerIndex) > values(innerIndex + 1) Then
                    temp = values(innerIndex + 1)
                    values(innerIndex + 1) = values(innerIndex)
                    values(innerIndex) = temp
                End If
            Next
        Next
    
        Return values
    End Function
    
    Private r As New Random()
    Private Function RandomNumbers(ByVal range As Integer) As Integer()
        'Throw an exception if the value is less than 1
        If range < 1 Then Throw New ArgumentOutOfRangeException("The range cannot be less than 1")
    
        'Return a collection of random numbers
        Return Enumerable.Range(1, range).Select(Function(i) r.Next()).ToArray()
    End Function
    

    小提琴:Live Demo

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-01-30
      • 2018-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-11
      • 2012-10-16
      相关资源
      最近更新 更多