Lotus Domino script snippet: Simple Quicksort algorithm
Algorithm can sort in both ascending and descending order.
Main function that can be called with an array any type that has a natural sort order. If boolean argument is true the array will be sorted in ascending order, otherwise it will be sorted in descending order.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
Private Function QuickSort(pvArray As Variant, pbAsc As Boolean) As Variant Dim lvNewArray() As Variant Dim llX As Long, llLow As Long, llHigh As Long ReDim lvNewArray(UBound(pvArray)) llLow = 0 llHigh = UBound(pvArray) For llX = 0 To UBound(pvArray) lvNewArray(llX) = pvArray(llX) Next qsort lvNewArray, llLow, llHigh, pbAsc QuickSort = lvNewArray End Function |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
Private Sub qSort(pvArray As Variant, llLow As Long, llHigh As Long, pbAsc As Boolean) Dim lvPivot As Variant Dim llBottom As Long, llX As Long If llLow = llHigh Then Exit Sub End If lvPivot = pvArray(llHigh) llBottom = llLow - 1 For llX = llLow To llHigh -1 If Not compareValues(lvPivot, pvArray(llX), pbAsc) Then llBottom = llBottom + 1 swapValues pvArray(llX), pvArray(llBottom) End If Next llBottom = llBottom + 1 swapValues pvArray(llHigh), pvArray(llBottom) If llBottom > llLow Then qsort pvArray, llLow, llBottom - 1, pbAsc End If If llBottom + 1 < llHigh Then qsort pvArray, llBottom + 1, llHigh, pbASc End If End Sub |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
Private Function compareValues(pvValue1, pvValue2, pbAsc) As Boolean If pbAsc Then If pvValue1 < pvValue2 Then compareValues = True End If Else If pvValue1 > pvValue2 Then compareValues = True End If End If End Function |