Visual Basic 2010 (Console) Guide
Selection Sort

The selection sort works by looking at the first item in the array. It compares this with all of the other elements in the array and swaps it with the smallest item in the remainder of the array. Then the second item is examined in the same way. This process continues with each element except the last.

The following algorithm can be used,

For outerLoop = 0 To arrayLength - 2
   minValue = outerLoop
   For innerLoop = outerLoop + 1 To arrayLength - 1
      If item[innerLoop] < item[minValue] Then minValue = innerLoop
   Next innerLoop
   temp = item[outerLoop]
   item[outerLoop] = item[minValue]
   item[minValue] = temp
Next outerLoop

Selection Sort Procedure

The following procedure can be used to sort the array that we have created to implement the other algorithms,

Sub SelectionSort()
   Dim min As Integer
   Dim temp As Double
   For outer = 0 To randomArray.Length - 2
      min = outer
      For inner = outer + 1 To randomArray.Length - 1
         If randomArray(inner) < randomArray(min) Then
            min = inner
         End If
      Next inner
      temp = randomArray(outer)
      randomArray(outer) = randomArray(min)
      randomArray(min) = temp
   Next outer
End Sub

Having A Go

  1. Add this little procedure to your sorting program and compare the execution time of all 3 of the sort algorithms you have seen so far. Perform each sort with 100, 1000 and 10000 items.