You are not logged in. You can browse in the PowerBASIC Community, but you must click Login (top right) before you can post. If this is your first visit, check out the FAQ or Sign Up.
Is there some reason you need to write your own QuickSort
routine? PB's Array Sort is just about the fastest sort
you can get on the market and it works with both numeric and
string arrays.
------------------
There are no atheists in a fox hole or the morning of a math test.
If my flag offends you, I'll help you pack.
---------Example---------
TYPE sortar
Length AS integer
data as byte
END TYPE
DIM SortArray(1 TO 1000) AS shared sortar
Defint A-Z
QuickSort 1,1000
End
SUB QuickSort (low as integer, high as integer)
IF low < high THEN
IF high - low = 1 THEN
IF SortArray(low).Length > SortArray(high).Length THEN
SWAP SortArray(low), SortArray(high)
END IF
ELSE
' Pick a pivot element at random, then move it to the end:
RandIndex = RandInt(low+0, high+0)
SWAP SortArray(high), SortArray(RandIndex)
Partition = SortArray(high).Length
DO
' Move in from both sides towards the pivot element:
I = low: J = high
DO WHILE (I < J) AND (SortArray(I).Length <= Partition)
I = I + 1
LOOP
DO WHILE (J > I) AND (SortArray(J).Length >= Partition)
J = J - 1
LOOP
' If we haven't reached the pivot element, it means that two
' elements on either side are out of order, so swap them:
IF I < J THEN
SWAP SortArray(I), SortArray(J)
END IF
LOOP WHILE I < J
' Move the pivot element back to its proper place in the array:
SWAP SortArray(I), SortArray(high)
' Recursively call the QuickSort procedure (pass the smaller
' subdivision first to use less stack space):
IF (I - low) < (high - I) THEN
QuickSort low, I - 1
QuickSort I + 1, high
ELSE
QuickSort I + 1, high
QuickSort low, I - 1
END IF
END IF
END IF
END SUB
sub qsort(dat() as long, l as long, r as long)
local i as long
local j as long
local m as long
i = l
j = r
m = dat((l+r)\2)
while (i<=j)
while dat(i)<m: incr i: wend
while dat(j)>m: decr j: wend
if (i<=j) then
swap dat(i),dat(j)
incr i
decr j
end if
wend
if (l<j) then qsort dat(),l,j
if (i<r) then qsort dat(),i,r
end sub
called: qsort array(),lbound(array),ubound(array)
We process personal data about users of our site, through the use of cookies and other technologies, to deliver our services, and to analyze site activity. For additional details, refer to our Privacy Policy.
By clicking "I AGREE" below, you agree to our Privacy Policy and our personal data processing and cookie practices as described therein. You also acknowledge that this forum may be hosted outside your country and you consent to the collection, storage, and processing of your data in the country where this forum is hosted.
Comment