Using a bubble sort algorithm

The Lotus Domino support site has a bubble sort algorithm posted with no help on how to implement it. And since it takes an array as input, it isn't obvious how to pass in the appropriate value. Here are step-by-step instructions on using the bubble sort script, assuming you have multiple values in one field that you want sorted and placed in a different field.

1. Create a form with two fields on it, INPUT and OUTPUT. Both are editable text (the default). Both allow multiple values (not the default). Values are separated when the user enters a semi-colon (the default).

2. Create a button on the same form called SORT that runs a script.

3. Go to the Declarations event (instead of the Click event) of the Sort button. (This isn't necessary, but it is a good place to paste the code in the step that follows.)

4. Copy the script below beginning with SUB BUB_SORT and ending with the last END SUB and paste that into the Declarations event of the Sort button. This will create a new event for the Sort button (and the code will disappear from the Declarations event).

5. Go back to the Click event of the SORT button, and replace the existing code with the script below that begins with SUB CLICK ending with the first END SUB.

6. Save the form.

7. Using the form in the Notes client, type some values in the INPUT field separated by a semi-colon (such as: b; d; a; c). You don't need to save the document.

8. Click the SORT button. The sorted values should appear in the OUTPUT field.

You could modify the script to put the sorted values back in the INPUT field if desired. Search for "Bubble" on this site to see variation(s) of the bubble sort algorithm.

Sub Click(Source As Button)
     Dim workspace As New NotesUIWorkspace
     Dim uidoc As NotesUIDocument
     Set uidoc = workspace.CurrentDocument
     Dim doc As NotesDocument
     Set doc = uidoc.document
     Dim i As NotesItem
     Set i = doc.GetFirstItem("input")
     Dim myarray As Variant
     myarray = i.values
     Call bub_sort(myarray)
     doc.output = myarray
End Sub

Sub bub_sort (in_array As Variant)
' This code is from the public Lotus site and is assumed to be the intellectual property of IBM Corporation.
     'returns a sorted array based on a complexity of O(n^2) using a bubble sort 
     If Not Isarray(in_array) Then
          Print "bub_sort: not receiving array"
          Exit Sub
     End If
     Dim top, bot, cur, cur2 As Integer
     top=Ubound (in_array)
     bot=Lbound (in_array)
     If top=bot Then
          Print "bub_sort: only one element"
          Exit Sub
     End If
     Dim tmp_element As Variant
     For cur=bot To top
          Do While cur2 > bot 'bubble up
               If (in_array(cur2) > in_array(cur2-1)) Then
                    Exit Do
               End If 
End Sub

This was first published in September 2001

There are Comments. Add yours.

TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
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
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.