Autor Thema: LotusScript und Rekursion  (Gelesen 2318 mal)

Offline stitze

  • Aktives Mitglied
  • ***
  • Beiträge: 226
  • Geschlecht: Männlich
    • kleisterbande
LotusScript und Rekursion
« am: 24.11.05 - 08:10:42 »
Moin, ich wollte gerade einen Quicksort in LS programmieren um einen Array zu sortieren.

Leider bekomme ich beim rekursiven Aufruf der Methode Quicksort eine Fehlermeldung

"Illegal use of parentheses"

Code
Sub QuickSort( array() As String, LeftBound As Integer, RightBound As Integer )
	
	Dim intLeft As Integer
	Dim intRight As Integer
	Dim intPos As Integer
	
	Dim strTemp As String
	
	Dim strPivot As String
	
	intLeft = LeftBound
	intRight = RightBound
	
	intPos = ( intRight + intLeft ) / 2
	
	strPivot = array( intPos )
	
	Do While intLeft <= intRight
		Do While array( intLeft ) < strPivot 
			intLeft = intLeft + 1
		Loop
		
		Do While array( intRight ) > strPivot
			intRight = intRight - 1
		Loop
		
		If intLeft <= intRight Then
			strTmp = array( intLeft )
			array( intLeft ) = array( intRight )
			array( intRight ) = strTmp
			
			intLeft = intLeft + 1	
			intRight = intRight - 1	
		End If
	Loop
	
	If LeftBound < intRight Then
		QuickSort( array, LeftBound, intRight )
	End If
	
	If intLeft < RightBound Then
		QuickSort( array, intLeft, RightBound )
	End If
End Sub

Kann LS keine Rekursion?

Gruß

Sebastian
« Letzte Änderung: 24.11.05 - 08:12:29 von stitze »
When i loaded my cap gun , i'm ready for action.

Saying that Java is nice because it works on all OS's is like saying anal sex is nice because it works on all genders.

klaussal

  • Gast
Re: LotusScript und Rekursion
« Antwort #1 am: 24.11.05 - 08:12:16 »
Hab ich bei mir gefunden:

Code
Quicksort for a document collection

Since getting having an unsorted DocumentCollection is generally a pain, I've 
written some code that's pretty quick in sorting these documents on the fly. 
This uses a QuickSort algorithm, instead of the much slower bubble sorts that 
seem to be floating out here...

I actually borrowed the CollectionToArray code from this board a while back, 
but it also used a bubble sort routine that was way too slow.

This code should be able to be used as is. I've used this method in several 
DB's with no changes since the initial write. The only thing you should have 
to change is the variable which tells it which field to sort on (explained 
below)...

Sub CollectionToArray ( dc As NotesDocumentCollection) 

Dim doc As NotesDocument
Dim k As Long
k = dc.Count
If k<>0 Then
Redim da( 1 To k+1) As NotesDocument
Set doc = dc.GetFirstDocument
For i=1 To k
Set da(i) = doc
Set doc = dc.GetNextDocument(doc)
Next
End If

'Need to add a value at the end that will always be greater than all 
other values
Set da(k+1) = dirDB.CreateDocument
da(k+1).Lastname = "ZZZZZZZZ"

End Sub

Function swap(i As Integer,j As Integer) As Variant
Dim temp As NotesDocument

Set temp = da(i)
Set da(i) = da(j)
Set da(j) = temp

End Function

Sub QuickSort (leftpos As Integer, rightpos As Integer)
Dim i As Integer
Dim j As Integer
Dim pivot As String
If (leftpos < rightpos) Then
i=leftpos
j=rightpos + 1
pivot = 
da(leftpos).GetFirstItem(key).Text 
Do
Do 
i=i+1
Loop While da(i).GetFirstItem(key).Text<pivot And 
i<=rightpos
Do
j=j-1 
Loop While da(j).GetFirstItem(key).Text > pivot And 
j>=leftpos
If (i<j) Then
Call swap(i, j)
End If
Loop While (i<j)
Call swap(leftpos,j)
Call QuickSort(leftpos,j-1)
Call QuickSort(j+1,rightpos)
End If

End Sub

These need to be public variables, so place them in your "Declarations":

Dim s As NotesSession
Dim db As NotesDatabase 'The current database
Dim da As Variant
Const key = "LastName" 'This the name of the FIELD you want to sort on

Now, how to use this stuff...

In your Initialize, or wherever you're calling this from:
1) Get your DocumentCollection (here: dc) 
2) Call CollectionToArray(dc) 'just creates an array of your documents so that 
we can re-order them
3) Call QuickSort(Lbound(da), Ubound(da)-1) 'actually sorts your documents
4) Redim Preserve da( 1 To Ubound(da)-1) As NotesDocument 'just chops off the 
'ZZZZZ' value we needed as a 'high' value (see CollectionToArray routine)
5) Forall doc In da ... End ForAll 'Use this to loop through your sorted 
documents

Remember, the DocumentArray (da) is just an array of documents, so you can 
reference the 'doc' in your ForAll and any of it's properties, fields, etc. 
just like any other document.

Let me know if you have any problems, or just to let me know you found this 
useful...

Steve Held
Paxion Corporation

Anpassung:
Replace the
For i=1 To k
Set da(i) = dc.GetNthDocument(i)
Next

with the appropriate GetNextDocument() code instead for some addtl. speed.
GetNthDocument is much slower than GetNextDocument....

Offline stitze

  • Aktives Mitglied
  • ***
  • Beiträge: 226
  • Geschlecht: Männlich
    • kleisterbande
Re: LotusScript und Rekursion
« Antwort #2 am: 24.11.05 - 08:13:22 »
Thx
When i loaded my cap gun , i'm ready for action.

Saying that Java is nice because it works on all OS's is like saying anal sex is nice because it works on all genders.

Offline Tode

  • Moderatoren
  • Gold Platin u.s.w. member:)
  • *****
  • Beiträge: 6.883
  • Geschlecht: Männlich
  • Geht nicht, gibt's (fast) nicht... *g*
Re: LotusScript und Rekursion
« Antwort #3 am: 24.11.05 - 17:11:21 »
das hier sollte eigentlich schon reichen...

Sub QuickSort( array() As String, LeftBound As Integer, RightBound As Integer )

wird zu

Sub QuickSort( array As Variant, LeftBound As Integer, RightBound As Integer )

natürlich kann Notes rekursion.

Tode
Gruss
Torsten (Tode)

P.S.: Da mein Nickname immer mal wieder für Verwirrung sorgt: Tode hat NICHTS mit Tod zu tun. So klingt es einfach, wenn ein 2- Jähriger versucht "Torsten" zu sagen... das klingt dann so: "Tooode" (langes O, das r, s und n werden verschluckt, das t wird zum badischen d)

 

Impressum Atnotes.de  -  Powered by Syslords Solutions  -  Datenschutz