Microsoft KB Archive/169617: Difference between revisions
(importing KB archive) |
m (Text replacement - "&" to "&") |
||
(3 intermediate revisions by the same user not shown) | |||
Line 111: | Line 111: | ||
For lLoop1 = UBound(iArray) To LBound(iArray) Step -1 | For lLoop1 = UBound(iArray) To LBound(iArray) Step -1 | ||
For lLoop2 = LBound(iArray) + 1 To lLoop1 | For lLoop2 = LBound(iArray) + 1 To lLoop1 | ||
If iArray(lLoop2 - 1) | If iArray(lLoop2 - 1) > iArray(lLoop2) Then | ||
lTemp = iArray(lLoop2 - 1) | lTemp = iArray(lLoop2 - 1) | ||
iArray(lLoop2 - 1) = iArray(lLoop2) | iArray(lLoop2 - 1) = iArray(lLoop2) | ||
Line 120: | Line 120: | ||
endTime = Time() | endTime = Time() | ||
timeToSort = endTime - startTime | timeToSort = endTime - startTime | ||
timeTaken = | timeTaken = "Bubble Sort: " & timeToSort | ||
End Sub | End Sub | ||
Line 133: | Line 133: | ||
lMin = lLoop1 | lMin = lLoop1 | ||
For lLoop2 = lLoop1 + 1 To UBound(vArray) | For lLoop2 = lLoop1 + 1 To UBound(vArray) | ||
If vArray(lLoop2) | If vArray(lLoop2) < vArray(lMin) Then lMin = lLoop2 | ||
Next lLoop2 | Next lLoop2 | ||
lTemp = vArray(lMin) | lTemp = vArray(lMin) | ||
Line 141: | Line 141: | ||
endTime = Time() | endTime = Time() | ||
timeToSort = endTime - startTime | timeToSort = endTime - startTime | ||
timeTaken = timeTaken & | timeTaken = timeTaken & "; Selection Sort: " & timeToSort | ||
End Sub | End Sub | ||
Line 154: | Line 154: | ||
Do | Do | ||
lHValue = 3 * lHValue + 1 | lHValue = 3 * lHValue + 1 | ||
Loop Until lHValue | Loop Until lHValue > UBound(vArray) | ||
Do | Do | ||
lHValue = lHValue / 3 | lHValue = lHValue / 3 | ||
Line 160: | Line 160: | ||
lTemp = vArray(lLoop1) | lTemp = vArray(lLoop1) | ||
lHold = lLoop1 | lHold = lLoop1 | ||
Do While vArray(lHold - lHValue) | Do While vArray(lHold - lHValue) > lTemp | ||
vArray(lHold) = vArray(lHold - lHValue) | vArray(lHold) = vArray(lHold - lHValue) | ||
lHold = lHold - lHValue | lHold = lHold - lHValue | ||
If lHold | If lHold < lHValue Then Exit Do | ||
Loop | Loop | ||
vArray(lHold) = lTemp | vArray(lHold) = lTemp | ||
Line 170: | Line 170: | ||
endTime = Time() | endTime = Time() | ||
timeToSort = endTime - startTime | timeToSort = endTime - startTime | ||
timeTaken = timeTaken & | timeTaken = timeTaken & "; Shell Sort: " & timeToSort | ||
End Sub | End Sub | ||
</pre></li> | </pre></li> |
Latest revision as of 12:29, 21 July 2020
Article ID: 169617
Article Last Modified on 8/30/2004
APPLIES TO
- Microsoft Visual Basic 4.0 Enterprise Edition
- Microsoft Visual Basic 5.0 Enterprise Edition
- Microsoft Visual Basic 6.0 Enterprise Edition
- Microsoft Visual Basic 4.0 Professional Edition
- Microsoft Visual Basic 5.0 Professional Edition
- Microsoft Visual Basic 6.0 Professional Edition
- Microsoft Visual Basic for Applications 5.0
- Microsoft Visual Basic for Applications 6.0
This article was previously published under Q169617
SUMMARY
This article demonstrates how to use sort algorithms for sorting numeric arrays. It describes three methods of sorting a series of numbers and provides sample code that shows how to implement them with the time taken to sort by each algorithm.
MORE INFORMATION
It is often necessary to sort a series of numbers in code, and there are various sorting algorithms available to do this.
This article includes three methods:
- Bubble sort
- Selection sort
- 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 that is 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
- Start a new Standard EXE project in Visual Basic. Form1 is created by default.
- Place a CommandButton (Command1) onto Form1.
Add the following code in the code window of Form1:
Option Explicit Dim startTime As Double Dim endTime As Double Dim timeToSort As Double Dim timeTaken As String Sub Command1_Click() Dim lMyArray(0 To 2000) 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 Screen.MousePointer = vbHourglass Call BubbleSortNumbers(vTemp1) Call SelectionSortNumbers(vTemp2) Call ShellSortNumbers(vTemp3) Screen.MousePointer = vbDefault MsgBox timeTaken End Sub Sub BubbleSortNumbers(iArray As Variant) Dim lLoop1 As Long Dim lLoop2 As Long Dim lTemp As Long startTime = Time() 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 endTime = Time() timeToSort = endTime - startTime timeTaken = "Bubble Sort: " & timeToSort End Sub Sub SelectionSortNumbers(vArray As Variant) Dim lLoop1 As Long Dim lLoop2 As Long Dim lMin As Long Dim lTemp As Long startTime = Time() 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 endTime = Time() timeToSort = endTime - startTime timeTaken = timeTaken & "; Selection Sort: " & timeToSort End Sub Sub ShellSortNumbers(vArray As Variant) Dim lLoop1 As Long Dim lHold As Long Dim lHValue As Long Dim lTemp As Long startTime = Time() 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) endTime = Time() timeToSort = endTime - startTime timeTaken = timeTaken & "; Shell Sort: " & timeToSort End Sub
- Run the project, and click Command1. You receive the following output or similar:
Bubble Sort: 4.6296E -05; Selection Sort: 3.4722E -05; Shell Sort: 0
NOTE: In most cases, the Shell sort is the fastest of the three sorts presented. To determine which sort is faster, you can also use the QueryPerformanceCounter function to time the application code.
REFERENCES
For additional information on how to use the QueryPerformanceCounter function, click the article number below to view the article in the Microsoft Knowledge Base:
172338 How To Use QueryPerformanceCounter to Time Code
Keywords: kbhowto kbsample KB169617