Sort a list of object identifiers: Difference between revisions
Content added Content deleted
(Added Fōrmulæ solution) |
(Added solution for Action!) |
||
Line 63: | Line 63: | ||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
1.3.6.1.4.1.11.2.17.5.2.0.79 |
|||
1.3.6.1.4.1.11.2.17.19.3.4.0.1 |
|||
1.3.6.1.4.1.11.2.17.19.3.4.0.4 |
|||
1.3.6.1.4.1.11.2.17.19.3.4.0.10 |
|||
1.3.6.1.4.1.11150.3.4.0 |
|||
1.3.6.1.4.1.11150.3.4.0.1 |
|||
</pre> |
|||
=={{header|Action!}}== |
|||
<lang Action!>DEFINE PTR="CARD" |
|||
PROC PrintArray(PTR ARRAY a INT size) |
|||
INT i |
|||
FOR i=0 TO size-1 |
|||
DO |
|||
PrintE(a(i)) |
|||
OD |
|||
RETURN |
|||
INT FUNC Decode(CHAR ARRAY s INT ARRAY a) |
|||
INT count |
|||
BYTE i,begin,end |
|||
CHAR ARRAY tmp(10) |
|||
count=0 i=1 |
|||
WHILE i<=s(0) |
|||
DO |
|||
begin=i |
|||
WHILE i<=s(0) AND s(i)#'. |
|||
DO i==+1 OD |
|||
end=i-1 |
|||
IF i<s(0) THEN i==+1 FI |
|||
SCopyS(tmp,s,begin,end) |
|||
a(count)=ValI(tmp) |
|||
count==+1 |
|||
OD |
|||
RETURN (count) |
|||
INT FUNC Compare(CHAR ARRAY s1,s2) |
|||
INT ARRAY a1(20),a2(20) |
|||
INT c1,c2,res |
|||
BYTE i |
|||
c1=Decode(s1,a1) |
|||
c2=Decode(s2,a2) |
|||
i=0 |
|||
DO |
|||
IF i=c1 AND i=c2 THEN |
|||
RETURN (0) |
|||
ELSEIF i=c1 THEN |
|||
RETURN (-1) |
|||
ELSEIF i=c2 THEN |
|||
RETURN (1) |
|||
FI |
|||
IF a1(i)<a2(i) THEN |
|||
RETURN (-1) |
|||
ELSEIF a1(i)>a2(i) THEN |
|||
RETURN (1) |
|||
FI |
|||
i==+1 |
|||
OD |
|||
RETURN (0) |
|||
PROC Sort(PTR ARRAY a INT size) |
|||
INT i,j,minpos |
|||
CHAR ARRAY tmp |
|||
FOR i=0 TO size-2 |
|||
DO |
|||
minpos=i |
|||
FOR j=i+1 TO size-1 |
|||
DO |
|||
IF Compare(a(minpos),a(j))>0 THEN |
|||
minpos=j |
|||
FI |
|||
OD |
|||
IF minpos#i THEN |
|||
tmp=a(i) |
|||
a(i)=a(minpos) |
|||
a(minpos)=tmp |
|||
FI |
|||
OD |
|||
RETURN |
|||
PROC Main() |
|||
DEFINE SIZE="6" |
|||
PTR ARRAY a(SIZE) |
|||
a(0)="1.3.6.1.4.1.11.2.17.19.3.4.0.10" |
|||
a(1)="1.3.6.1.4.1.11.2.17.5.2.0.79" |
|||
a(2)="1.3.6.1.4.1.11.2.17.19.3.4.0.4" |
|||
a(3)="1.3.6.1.4.1.11150.3.4.0.1" |
|||
a(4)="1.3.6.1.4.1.11.2.17.19.3.4.0.1" |
|||
a(5)="1.3.6.1.4.1.11150.3.4.0" |
|||
PrintE("Array before sort:") |
|||
PrintArray(a,SIZE) PutE() |
|||
Sort(a,SIZE) |
|||
PrintE("Array after sort:") |
|||
PrintArray(a,SIZE) |
|||
RETURN</lang> |
|||
{{out}} |
|||
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Sort_a_list_of_object_identifiers.png Screenshot from Atari 8-bit computer] |
|||
<pre> |
|||
Array before sort: |
|||
1.3.6.1.4.1.11.2.17.19.3.4.0.10 |
|||
1.3.6.1.4.1.11.2.17.5.2.0.79 |
|||
1.3.6.1.4.1.11.2.17.19.3.4.0.4 |
|||
1.3.6.1.4.1.11150.3.4.0.1 |
|||
1.3.6.1.4.1.11.2.17.19.3.4.0.1 |
|||
1.3.6.1.4.1.11150.3.4.0 |
|||
Array after sort: |
|||
1.3.6.1.4.1.11.2.17.5.2.0.79 |
1.3.6.1.4.1.11.2.17.5.2.0.79 |
||
1.3.6.1.4.1.11.2.17.19.3.4.0.1 |
1.3.6.1.4.1.11.2.17.19.3.4.0.1 |