FILE: How to Sort Algorithms for Numeric Arrays

Last reviewed: June 6, 1997
Article ID: Q169617
The information in this article applies to:
  • Microsoft Visual Basic Professional and Enterprise Editions for Windows, version 5.0
  • Standard, Professional, and Enterprise Editions of Microsoft Visual Basic, 16-bit and 32-bit, for Windows, version 4.0

SUMMARY

SORTED.EXE is a sample that demonstrates how to sort algorithms for numeric arrays. This article describes the three methods of sorting a series of numbers in a code and provides sample code showing how to implement them.

The following file is available for download from the Microsoft Software Library:

 ~ Sorted.exe

For more information about downloading files from the Microsoft Software Library, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q119591
   TITLE     : How to Obtain Microsoft Support Files from
               Online Services

MORE INFORMATION

It is often necessary to sort a series of numbers in code and there are various sorting algorithms available to do this.

The three methods discussed are:

  1. Bubble sort.

  2. Selection sort.

3 Shell sort.

Each routine simply receives an array full of numbers within the bounds of a LONG datatype, although this could be easily changed for different numeric datatypes. Note that the sort routines return the result of the sort operation in the array passed to the routine. Therefore, if you do not want the original array to be modified, copy the array to a variant, as shown in the example below, and then pass the variant to the relevant procedure.

Step-by-Step Example

  1. Start a new project. Form1 is created by default.

  2. Place a CommandButton on the form.

  3. Add the following code to the Form1 code window:

    Option Explicit

       sub Command1_Click()
         Dim lMyArray(0 TO 9) As Long
         Dim vTemp1 As Variant
         Dim vTemp2 As Variant
         Dim vTemp3 As Variant
         Dim iLoop As Integer
         Randomize
         For iLoop = LBound(lMyArray) To UBound(lMyArray)
           lMyArray(iLoop) = Int(Rnd * 100) + 1
         Next iLoop
         vTemp1 = lMyArray
         vTemp2 = lMyArray
         vTemp3 = lMyArray
         Call BubbleSortNumbers(vTemp1)
         Call SelectionSortNumbers(vTemp2)
         Call ShellSortNumbers(vTemp3)
       end sub
    
       Sub BubbleSortNumbers(iArray As Variant)
         Dim lLoop1 As Long
         Dim lLoop2 As Long
         Dim lTemp As Long
         For lLoop1 = UBound(iArray) To LBound(iArray) Step -1
           For lLoop2 = LBound(iArray) + 1 To lLoop1
    
             If iArray(lLoop2 - 1) > iArray(lLoop2) Then
               lTemp = iArray(lLoop2 - 1)
               iArray(lLoop2 - 1) = iArray(lLoop2)
               iArray(lLoop2) = lTemp
             End If
           Next lLoop2
         Next lLoop1
       End Sub
    
       Sub SelectionSortNumbers(vArray As Variant)
         Dim lLoop1 As Long
         Dim lLoop2 As Long
         Dim lMin As Long
         Dim lTemp As Long
         For lLoop1 = LBound(vArray) To UBound(vArray) - 1
           lMin = lLoop1
             For lLoop2 = lLoop1 + 1 To UBound(vArray)
               If vArray(lLoop2) < vArray(lMin) Then lMin = lLoop2
             Next lLoop2
             lTemp = vArray(lMin)
             vArray(lMin) = vArray(lLoop1)
             vArray(lLoop1) = lTemp
         Next lLoop1
       End Sub
    
       Sub ShellSortNumbers(vArray As Variant)
         Dim lLoop1 As Long
         Dim lHold As Long
         Dim lHValue As Long
         Dim lTemp As Long
         lHValue = LBound(vArray)
         Do
           lHValue = 3 * lHValue + 1
         Loop Until lHValue > UBound(vArray)
         Do
           lHValue = lHValue / 3
           For lLoop1 = lHValue + LBound(vArray) To UBound(vArray)
             lTemp = vArray(lLoop1)
             lHold = lLoop1
             Do While vArray(lHold - lHValue) > lTemp
               vArray(lHold) = vArray(lHold - lHValue)
               lHold = lHold - lHValue
               If lHold < lHValue Then Exit Do
             Loop
             vArray(lHold) = lTemp
           Next lLoop1
         Loop Until lHValue = LBound(vArray)
       End Sub
    
    
NOTE: In most cases, the Shell sort is the fastest of the three sorts presented. The SORTED.EXE file (that you can download) includes additional code to determine the efficiency of each sort.


Keywords : kbcode kbsample vb4all vb4win vb5all vb5howto kbhowto
Version : 5.0
Platform : WINDOWS
Resolution Type : kbfile


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: June 6, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.