Deshalb ja auch in dieser Rubrik. Die spinnen offenbar, die bei Google!
Zu oben: Kleiner Fehler ausgebessert: Randomwert auch für das 0-Element (Arrays beginnen bei Null).
Nun kann man auch Laufzeiten unterschiedlicher Sorts vergleichen, zum Beispiel wenn man 500000 8-Byte-Floatwerte sortiert: 184,31 Sekunden...
WindowTitle "NONREKURSIVES FLOATWERTE-QUICKSORT"
Declare NumMax&,A![],Stack1&[],Stack2&[],StackPtr&,HeadPtr&
Declare TailPtr&,Pivot!,a&,b&,t!,q&,r&,p&,s&,i&,ms1&,sec!: J0:
Cls:Print "Wieviele Random Numbers? ";:Input NumMax&
Randomize:i&=0:While i&<NumMax&:A![i&]=Rnd()*100000:Inc i&:EndWhile
ms1& = &GetTickCount:StackPtr&=0:HeadPtr&=0:TailPtr&=NumMax&-1
Print "Starte Nonrekursives Quicksort...";: J2:
While HeadPtr& < TailPtr&:Pivot!=A![(HeadPtr&+TailPtr&)/2]
a&=HeadPtr&:b&=TailPtr&: J1:
While A![a&]<Pivot!:inc a&:EndWhile
While A![b&]>Pivot!:dec b&:EndWhile
If a&<b&:t!=A![a&]:A![a&]=A![b&]:A![b&]=t!:inc a&:dec b&:Goto "J1"
EndIf :If a&=b&:q&=b&-1:r&=a&+1: Else :q&=b&:r&=a&:EndIf
inc StackPtr&:p&=HeadPtr&:s&=TailPtr&
If (q&-p&)<(s&-r&):Stack1&[StackPtr&]=r&:Stack2&[StackPtr&]=s&
HeadPtr&=p&:TailPtr&=q&:Else :Stack1&[StackPtr&]=p&
Stack2&[StackPtr&]=q&:HeadPtr&=r&:TailPtr&=s&:EndIf :EndWhile
If StackPtr&>0:HeadPtr&=Stack1&[StackPtr&]:TailPtr&=Stack2&[StackPtr&]
dec StackPtr&:Goto "J2":EndIf :sec!=(&GetTickCount-ms1&)/1000:print
Print "Kontrollausgabe (jedes " + str$(int(NumMax&/30+1))+". Element):"
WhileLoop 0,NumMax&-1,int(1+NumMax&/30):Print A![&Loop];:EndWhile :print
print:print "Sort-Dauer für "+str$(NumMax&)+" Variablen: "+\
str$(sec!)+" Sek.":print:print "Taste...":WaitInput :Goto "J0"
Alles anzeigen
und das gleiche (185,66 Sekunden) für 8-Byte Strings:
WindowTitle "NONREKURSIVES STRING-QUICKSORT"
Declare NumMax&,A$[],Stack1&[],Stack2&[],StackPtr&,HeadPtr&
Declare TailPtr&,Pivot$,a&,b&,t$,q&,r&,p&,s&,i&,ms1&,sec!,m&: J0:
Cls:Print "Wieviele Random 8-Zeichen-Strings? ";:Input NumMax&
Randomize:i&=0:While i&<NumMax&
A$[i&]=chr$(65+int(@rnd(26)))+chr$(65+int(@rnd(26)))+chr$(65+int(@rnd(26)))+\
chr$(65+int(@rnd(26)))+chr$(65+int(@rnd(26)))+chr$(65+int(@rnd(26)))+\
chr$(65+int(@rnd(26)))+chr$(65+int(@rnd(26)))
'print a$[i&],
Inc i&:EndWhile :ms1&=&GetTickCount:StackPtr&=0:HeadPtr&=0
TailPtr&=NumMax&-1:print :print "Starte Nonrekursives Quicksort...";: J2:
While HeadPtr&<TailPtr& : Pivot$=A$[(HeadPtr&+TailPtr&)/2]
a&=HeadPtr& : b&=TailPtr&: J1:
While A$[a&]<Pivot$:inc a&:EndWhile
While A$[b&]>Pivot$:dec b&:EndWhile
If a&<b& : t$=A$[a&] : A$[a&]=A$[b&] : A$[b&]=t$: inc a&: dec b&: Goto "J1"
EndIf :If a&=b&:q&=b&-1:r&=a&+1: Else :q&=b&:r&=a&:EndIf
inc StackPtr&:p&=HeadPtr&:s&=TailPtr&
If (q&-p&)<(s&-r&):Stack1&[StackPtr&]=r&:Stack2&[StackPtr&]=s&
HeadPtr&=p&:TailPtr&=q&:Else :Stack1&[StackPtr&]=p&
Stack2&[StackPtr&]=q&:HeadPtr&=r&:TailPtr&=s&:EndIf :EndWhile
If StackPtr&>0:HeadPtr&=Stack1&[StackPtr&]:TailPtr&=Stack2&[StackPtr&]
dec StackPtr&:Goto "J2":EndIf :sec!=(&GetTickCount-ms1&)/1000:print
Print "Kontrollausgabe (jedes " + str$(int(NumMax&/30+1))+". Element):"
WhileLoop 0,NumMax&-1,int(1+NumMax&/30):Print A$[&Loop],:EndWhile :print
print:print "Sort-Dauer für "+str$(NumMax&)+" $-Variablen: "+\
str$(sec!)+" Sek.":print:print "Taste...":WaitInput :Goto "J0"
Alles anzeigen
...also alles erstaunlich konstant!
P.S.: Zweiten kleinen Fehler entdeckt und in allen 3 Codes behoben...