Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
35
Task/Sort-three-variables/D/sort-three-variables.d
Normal file
35
Task/Sort-three-variables/D/sort-three-variables.d
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
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);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue