交换数组中的位置 vb.net
Swaping positions within an array vb.net
我正在尝试编写一个程序,该程序具有两个部分,具体取决于按下两个按钮中的哪一个。
第一部分是正在工作的位,用户按下标有 "unsort" 的第一个按钮,这会触发一个循环,该循环显示一个输入框,要求随机数 8 次。这8个数字存储在一个数组中。
然而这是我正在努力的第二部分;第二个按钮被标记为排序并且应该输出用户刚刚使用第一个按钮输入的数字是顺序,从最小到最大。我知道这里必须使用冒泡排序,并且还必须使用循环中的循环,但是我不明白这些循环的内容。由于我原来的 post 我已经编辑了 post 以在我之前坚持的循环中包含一些代码,但是它仍然没有产生所需的输出(所有数字按顺序排列)而是相反只是以看似随机的顺序输出数字
代码在下面post编辑并带有注释:
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
正如代码中所指出的,这段代码中对数字进行排序的部分只是将它们随机排序,而不是实际排序。
如果要提示用户输入,则首先需要使用 NumericUpDown 等控件获取数值,或者需要使用 [=11 将字符串值转换为整数值=].此外,请记住 VB.Net 中的数组具有基于 0 的索引,因此它们从 0 开始,而不是 1。
就冒泡排序算法而言,您需要像 i 和 j 一样的嵌套循环,只有您的内嵌循环(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
Fiddle: Live Demo
使用现在包含数组元素交换的更新代码,您正在构建显示已排序数组的字符串太快了:它将显示工作原理而不是最终结果。
您需要做的就是在数组有序后构建字符串:
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()
放在那里是因为期待您明确地将输入的字符串转换为数字;严格来说,&
运算符会将其参数转换为字符串,但我更愿意在代码中使其显而易见。
就像您的代码一样,存在从输入(一串数字)到整数(数组元素的类型)的隐式转换。虽然这看起来很方便,但如果 VB 为您猜测错误的转换,则可能会出现问题。有一种方法可以告诉它在变量类型不匹配时通知您:将 Option Strict On
放在第一行,它甚至会为您提供建议,告诉您需要做什么才能正确处理。
我正在尝试编写一个程序,该程序具有两个部分,具体取决于按下两个按钮中的哪一个。
第一部分是正在工作的位,用户按下标有 "unsort" 的第一个按钮,这会触发一个循环,该循环显示一个输入框,要求随机数 8 次。这8个数字存储在一个数组中。
然而这是我正在努力的第二部分;第二个按钮被标记为排序并且应该输出用户刚刚使用第一个按钮输入的数字是顺序,从最小到最大。我知道这里必须使用冒泡排序,并且还必须使用循环中的循环,但是我不明白这些循环的内容。由于我原来的 post 我已经编辑了 post 以在我之前坚持的循环中包含一些代码,但是它仍然没有产生所需的输出(所有数字按顺序排列)而是相反只是以看似随机的顺序输出数字
代码在下面post编辑并带有注释:
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
正如代码中所指出的,这段代码中对数字进行排序的部分只是将它们随机排序,而不是实际排序。
如果要提示用户输入,则首先需要使用 NumericUpDown 等控件获取数值,或者需要使用 [=11 将字符串值转换为整数值=].此外,请记住 VB.Net 中的数组具有基于 0 的索引,因此它们从 0 开始,而不是 1。
就冒泡排序算法而言,您需要像 i 和 j 一样的嵌套循环,只有您的内嵌循环(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
Fiddle: Live Demo
使用现在包含数组元素交换的更新代码,您正在构建显示已排序数组的字符串太快了:它将显示工作原理而不是最终结果。
您需要做的就是在数组有序后构建字符串:
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()
放在那里是因为期待您明确地将输入的字符串转换为数字;严格来说,&
运算符会将其参数转换为字符串,但我更愿意在代码中使其显而易见。
就像您的代码一样,存在从输入(一串数字)到整数(数组元素的类型)的隐式转换。虽然这看起来很方便,但如果 VB 为您猜测错误的转换,则可能会出现问题。有一种方法可以告诉它在变量类型不匹配时通知您:将 Option Strict On
放在第一行,它甚至会为您提供建议,告诉您需要做什么才能正确处理。