Sort three variables: Difference between revisions

m (syntax highlighting fixup automation)
 
(11 intermediate revisions by 9 users not shown)
Line 298:
+0
+77444</pre>
 
=={{header|Amazing Hopper}}==
<syntaxhighlight lang="c">
 
#include <basico.h>
 
algoritmo
 
x = 77444, y = -12.5, z = 0
low=x, midd=y, high=z
imprimir("BEFORE:\nX = ",x, " Y = ",y, " Z = ",z,NL)
#basic{
x = min( min( low,midd),high)
z = max( max( low,midd),high)
y = low + midd + high - x - z
}
 
imprimir("\nAFTER:\nX = ",x, " Y = ",y, " Z = ",z,NL,NL)
 
x = "lions, tigers, and"
y = "bears, oh my!"
z = "(from the \"Wizard of OZ\")"
imprimir("BEFORE:\nX = ",x, "\nY = ",y, "\nZ = ",z,NL)
#(x > y), entonces { intercambiar( x,y) }
#(y > z), entonces {
intercambiar (y,z )
#(x > y), entonces { intercambiar( x,y ) }
}
 
imprimir("\nAFTER:\nX = ",x, "\nY = ",y, "\nZ = ",z,NL,NL)
 
p = {}
'"lions, tigers, and",77444,"bears, oh my!",-12.7,0,"(from the \"Wizard of OZ\")"'
enlistar en 'p'
 
fijar separador 'NL'
imprimir("BEFORE:\n",p,NL)
matriz.ordenar(p)
imprimir("\nAFTER:\n",p,NL)
 
 
terminar
</syntaxhighlight>
{{out}}
<pre>
$ hopper3 basica/sort3var.bas
BEFORE:
X = 77444 Y = -12.500000 Z = 0
 
AFTER:
X = -12.500000 Y = 0.000000 Z = 77444.000000
 
BEFORE:
X = lions, tigers, and
Y = bears, oh my!
Z = (from the "Wizard of OZ")
 
AFTER:
X = (from the "Wizard of OZ")
Y = bears, oh my!
Z = lions, tigers, and
 
BEFORE:
lions, tigers, and
77444
bears, oh my!
-12.700000
0
(from the "Wizard of OZ")
 
AFTER:
(from the "Wizard of OZ")
bears, oh my!
lions, tigers, and
-12.700000
0
77444
 
</pre>
 
=={{header|APL}}==
Line 903 ⟶ 985:
BEFORE: x=[lions, tigers, and]; y=[bears, oh my!]; z=[(from the "Wizard of OZ")]
AFTER: x=[(from the "Wizard of OZ")]; y=[bears, oh my!]; z=[lions, tigers, and]</pre>
 
=={{header|EasyLang}}==
<syntaxhighlight lang=easylang>
proc sort3 . a b c .
if a > c
swap a c
.
if b > c
swap b c
.
if a > b
swap a b
.
.
x = 77444
y = -12
z = 0
sort3 x y z
print x & " " & y & " " & z
#
proc sort3str . a$ b$ c$ .
if strcmp a$ c$ > 0
swap a$ c$
.
if strcmp b$ c$ > 0
swap b$ c$
.
if strcmp a$ b$ > 0
swap a$ b$
.
.
x$ = "lions, tigers, and"
y$ = "bears, oh my!"
z$ = "(from the \"Wizard of OZ\")"
sort3str x$ y$ z$
print x$
print y$
print z$
</syntaxhighlight>
 
 
=={{header|EDSAC order code}}==
Line 956 ⟶ 1,078:
 
=={{header|Elena}}==
ELENA 56.0x :
<syntaxhighlight lang="elena">import extensions;
Line 1,137 ⟶ 1,259:
typically be used directly from the stack by a subsequent operation in the
program.
<LANGsyntaxhighlight lang="forth" lines>\ sort 3 integers
VARIABLE X VARIABLE Y VARIABLE Z
 
Line 1,146 ⟶ 1,268:
2DUP < IF SWAP THEN ;
 
: SORT3INTS ( a b c -- c b a) ?SWAP >R ?SWAP R> ?SWAP ;</LANGsyntaxhighlight>
 
Testing is done using the Forth console using '?' to view VARIABLE contents
Line 1,161 ⟶ 1,283:
==={{header|Strings}}===
Strings require extending the language but the primitives needed are part of ANS/ISO Forth.
<LANGsyntaxhighlight lang="forth" lines>DECIMAL
: BUFFER: ( n -- ) CREATE ALLOT ;
 
Line 1,186 ⟶ 1,308:
\ non-destructive print 3 counted-strings from data stack
: .STRS ( caddr1 caddr2 caddr3 -- caddr1 caddr2 caddr3) \ order is dependant
3 0 DO ROT DUP CR COUNT TYPE LOOP ; </LANGsyntaxhighlight>
With these extensions we can do the same testing at the Forth console and
examine the string order with '.STRS'.
Line 1,265 ⟶ 1,387:
=={{header|Fōrmulæ}}==
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Sort_three_variables}}
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text. Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation &mdash;i.e. XML, JSON&mdash; they are intended for storage and transfer purposes more than visualization and edition.
 
