Sort three variables: Difference between revisions
Content added Content deleted
m (→Free Pascal: fix syntax mistake) |
(Added solution for Little Man Computer) |
||
Line 1,876: | Line 1,876: | ||
(from the "Wizard of OZ") |
(from the "Wizard of OZ") |
||
lions, tigers, and</pre> |
lions, tigers, and</pre> |
||
=={{header|Little Man Computer}}== |
|||
The LMC in its original form supports only integers in the range 0..999. So the numbers in the task have been changed here from 77444, -12, 0 to 774, 0, 12. |
|||
<lang Little Man Computer> |
|||
// Little Man Computer |
|||
// Sort x, y, z, in ascending order |
|||
// Based on a sorting network: |
|||
// if x > z then swap( x, z) |
|||
// if x > y then swap( x, y) |
|||
// if y > z then swap( y, z) |
|||
// with the addition that if the 2nd swap is executed |
|||
// then the 3rd comparison is not needed. |
|||
// Read numbers x, y, z, and display in their original order |
|||
INP |
|||
STA x |
|||
OUT |
|||
INP |
|||
STA y |
|||
OUT |
|||
INP |
|||
STA z |
|||
OUT |
|||
// Sort so that x <= y <= z, and display in new order |
|||
LDA z |
|||
SUB x |
|||
BRP label1 |
|||
LDA x |
|||
STA t |
|||
LDA z |
|||
STA x |
|||
LDA t |
|||
STA z |
|||
label1 LDA y |
|||
SUB x |
|||
BRP label2 |
|||
LDA x |
|||
STA t |
|||
LDA y |
|||
STA x |
|||
LDA t |
|||
STA y |
|||
BRA sorted // added to the sorting network |
|||
label2 LDA z |
|||
SUB y |
|||
BRP sorted |
|||
LDA y |
|||
STA t |
|||
LDA z |
|||
STA y |
|||
LDA t |
|||
STA z |
|||
sorted LDA x |
|||
OUT |
|||
LDA y |
|||
OUT |
|||
LDA z |
|||
OUT |
|||
HLT |
|||
x DAT |
|||
y DAT |
|||
z DAT |
|||
t DAT |
|||
// end |
|||
</lang> |
|||
{{out}} |
|||
<pre> |
|||
744 |
|||
0 |
|||
12 |
|||
0 |
|||
12 |
|||
744 |
|||
</pre> |
|||
=={{header|Lua}}== |
=={{header|Lua}}== |