Domino Code Fragment

Code Name*
AlphaQuickSort Function
Date*
04/29/2024
Source (or email address if you prefer)*
Rlatulippe@romac.com
IP address:.18.118.138.223
Description*
Sorts an array of alphanumeric elements. The function returns an array of the elements sorted alphanumerically.
Type*
LotusScript
Categories*
(Misc)
Implementation:
Required Client:
Server:
Limitations:
Comments:
Files/Graphics attachments (if applicable): Code:
AlphaQuickSort Function

Function AlphaQuickSort (nFirst As Integer, nLast As Integer, arrayelts As Variant) As Variant

Sorts an array of alphanumeric elements. The function returns an array of the elements sorted alphanumerically.

Function Syntax

AlphaQuickSort( nFirst , nLast , arrayelts )

Formal Parameters

nFirst

Must be an integer variable of the Lower Bound of the Array to be sorted.

nLast

Must be an integer variable of the Upper Bound of the Array to be sorted.

arrayelts

Pass the Array that will be sorted.

Return value

The AlphaQuickSort function's return value is an array. This array will contain the elements of arrayelts sorted alphanumerically. You must assign the return to a variant variable.

Theory

Quicksort is a "divide and conquer" method of sorting. It works by partitioning a set (the array) into two parts, then sorts the parts independently. In our AlphaQuickSort function the formal parameters nFirst and nLast delimit the subpart within the original list (array) that is to be sorted. This arrangement uses the partition method which must arrange the array to make the following three conditions hold.

  1. The element a[i] is in its final place for some i
  2. All elements in a[nFirst],...,a[i - 1] are less than or equal to a[i]
  3. All elements in a[i + 1],...,a[nLast] are greater than or equal to a[i].

Function

'-- nFirst is the LowerBound of arrayelts, nLast is the Upper Bound of arrayelts, arrayelts is the
'-- alphanumeric array to be sorted. Return value is arrayelts sorted alphanumerically.
Function
AlphaQuickSort(nFirst As Integer, nLast As Integer, arrayelts As Variant) As Variant
Dim m As Variant
Dim i As Integer '-- Stores the Lower bound for tracking
Dim t As Variant '-- Used to store temp value
Dim j As Integer '-- Stores the Upper bound for tracking
If nLast > nFirst Then '-- If there's nothing to sort, jump out
m = arrayelts(nFirst) '-- Initialize boundaries, nominate a value to sort
j = nLast
i = nFirst

'-- Repeat until i and j "meet in the middle"
While (i < j)

'-- Push in the boundaries while data is sorted
While (arrayelts(i) <= m And i < nLast)
i = i + 1
Wend
While (arrayelts(j) > m)
j = j - 1
Wend

'-- If there is data between i and j something is out of order - swap it
If i < j Then

t = arrayelts(i)
arrayelts(i) = arrayelts(j)
arrayelts(j) = t
End If
Wend

t = arrayelts(nFirst)
'-- Swap the nominated and bottom values - why we came here
arrayelts(nFirst) = arrayelts(j)
arrayelts(j) = t

'-- Recurse and sort data either side of upper bound
Call AlphaQuicksort (nFirst, j - 1, arrayelts)
Call AlphaQuicksort ((j + 1), nLast, arrayelts)
AlphaQuicksort = arrayelts
End If
End Function

Usage

The valid values for the arayelts elements are alphanumeric characters. This will not sort an array of strictly numeric elements. Assign the return to a variant variable.

Example

This example asks the user for input of names and assigns each name to an element in the array. On NULL input it calls the AlphaQuickSort function which sorts the elements in the array. The return is assigned to a variable of type variant. The sorted elements are then formatted for display in Messagebox.

Sub Click(Source As Button)
Dim names() As String
Dim sorted As Variant
Dim into As String
Dim I As Integer
Dim out As String
Redim names (0)
I = 0

into = Inputbox$("Enter a name, or nothing to quit.","Enter names here!")
While into <> ""
Redim Preserve names(0 To I)
names(I) = into
I = I + 1
into = Inputbox$("Enter a name, or nothing to quit.","Enter names

here!")
Wend

sorted = AlphaQuickSort(Lbound(names), Ubound(names), names)

If Isarray(sorted) = True Then
out = Cstr(sorted(Lbound(sorted)))
For i = Lbound(sorted) + 1 To Ubound(sorted)
out = out & ", " & Cstr(sorted(i))
Next i
Messagebox out ,
MB_ICONINFORMATION, "The list of sorted names are"
Exit Sub
End If
Messagebox out ,
MB_ICONINFORMATION, "The list of sorted names are"
End Sub