VB Selectionsort
' ******************************************************* ' Selectionsort. ' Min and max can be used to set the part in the list being sorted (default whole list) ' Example Selectionsort lstUsers, 2 where the first in lstUsers is' limit the number? not have to determine size? ' ******************************************************* Sub Selectionsort(list As Variant, Optional min As Integer = -2, Optional max As Integer = -2) Dim i As Integer Dim j As Integer Dim best_value As Variant 'Long Dim best_j As Integer If min = -2 Then min = LBound(list) If max = -2 Then max = UBound(list) For i = min To max - 1 best_value = list(i) best_j = i For j = i + 1 To max If list(j) < best_value Then best_value = list(j) best_j = j End If Next j list(best_j) = list(i) list(i) = best_value Next i End Sub Private Sub SelectionsortTest0() Dim list As Variant list = Array(2, 3, 1, 4) Selectionsort list, 0, 3 Debug.Print list(0) Debug.Print list(1) Debug.Print list(2) Debug.Print list(3) End Sub Private Sub SelectionsortTest1() Dim list(0 To 3) As Long list(0) = 2 list(1) = 3 list(2) = 1 list(3) = 4 Selectionsort list Debug.Print list(0) Debug.Print list(1) Debug.Print list(2) Debug.Print list(3) End Sub Private Sub SelectionsortTest2() Dim list(0 To 3) As String list(0) = "a" list(1) = "" list(2) = "d" list(3) = "c" Selectionsort list Debug.Print list(0) Debug.Print list(1) Debug.Print list(2) Debug.Print list(3) End Sub