Manage Learn to apply best practices and optimize your operations.

Using a bubble sort algorithm

Step-by-step instructions for 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

Dig Deeper on LotusScript



  • Favorite iSeries cheat sheets

    Here you'll find a collection of valuable cheat sheets gathered from across the iSeries/ community. These cheat ...

  • HTML cheat sheet

    This is a really cool cheat sheet if you're looking to learn more about HTML. You'll find just about everything you every wanted ...

  • Carol Woodbury: Security

    Carol Woodbury