Here is a "Bubble" sort in VB6. I don't have
VB.Net available, so I wrote it in VB6 and you will have to do the conversion. The bubble sort is fast for a small array. It will be slow for a large array. There are many sorts available, some are good for small arrays, and some are good for large arrays. As you can see, the worst case for this is to do N^2 calculations where N is the number of elements in the array. Some of the sorts that are better for large arrays will have a worst case of N*Log(N) (which is smaller than N^2).
Code:
Private Sub Command1_Click()
Dim numberArray(9) As String
Dim indexCounter As Long
Dim bubbleCounter As Long
Dim temporaryHolder As String
' Generate numbers.
Call Randomize
Debug.Print "Original Positions:"
For indexCounter = 0 To 9
numberArray(indexCounter) = CStr(Int(Rnd() * 100) + 1)
Debug.Print indexCounter & vbTab & numberArray(indexCounter)
Next
' Sort the array.
For indexCounter = 1 To 9
For bubbleCounter = indexCounter To 1 Step -1
If Val(numberArray(bubbleCounter)) < Val(numberArray(bubbleCounter - 1)) Then
temporaryHolder = numberArray(bubbleCounter - 1)
numberArray(bubbleCounter - 1) = numberArray(bubbleCounter)
numberArray(bubbleCounter) = temporaryHolder
Else
Exit For
End If
Next
Next
Debug.Print "Sorted:"
For indexCounter = 0 To 9
Debug.Print indexCounter & vbTab & numberArray(indexCounter)
Next
End Sub
John R Lick
[email protected]