Manage Learn to apply best practices and optimize your operations.

Recursive Quick Sort Script

Recursive Quick Sort script
Sub quicksort (lngLbound As Long,lngUbound As Long, varSortArray As Variant)
'Pass the lower bound of the array to lngLbound, the upper bound of the array
to lngUbound and the array to varSortArray.
Dim varValue1 As Variant
Dim varValue2 As Variant
Dim lngTmpLbound As Long
Dim lngTmpUbound As Long

If lngUbound > lngLbound Then 'If there's nothing to sort, jump out
varValue1 = varSortArray(lngLbound) 'Initialize boundaries, nominate
a value to sort
lngTmpUbound = lngUbound
lngTmpLBound = lngLbound

While (lngTmpLBound < lngTmpUbound) 'Repeat until lngTmpLBound and
lngTmpUbound "meet in the middle"
While (varSortArray(lngTmpLBound) <= varValue1 And lngTmpLBound
< lngUbound) 'Push in the boundaries while data is sorted
lngTmpLBound = lngTmpLBound + 1
While (varSortArray(lngTmpUbound) > varValue1)
lngTmpUbound = lngTmpUbound - 1
If lngTmpLBound < lngTmpUbound Then 'If there is data between
lngTmpLBound and lngTmpUbound something is out of order - swap it
varValue2 = varSortArray(lngTmpLBound)
varSortArray(lngTmpLBound) = varSortArray(lngTmpUbound)
varSortArray(lngTmpUbound) = varValue2
End If

varValue2 = varSortArray(lngLbound) 'Swap the nominated and bottom
values - why we came here
varSortArray(lngLbound) = varSortArray(lngTmpUbound)
varSortArray(lngTmpUbound) = varValue2
Call quicksort (lngLbound, lngTmpUbound - 1, varSortArray) 'Recurse
and sort data either side of upper bound
Call quicksort ((lngTmpUbound + 1), lngUbound, varSortArray)
End If
End Sub
This was last published in November 2000

Dig Deeper on Domino Resources - Part 5

Start the conversation

Send me notifications when other members comment.

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Please create a username to comment.




  • iSeries tutorials's tutorials provide in-depth information on the iSeries. Our iSeries tutorials address areas you need to know about...

  • V6R1 upgrade planning checklist

    When upgrading to V6R1, make sure your software will be supported, your programs will function and the correct PTFs have been ...

  • Connecting multiple iSeries systems through DDM

    Working with databases over multiple iSeries systems can be simple when remotely connecting logical partitions with distributed ...