'''Solution'''
Programs in Fōrmulæ are created/edited online in its [https://formulae.org website], However they run on execution servers. By default remote servers are used, but they are limited in memory and processing power, since they are intended for demonstration and casual use. A local server can be downloaded and installed, it has no limitations (it runs in your own computer). Because of that, example programs can be fully visualized and edited, but some of them will not run if they require a moderate or heavy computation/memory resources, and no local server is being used.
 
'''Test case 1'''
In '''[https://formulae.org/?example=Sort_three_variables this]''' page you can see the program(s) related to this task and their results.
 
[[File:Fōrmulæ - Sort three variables 01.png]]
 
[[File:Fōrmulæ - Sort three variables 02.png]]
 
'''Test case 2'''
 
[[File:Fōrmulæ - Sort three variables 03.png]]
 
[[File:Fōrmulæ - Sort three variables 04.png]]
 
=={{header|Free Pascal}}==
Line 1,345 ⟶ 1,477:
Y = 9.0099999999999998E+000
Z = 1.2340000000000000E+001</pre>
 
=={{header|Frink}}==
The following sorts the values in the variables x, y, and z and sets the sorted values back to the variables.
 
<syntaxhighlight lang="frink">x = 77444
y = -12
z = 0
 
[x,y,z] = sort[[x,y,z]]
println["x = $x"]
println["y = $y"]
println["z = $z"]</syntaxhighlight>
{{out}}
<pre>
x = -12
y = 0
z = 77444
</pre>
 
=={{header|Go}}==
Line 3,115 ⟶ 3,265:
bears, oh my!
lions, tigers, and</pre>
 
=={{header|RPL}}==
{{works with|HP|48}}
≪ → x y z
≪ x y z 3 →LIST SORT LIST→ DROP
≫ ≫ '<span style="color:blue">SORT3</span>' STO
 
"lions, tigers, and" "bears, oh my!" "(from the 'Wizard of OZ')" <span style="color:blue">SORT3</span>
77444 -12 9 <span style="color:blue">SORT3</span>
{{out}}
<pre>
6: "bears, oh my!"
5: "lions, tigers, and"
4: "(from the 'Wizard of OZ')"
3: -12
2: 9
1: 77444
</pre>
 
=={{header|Ruby}}==
Line 3,123 ⟶ 3,291:
puts x, y, z
 
x, y, z = 7.7444e4, -12, 18/2r # Float, Integer, Rational; taken from Perl 6Raku
x, y, z = [x, y, z].sort
puts x, y, z
Line 3,338 ⟶ 3,506:
z: 77444
</pre>
 
=={{header|TXR}}==
 
The following is a comprehensive solution to the general problem of sorting any number of mutable places. We develop a macro operator called <code>sort-places</code> which is called with zero or more argument expressions that denote assignable places. The places are sorted in order according to the <code>greater</code> function.
 
The zero and one argument cases are handled as no-ops; the arguments are not evaluated at all, even for their side effects if they have any. The two argument case is handled by generating a conditional which controls a single swap. The three-argument case performs up to three swaps.
 
For four or more arguments, a hidden vector object is generated. The values of the places are stuffed into the vector, which is then sorted, after which the values are retrieved and stored in the places.
 
'''All cases work in such a way that the place expressions are evaluated at most once,''' which is achieved by leveraging the simple-to-use <code>placelet</code> macro.
 
<syntaxhighlight lang="txrlisp">(defmacro sort-places (. places)
(caseql (len places)
((0 1) nil)
(2 (with-gensyms (p0 p1)
^(placelet ((p0 (read-once ,[places 0]))
(p1 (read-once ,[places 1])))
(if (greater p0 p1)
(swap p0 p1)))))
(3 (with-gensyms (p0 p1 p2)
^(placelet ((p0 (read-once ,[places 0]))
(p1 (read-once ,[places 1]))
(p2 (read-once ,[places 2])))
(if (greater p0 p1)
(swap p0 p1))
(if (greater p1 p2)
(swap p1 p2))
(if (greater p0 p1)
(swap p0 p1)))))
(t (let ((gens [mapcar (ret (gensym)) places]))
(with-gensyms (vec)
^(placelet ,(zip gens places)
(let ((,vec (vec ,*gens)))
(nsort ,vec)
(set ,*(append-each ((g gens)
(i 0))
^(,g [,vec ,i]))))))))))
 
(prinl (sort-places))
 
(let ((x 1))
(sort-places x)
(prinl x))
 
(let ((x 2)
(y 1))
(sort-places x y)
(prinl (list x y)))
 
(let ((a 3)
(b 2)
(c 1))
(sort-places a b c)
(prinl (list a b c)))
 
(let ((a 4)
(b 3)
(c 2)
(d 1))
(sort-places a b c d)
(prinl (list a b c d)))</syntaxhighlight>
 
{{out}}
 
<pre>nil
1
(1 2)
(1 2 3)
(1 2 3 4)</pre>
 
=={{header|Visual Basic .NET}}==
Line 3,383 ⟶ 3,620:
{{libheader|Wren-sort}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./sort" for Sort
import "./fmt" for Fmt
 
var sort3 = Fn.new { |x, y, z|
490

edits