Selection Sort
Selection sort is a sorting algorithm. It works by selecting the smallest element of the array and placing it at the head of the array. Then the process is repeated for the remainder of the array, the next largest element is selected and put into the next slot, and so on down the line.
Public Shared Sub SelectionSort(ByRef data As Integer())
Dim min As Integer
For i As Integer = 0 To data.Length - 2
min = i
For j As Integer = i + 1 To data.Length - 1
If data(j) < data(min) Then
min = j
End If
Next
Dim temp As Integer = data(min)
data(min) = data(i)
data(i) = temp
Next
End Sub
Example
Dim data() As Integer = { -1, 25, -58964, 8547, -119, 0, 78596 }
SelectionSort(data)
Output
-58964
-119
-1
0
25
8547
78596