_Title "Recursive Vrs NonRecursive test with Quicksort" 'b+ 2021-11-01, Sanmayce added some stuff 2021-Nov-04 ArrayHigh = 10000000
n(i) = i '13 'i
For i
= ArrayHigh
To 2 Step -1 ' Fisher Yates Shuffle m(i) = n(i) ' copy m off n
z(i) = n(i) ' copy m off n
Print "Test arrays ready. Testing Recursive QuickSort first." n(1) = ArrayHigh
m(1) = n(1)
z(1) = n(1)
QuickSort 1, ArrayHigh
QuickSortT#
= Timer(.001) - T#
For i
= 1 To ArrayHigh
- 1 Print "QuickSort checked, QuickSort time was"; QuickSortT#
Quicksort_QB64
QuicksortQB64T#
= Timer(.001) - T#
For i
= 1 To ArrayHigh
- 1 Print "Quicksort_QB64 checked, Quicksort_QB64 time was"; QuicksortQB64T#
Quicksort_QB64_v2
QuicksortQB64T#
= Timer(.001) - T#
For i
= 1 To ArrayHigh
- 1 Print "Quicksort_QB64_v2 checked, Quicksort_QB64_v2 time was"; QuicksortQB64T#
' 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
StackPtr = 0
StackPtr = StackPtr + 1
Stack&&(StackPtr + LeftMargin) = Left
StackPtr = StackPtr + 1
Stack&&(StackPtr + LeftMargin) = Right
Right = Stack&&(StackPtr)
Left = Stack&&(StackPtr - 1)
StackPtr = StackPtr - 2
' 'Classica' partitioning [
'Pivot~&& = z((Left + Right) \ 2)
'Indx = Left
'Jndx = Right
'Do
' Do While (z(Indx) < Pivot~&&)
' Indx = Indx + 1
' Loop
' Do While (z(Jndx) > Pivot~&&)
' Jndx = Jndx - 1
' Loop
' If Indx <= Jndx Then
' If Indx < Jndx Then Swap z(Indx), z(Jndx)
' Indx = Indx + 1
' Jndx = Jndx - 1
' End If
'Loop While Indx <= Jndx
' 'Classica' partitioning ]
' 'Magnetica' partitioning [
Jndx = Right
PL = Left
PR = Left
Swap z
((Left
+ Right
) \
2), z
(PL
) PL = PL + 1
PR = PR + 1
PR = PR + 1
Jndx = Jndx - 1
Jndx = PL - 1
Indx = PR + 1
' 'Magnetica' partitioning ]
StackPtr = StackPtr + 2
Stack&&(StackPtr - 1) = Indx
Stack&&(StackPtr) = Right
Right = Jndx