Sort stability: Difference between revisions

Liberty BASIC entry
(Liberty BASIC entry)
Line 407:
UK - London
US - New York</pre>
 
=={{header|Liberty BASIC}}==
LB has build-in SORT routine.
Documentation does not says if it's stable or not.
Example from RC keeps order.
 
Here's an example showing that SORT indeed unstable.
<lang lb>
randomize 0.5
N=15
dim a(N,2)
 
for i = 0 to N-1
a(i,1)= int(i/5)
a(i,2)= int(rnd(1)*5)
next
 
print "Unsorted by column #2"
print "by construction sorted by column #1"
for i = 0 to N-1
print a(i,1), a(i,2)
next
 
sort a(), 0, N-1, 2
print
 
print "After sorting by column #2"
print "Notice wrong order by column #1"
for i = 0 to N-1
print a(i,1), a(i,2),
if i=0 then
print
else
if a(i,2) = a(i-1,2) AND a(i,1) < a(i-1,1) then print "bad order" else print
end if
next
</lang>
{{Out}}
<pre>
Unsorted by column #2
by construction sorted by column #1
0 4
0 1
0 1
0 0
0 4
1 1
1 1
1 2
1 1
1 0
2 4
2 3
2 2
2 0
2 4
 
After sorting by column #2
Notice wrong order by column #1
0 0
1 0
2 0
1 1
1 1
1 1
0 1 bad order
0 1
1 2
2 2
2 3
2 4
0 4 bad order
0 4
2 4
</pre>
 
=={{header|Lua}}==
Anonymous user