For numberOfRunsCompleted As Integer = 0 To arrayToSort.Length
For valueWithinArray As Integer = 0 To arrayToSort.Length - 2
If arrayToSort(arrayValue) > arrayToSort(arrayValue 1) Then
Dim tempStorage As Integer = arrayToSort(arrayValue)
arrayToSort(arrayValue) = arrayToSort(arrayValue 1)
arrayToSort(arrayValue 1) = tempStorage
End If
Next
Next
The array is declared as arrayToSort(7)
, being the integers from 9 to 2 inclusive.
CodePudding user response:
I changed the name of your variable valueWithinArray
to index
. It really isn't the value in the array, it is the index of the element. You introduced another undeclared variable (arrayValue
) where you really wanted valueWithinArray
(now called index
).
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim arrayToSort() As Integer = {6, 8, 7, 2, 9, 4, 5, 3}
For numberOfRunsCompleted As Integer = 0 To arrayToSort.Length
For index As Integer = 0 To arrayToSort.Length - 2
If arrayToSort(index) > arrayToSort(index 1) Then
Dim tempStorage As Integer = arrayToSort(index)
arrayToSort(index) = arrayToSort(index 1)
arrayToSort(index 1) = tempStorage
End If
Next
Next
For Each i In arrayToSort
Debug.Print(i.ToString)
Next
End Sub
That was a good learning excercise but to save some typing...
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim arrayToSort() As Integer = {9, 8, 7, 6, 5, 4, 3, 2}
Array.Sort(arrayToSort)
For Each i In arrayToSort
Debug.Print(i.ToString)
Next
End Sub
The results will show up in the Immediate Window.