Sort three variables: Difference between revisions

(→‎{{header|Ruby}}: Added Ruby)
Line 376:
11.3
</pre>
=={{header|D}}==
<lang D>import std.stdio;
 
void main() {
driver(77444, -12, 0);
driver("lions, tigers, and", "bears, oh my!", "(from the \"Wizard of OZ\")");
}
 
void driver(T)(T x, T y, T z) {
writeln("BEFORE: x=[", x, "]; y=[", y, "]; z=[", z, "]");
sort3Var(x,y,z);
writeln("AFTER: x=[", x, "]; y=[", y, "]; z=[", z, "]");
}
 
void sort3Var(T)(ref T x, ref T y, ref T z)
out {
assert(x<=y);
assert(x<=z);
assert(y<=z);
}
body {
import std.algorithm : swap;
 
if (x < y) {
if (z < x) {
swap(x,z);
}
} else if (y < z) {
swap(x,y);
} else {
swap(x,z);
}
if (z<y) {
swap(y,z);
}
}</lang>
 
{{out}}
<pre>BEFORE: x=[77444]; y=[-12]; z=[0]
AFTER: x=[-12]; y=[0]; z=[77444]
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|Elena}}==
ELENA 3.2 :
1,452

edits