我遇到了一些我知道缺少TheList
数组的编程代码,但无法确定将其放在何处。这来自修订剑桥教科书(第174页),似乎不完整。我只是坚持Sub Main()
,因为它说你不能省略它。任何伟大的想法将不胜感激!
Module Module1
Dim TheList(20) As Integer
Sub Main()
TheList(1) = 20
TheList(2) = 2
TheList(3) = 22
TheList(4) = 4
TheList(5) = 5
TheList(6) = 28
TheList(7) = 21
TheList(8) = 6
TheList(9) = 19
TheList(10) = 18
TheList(11) = 7
TheList(12) = 16
TheList(13) = 8
TheList(14) = 56
TheList(15) = 92
TheList(16) = 1
TheList(17) = 3
TheList(18) = 42
TheList(19) = 76
TheList(20) = 84
Call SelectionSort()
Call DisplayList()
End Sub
Sub DisplayList()
Dim Index As Integer
Console.WriteLine()
For Index = 1 To 20
Console.Write(TheList(Index) & " ")
Next
End Sub
Sub SelectionSort()
Dim SortedListPosn, InsertPosn, SortedPosn As Integer
Dim Index, ShufflePosn As Integer
Dim CurrentValue As Integer
Dim InsertPosnFound As Boolean
For Index = 2 To 9
CurrentValue = TheList(Index)
SortedListPosn = 1
InsertPosnFound = False
Do
If CurrentValue > TheList(SortedPosn) Then
SortedListPosn = SortedListPosn + 1
Else
InsertPosn = SortedListPosn
InsertPosnFound = True
End If
Loop Until InsertPosnFound = True
For ShufflePosn = Index To (InsertPosn + 1) Step -1
TheList(ShufflePosn) = TheList(ShufflePosn - 1)
Next
TheList(InsertPosn) = CurrentValue
Next
End Sub
结束模块
答案 0 :(得分:0)
你的问题是你的SelectionSort,当我调试它时,它最终会抛出“算术运算导致溢出”。
你需要自己编写吗? VB.Net已经内置了可以做你想做的库。例如,您可以使用Array.Sort(TheList)。
Module Module1
Dim TheList(20) As Integer
Sub Main()
TheList(1) = 20
TheList(2) = 2
TheList(3) = 22
TheList(4) = 4
TheList(5) = 5
TheList(6) = 28
TheList(7) = 21
TheList(8) = 6
TheList(9) = 19
TheList(10) = 18
TheList(11) = 7
TheList(12) = 16
TheList(13) = 8
TheList(14) = 56
TheList(15) = 92
TheList(16) = 1
TheList(17) = 3
TheList(18) = 42
TheList(19) = 76
TheList(20) = 84
'Call SelectionSort()
Array.Sort(TheList)
Call DisplayList()
' Put this to pause the console window from closing, then just press the ENTER key
Console.ReadLine()
End Sub
Sub DisplayList()
Dim Index As Integer
Console.WriteLine()
For Index = 1 To 20
Console.Write(TheList(Index) & " ")
Next
End Sub
End Module
结果:
那么我们如何修复选择排序......试一试......
Module Module1
Dim TheList(20) As Integer
Sub Main()
TheList(1) = 20
TheList(2) = 2
TheList(3) = 22
TheList(4) = 4
TheList(5) = 5
TheList(6) = 28
TheList(7) = 21
TheList(8) = 6
TheList(9) = 19
TheList(10) = 18
TheList(11) = 7
TheList(12) = 16
TheList(13) = 8
TheList(14) = 56
TheList(15) = 92
TheList(16) = 1
TheList(17) = 3
TheList(18) = 42
TheList(19) = 76
TheList(20) = 84
Call SelectionSort()
' Call QuickSort(1, 20)
Call DisplayList()
Console.ReadLine()
End Sub
Sub DisplayList()
Dim Index As Integer
Console.WriteLine()
For Index = 1 To 20
Console.Write(TheList(Index) & " ")
Next
End Sub
Sub SelectionSort()
Dim CurrentIndex, NextIndex, SmallestValueIndex, TempValue As Integer
For CurrentIndex = 1 To 20
SmallestValueIndex = CurrentIndex
For NextIndex = CurrentIndex + 1 To 20
' Find a number in the list that's the smallest to swap into the current position
If TheList(NextIndex) < TheList(SmallestValueIndex) Then
SmallestValueIndex = NextIndex
End If
Next
' Swap the current index location with the smallest's value location
TempValue = TheList(CurrentIndex)
TheList(CurrentIndex) = TheList(SmallestValueIndex)
TheList(SmallestValueIndex) = TempValue
Next
End Sub
Sub QuickSort(ByVal Left As Integer, ByVal Right As Integer)
Dim I As Integer = Left
Dim J As Integer = Right
Dim Pivot As Integer = TheList((Left + Right) / 2)
While I <= J
While TheList(I) < Pivot
I = I + 1
End While
While TheList(J) > Pivot
J = J - 1
End While
If I <= J Then
' Swap
Dim Temp = TheList(I)
TheList(I) = TheList(J)
TheList(J) = Temp
I = I + 1
J = J - 1
End If
End While
' Recursion
If Left < J Then
QuickSort(Left, J)
End If
If I < Right Then
QuickSort(I, Right)
End If
End Sub
End Module
结果(选择排序):
结果(快速排序):