Post by Sver on Aug 30, 2019 5:03:45 GMT -5
Sort is unstable when your using a multi dimensioned array.
When there are the same values, it will change the ranking.
For a stable sort with nummeric values in a colomn, you can use this,
Test:
When there are the same values, it will change the ranking.
For a stable sort with nummeric values in a colomn, you can use this,
dim item(20,3) 'array to sort
'-------------parameters sort---------------------------
first= 1 'start
last = 20 'end
colomn=0 'column to sort
totalcolomn=3 'how many colomns
'---------------------------------------------------------------------
counter = last
do
hasChanged = 0
for i=1 to counter - 1
if item(i,colomn) > item(i + 1,colomn) then
for firstcolumn=0 to totalcolomn
temp = item(i,firstcolumn)
item(i,firstcolumn) = item(i + 1,firstcolumn)
item(i + 1,firstcolumn) = temp
next firstcolumn
hasChanged = 1
end if
next i
counter =counter -1
loop while hasChanged = 1
Test:
dim item(20,3) 'array to sort
'----------------------------------fill column 0 --------------------
for i = 1 to 20
item(i,0) = int(rnd(1) * 50)
'---------------------------------fill column 1 -----------------------
a=a+1
item(i,1) =a
next
'--------------------------------fill column 2 ----------------------
for i = 1 to 20
item(i,2) = int(rnd(1) * 50)
next
'-----------------------parameters sort
first= 1 'start
last = 20 'end
colomn=0 'column to sort
totalcolomn=3 'how many colomns
'---------------------------------------------------------------------------
[start]
if c=3 then end
c=c+1
'--------------------------------show----------------------------
print "Sort"
for i = 1 to 20
print item(i,0);
print " ";
print item(i,1);
print " ";
print item(i,2)
next i
print " "
'-----------------------------------real code------sort multi colomn numberic array---------------------------
counter = last
do
hasChanged = 0
for i=1 to counter - 1
if item(i,colomn) > item(i + 1,colomn) then
for firstcolumn=0 to totalcolomn
temp = item(i,firstcolumn)
item(i,firstcolumn) = item(i + 1,firstcolumn)
item(i + 1,firstcolumn) = temp
next firstcolumn
hasChanged = 1
end if
next i
counter =counter -1
loop while hasChanged = 1
'-----------------------------------------------------------------------------------------------------------------------------------
goto [start] 'sort again