|
Post by meanoscar47 on Nov 4, 2021 10:46:33 GMT -5
I got the below sort routine here years ago and it works great for sorting runner information...when all the Bib Numbers are unique. I set up a test file to read in and sort 1500 bib numbers many of which are NOT unique. It executes but excludes lower numbers generally. My simple swapping code takes 25 seconds to execute and this one (doing it wrong however) is in the blink of an eye. Will several dupes fool it? Is there something inbetween simple swapping and this that will work? call QSort 1, index
goto [aftersort]
sub QSort Start, Finish
i = Start
j = Finish
x = Bib(int((i+j)/2))
while i <= j
while Bib(i) < x
i = i + 1
wend
while Bib(j) > x
j = j - 1
wend
if i <= j then
a = Bib(i) : b$ = NeTime$(i) : c = Place(i)
Bib(j) = Bib(i) : NeTime$(j) = NeTime$(i) : Place(j) = Place(i)
Bib(j) = a : NeTime$(j) = b$ : Place(j) = c
i = i + 1
j = j - 1
end if
wend
if j > Start then call QSort Start, j
if i < Finish then call QSort i, Finish
end sub
[aftersort]
|
|
|
Post by Brandon Parker on Nov 4, 2021 16:33:10 GMT -5
Here is a short example of Quick Sort implemented in LB.
'Use a dedicated array for sorting Dim QSortDataArray(99)
'Let's put some pseudo-random numbers into the array For i = 0 To 99 QSortDataArray(i) = Int(rnd(1) * 500) + 1 Next i
'Call the quickSort subroutine to sort the 'QSortDataArray() array Call quickSort 0, 99
'Print the QSortDataArray() array out to show 'that we have sorted it For i = 0 To 99 Print QSortDataArray(i) Next i
Wait
Sub quickSort minElement, maxElement If (minElement < maxElement) Then 'pivotIndex is partitioning index, QSortDataArray(pivotIndex 'is now at correct place in sorted array pivotIndex = partition(minElement, maxElement)
Call quickSort minElement, (pivotIndex - 1) 'Before pivot_index Call quickSort (pivotIndex + 1), maxElement 'After pivot_index End If End Sub
Sub swap elementA, elementB temp = QSortDataArray(elementA) QSortDataArray(elementA) = QSortDataArray(elementB) QSortDataArray(elementB) = temp End Sub
Function partition(minElement, maxElement) 'Selecting the last element as the pivot point pivotElement = QSortDataArray(maxElement) 'Index of the smaller element partition = (minElement - 1)
For i = minElement To (maxElement - 1) 'If the current element is smaller than or equal to pivot If (QSortDataArray(i) <= pivotElement) Then 'Increment the index of the smaller element partition = (partition + 1) 'Do some swapping Call swap partition, i End If Next i 'Do some swapping Call swap (partition + 1), maxElement partition = (partition + 1) End Function
{:0)
Brandon Parker
|
|
|
Post by meanoscar47 on Nov 5, 2021 6:26:04 GMT -5
IN the second line of swapping, I had Bib(j) = Bib(i). Reversed, it works fine.
|
|
|
Post by Rod on Nov 5, 2021 11:27:19 GMT -5
Coding your own is fun but Liberty has a built in Sort command which is much faster than a coded sort.
|
|