Sorting Arrays in Visual Basic

Array sorting.  Two words that would strike the fear of God into me when I was at college.   Sorting in general used to strike the fear of God into me as it is/was such a dry, uninspiring subject.   Modern programmers rarely have to worry about sorting algorithms, and nor should they, they’ve been done to death by folk far brighter than ourselves.  There’s scores of algorithms that mathematicians have devised for all sorts of reasons and, if you’re that way inclined, take a look at this Wikipedia page on the subject.  It’s a great cure for insomnia.   That said, this tutorial on sorting arrays in Visual Basic is a great means to revisit everything we’ve learnt so far.  Variables, Loops, Function & Subroutines , Arrays neatly rounded up in one powerful tutorial.

There are a wide wide variety of routines for sorting, with different characteristics, and today we we look at the Selection Sort algorithm.  This algorithm is relatively simple and performs reasonably well, especially  with smaller datasets.  To sort an array in ascending order, the Selection Sort algorithm is as follows:

Selection Sort

To implement this in Visual Basic we can use the following Subroutine

Private Sub SortArray(ByRef array() As Integer)

     Dim i As Integer
     Dim j As Integer
     Dim minimum As Integer
     Dim swapValue As Integer
     Dim upperBound As Integer
     Dim lowerBound As Integer

     lowerBound = LBound(array)
     upperBound = UBound(array)

     For i = lowerBound To upperBound

          minimum = i

          For j = i + 1 To upperBound
               'Search for the smallest remaining item in the array
               If array(j) < array(minimum) Then
                    'A smaller value has been found, remember the position in the array
                    minimum = j
               End If
          Next j

          If minimum <> i Then
               'Swap array Values
               swapValue = array(minimum)
               array(minimum) = array(i)
               array(i) = swapValue
          End If

     Next i
End Sub

Have a read of that code.  All of the concepts will be relatively familar by now, but take a good look at how this routine implements the described algorithm.  Implementing code from a written description is an extremely important part of being a software developer.  Understanding other people’s code perhaps even more so.

Create a New Project called Sort Array.

Add a ListBox onto the Form called ListBoxSorted.

Paste this Sort routine into the form

In the Form_Load event paste the following:

Dim myArray(9) As Integer
Dim i As Integer

myArray(0) = 34
myArray(1) = 12
myArray(2) = 45
myArray(3) = 54
myArray(4) = 1
myArray(5) = 12
myArray(6) = 198
myArray(7) = 23
myArray(8) = 18
myArray(9) = 7

ListBoxSorted.Items.Add("Unordered Array")

For i = LBound(myArray) To UBound(myArray)


ListBoxSorted.Items.Add("Ordered Array")

For i = LBound(myArray) To UBound(myArray)

This code creates an array of 10 elements, adds random numbers to the elements, displays the unordered array in the ListBox, calls the SortArray routine and displays the sorted array.

Can you improve this routine?  Can you see any repetition?  How would you fix that?  Could you also sort multi-dimensional arrays?  How?

Of course, you can do exactly what was described here by using the Array.Sort method but at this stage in your careers, understanding the fundamentals is absolutely key.

“Any fool can know. The point is to understand.”