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: |
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.
Suppose Arrayelts is the following list of 7 strings:
Arielle, Ray, Paul, Roy, Howard, Tom, Steve
The reduction method finds the position of one of the elements of arrayelts. In this example we use the first element Arielle. Beginning with that string we start at the left of the list scanning right searching for a string greater than Arielle, at the same time we also start at the right of the list going left searching for a string less than Arielle. We come across the string Ray from the left while from the right we also meet the string Ray. Thus Arielle is sorted in the list and we push in our boundaries. Starting at the next element we get the string Ray. We start our search from the left seeking a string greater than Ray while searching from the right for a string less than Ray. We come across the strings Roy and Howard. We swap the strings Roy and Howard and yield the list:
Arielle, Ray, Paul, Howard, Roy, Tom, Steve
Again we start our search from the Howard string searching from the left for a string greater than Ray and from the right at Roy searching for a string less than Ray. From the right we come across the string Howard while from the left we meet the value Roy. Since the string Howard is less than the string Roy and less than Ray we swap Howard and Ray and yield the list;
Arielle, Howard, Paul, Ray, Roy, Tom, Steve
We now start our search with the string Howard, searching from the left for a value greater and from the right for a value less. In our search we come across the string Paul from the left and from the right. Once again our string Howard is sorted and we move to the next element in the list which is Paul. We find that each of the next following elements are sorted until we hit the string Tom. We start our search for Tom and find the value Steve from the right and Tom from the left. We swap out Tom and Steve and yield the list:
Arielle, Howard, Paul, Ray, Roy, Steve, Tom
Finally we hit our last element Steve and the list arrayelts has now been sorted.
'-- 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