Below is an implementation of a quicksort algorithm in LotusScript. Quicksort is a popular sorting algorithm known for its efficiency and simplicity. The basic idea behind quicksort is to divide an array into two sub-arrays and sort these arrays. Subdividing the array to the point that it can’t be separated anymore will allow it a quick sorting process.
Here is the LotusScript code for quicksort I created from one of my old college programming books. I tried hard to provide all the variables with meaningful names so the code would be easier to understand.
Sub QuickSort(LowerBound As Integer, UpperBound As Integer, ArrayToSort As Variant)
Dim PivotValue As Variant
Dim LeftIndex As Integer
Dim RightIndex As Integer
If UpperBound > LowerBound Then 'Nothing to sort, exit subroutine
PivotValue = ArrayToSort(LowerBound) 'Starting point
RightIndex = UpperBound
LeftIndex = LowerBound
'Repeat until LeftIndex and RightIndex "meet in the middle"
While (LeftIndex < RightIndex)
'Reduce boundaries while data is sorted
While (ArrayToSort(LeftIndex) <= PivotValue And LeftIndex < UpperBound)
LeftIndex = LeftIndex + 1
Wend
While (ArrayToSort(RightIndex) > PivotValue)
RightIndex = RightIndex - 1
Wend
'Is data between LeftIndex and RightIndex out of order, swap it
If LeftIndex < RightIndex Then
Call SwapValues(ArrayToSort(LeftIndex), ArrayToSort(RightIndex))
End If
Wend
Call SwapValues(ArrayToSort(LowerBound), ArrayToSort(RightIndex))
'Recursive call to sort data
Call QuickSort(LowerBound, RightIndex - 1, ArrayToSort)
'Recursive call to sort data
Call QuickSort((RightIndex + 1), UpperBound, ArrayToSort)
End If
End Sub
If you would like to test the quicksort code, you may use the following in a LotusScript agent or action.
Dim myArray As Variant
Dim teststr As String
Dim delim As String
teststr = Jan,Feb,Mar,Apr,May,June,July,Aug,Sept,Oct,Nov,Dec"
delim = ","
myArray = Split(teststr, delim)
Call QuickSort(myArray, 0, UBound(myArray))
Dim x As integer
For x = 0 To UBound(myArray)
MsgBox myarray(x)
Next
I would like to hear any recommendations to improve the quicksort code.