Sort three variables: Difference between revisions

No edit summary
Line 3:
{{Sorting Algorithm}}
 
;Task:
Sort   (the values of)   three variables   ('''X''',   '''Y''',   and   '''Z''')   that contain any value   (numbers and/or literals).
 
Line 1,073:
 
In '''[https://formulae.org/?example=Sort_three_variables this]''' page you can see the program(s) related to this task and their results.
 
=={{header|Free Pascal}}==
The FPC (FreePascal Compiler) adopted Delphi’s generics scheme.
It works in the <tt>{$mode objFPC}</tt> and <tt>{$mode Delphi}</tt> compiler compatibility modes, the former requiring the keywords <tt>generic</tt>/<tt>specialize</tt>.
{$mode objFPC}
 
generic procedure sort<T>(var X, Y: T);
procedure swap;
var
Z: T;
begin
Z := X;
X := Y;
Y := Z
end;
begin
if X > Y then
begin
swap
end
end;
 
generic procedure sort<T>(var X, Y, Z: T);
begin
specialize sort<T>(X, Y);
specialize sort<T>(X, Z);
specialize sort<T>(Y, Z)
end;
 
generic procedure print<T>(const X, Y, Z: T);
begin
writeLn('X = ', X);
writeLn('Y = ', Y);
writeLn('Z = ', Z)
end;
 
{ === MAIN ============================================================= }
var
A, B, C: string;
I, J, K: integer;
P, Q, R: real;
begin
A := 'lions, tigers, and';
B := 'bears, oh my!';
C := '(from the "Wizard of OZ")';
specialize sort<string>(A, B, C);
specialize print<string>(A, B, C);
writeLn;
I := 77444;
J := -12;
K := 0;
specialize sort<integer>(I, J, K);
specialize print<integer>(I, J, K);
writeLn;
P := 12.34;
Q := -56.78;
R := 9.01;
specialize sort<real>(P, Q, R);
specialize print<real>(P, Q, R)
end.</lang>
{{out}}
<pre>X = (from the "Wizard of OZ")
Y = bears, oh my!
Z = lions, tigers, and
 
X = -12
Y = 0
Z = 77444
 
X = -5.6780000000000001E+001
Y = 9.0099999999999998E+000
Z = 1.2340000000000000E+001</pre>
 
=={{header|Go}}==
Line 2,032 ⟶ 2,107:
0
77444</pre>
 
=={{header|Nim}}==
<lang nim>proc sortThree[T](a, b, c: var T) =
Line 2,098 ⟶ 2,174:
z: 77444
- : unit = ()</pre>
 
=={{header|Pascal}}==
{{works with|Extended Pascal}}
Pascal is a statically typed programming language.
The data types of all variables and routine parameters need to be known in advance.
The following <tt>program</tt> demonstrates sorting three strings, but can be easily adapted to work for <tt>integer</tt> and <tt>real</tt> variables as well.
<lang pascal>program sortThreeVariables(output);
 
type
{ this Extended Pascal data type may hold up to 25 `char` values }
line = string(25);
 
{ this procedure sorts two lines }
procedure sortLines(var X, Y: line);
{ nested procedure allocates space for Z only if needed }
procedure swap;
var
Z: line;
begin
Z := X;
X := Y;
Y := Z
end;
begin
{ for lexical sorting write `if GT(X, Y) then` }
if X > Y then
begin
swap
end
end;
 
{ sorts three line variables’s values }
procedure sortThreeLines(var X, Y, Z: line);
begin
{ `var` parameters can be modified at the calling site }
sortLines(X, Y);
sortLines(X, Z);
sortLines(Y, Z)
end;
 
{ writes given lines on output preceded by `X = `, `Y = ` and `Z = ` }
procedure printThreeLines(protected X, Y, Z: line);
begin
{ `protected` paremeters cannot be overwritten }
writeLn('X = ', X);
writeLn('Y = ', Y);
writeLn('Z = ', Z)
end;
 
{ === MAIN ============================================================= }
var
A, B: line;
{ for demonstration purposes: alternative method to initialize }
C: line value '(from the "Wizard of OZ")';
begin
A := 'lions, tigers, and';
B := 'bears, oh my!';
sortThreeLines(A, B, C);
printThreeLines(A, B, C)
end.</lang>
 
=={{header|Perl}}==
Line 2,563 ⟶ 2,700:
bears, oh my!
lions, tigers, and</pre>
 
=={{header|Red}}==
Red can natively sort values of various types. For instance here (not exhaustive): strings, integers, floats, characters, IP addresses, email addresses, words (~ variable names).
149

edits