_Title "Recursive Vrs NonRecursive #2 test with Quicksort" 'b+ 2021-11-01 '2021-11-01 #2 test try a recursive sub wo parameters
ArrayHigh = 10000000
n(i) = i
For i
= ArrayHigh
To 2 Step -1 ' Fisher Yates Shuffle m(i) = n(i) ' copy m off n
o(i) = n(i)
m(1) = n(1)
o(1) = n(1)
Print "Test arrays ready. Testing Recursive QuickSort first."
QuickSort 1, ArrayHigh
QuickSortT#
= Timer(.001) - T#
Print "QuickSort checked, QuickSort time was"; QuickSortT#
Quicksort_QB64
QuicksortQB64T#
= Timer(.001) - T#
Print "Quicksort_QB64 checked, Quicksort_QB64 time was"; QuicksortQB64T#
StackPt = StackPt + 1
Stack(StackPt) = 1
StackPt = StackPt + 1
Stack(StackPt) = ArrayHigh
QSort
QSortT#
= Timer(.001) - T#
Print "QSort checked, QSort time was"; QSortT#
Print "Number of calls to QSort"; recurrence
' recursive
Lo = start: Hi = finish
Middle = n((Lo + Hi) \ 2)
Lo = Lo + 1: Hi = Hi - 1
If Hi
> start
Then QuickSort start
, Hi
If Lo
< finish
Then QuickSort Lo
, finish
' non recursive
' ref: https://rosettacode.org/wiki/Sorting_algorithms/Quicksort#QB64
LeftMargin = Left
StackPtr = 0
StackPtr = StackPtr + 1
Stack&&(StackPtr + LeftMargin) = Left
StackPtr = StackPtr + 1
Stack&&(StackPtr + LeftMargin) = Right
Right = Stack&&(StackPtr + LeftMargin)
StackPtr = StackPtr - 1
Left = Stack&&(StackPtr + LeftMargin)
StackPtr = StackPtr - 1
Pivot~&& = m((Left + Right) \ 2)
Indx = Left
Jndx = Right
Indx = Indx + 1
Jndx = Jndx - 1
Indx = Indx + 1
Jndx = Jndx - 1
StackPtr = StackPtr + 1
Stack&&(StackPtr + LeftMargin) = Indx
StackPtr = StackPtr + 1
Stack&&(StackPtr + LeftMargin) = Right
Right = Jndx
' recursive No Parameters!!!
recurrence = recurrence + 1
If StackPt
> 0 Then 'else done! finish = Stack(StackPt) 'pull off last 2 arguments
StackPt = StackPt - 1
start = Stack(StackPt)
StackPt = StackPt - 1
Lo = start: Hi = finish
Middle = o((Lo + Hi) \ 2)
Lo = Lo + 1: Hi = Hi - 1
StackPt = StackPt + 1
Stack(StackPt) = start
StackPt = StackPt + 1
Stack(StackPt) = Hi
QSort
StackPt = StackPt + 1
Stack(StackPt) = Lo
StackPt = StackPt + 1
Stack(StackPt) = finish
QSort