Sort three variables: Difference between revisions
(Created page with "{{draft task}} Sort (the values of) three variables (X, Y, and Z) that contain any value (numbers and/or literals). If that isn't possible...") |
m (used a bigger-sized bullet.) |
||
Line 45: | Line 45: | ||
One algorithm could be: |
One algorithm could be: |
||
Θ store the three variables '''x''', '''y''', and '''z''' |
|||
into an array (or a list) '''A''' |
into an array (or a list) '''A''' |
||
Θ sort (the three elements of) the array '''A''' |
|||
Θ extract the three elements from the array and place them in the |
|||
variables '''x''', '''y''', and '''z''' in order of extraction |
variables '''x''', '''y''', and '''z''' in order of extraction |
||
Revision as of 04:48, 30 April 2017
Sort (the values of) three variables (X, Y, and Z) that contain any value (numbers and/or literals).
If that isn't possible in your language, then just sort numbers (and note if they can be floating point, integer, or other).
I.E.: (for the three variables x, y, and z), where:
x = 'lions, tigers, and' y = 'bears, oh my!' z = '(from the "Wizard of OZ")'
After sorting, the three variables would hold:
x = '(from the "Wizard of OZ")' y = 'bears, oh my!' z = 'lions, tigers, and'
For numeric value sorting, use:
I.E.: (for the three variables x, y, and z), where:
x = 77444 y = -12 z = 0
After sorting, the three variables would hold:
x = -12 y = 0 z = 77444
The variables should contain some form of a number, but specify if the algorithm used can be for floating point or integers. Note any limitations.
The values may or may not be unique.
The method used for sorting can be any algorithm; the goal is to use the most idiomatic in the computer programming language used.
More than one algorithm could be shown if one isn't clearly the better choice.
One algorithm could be:
Θ store the three variables x, y, and z into an array (or a list) A
Θ sort (the three elements of) the array A
Θ extract the three elements from the array and place them in the variables x, y, and z in order of extraction
Show the results of the sort here on this page using at least the values of those shown above.
REXX
Since Classic REXX has no native sorting built-in, here is an alternative algorithm.
generic
This version will sort numbers and/or literals.
The literals can be of any length (only limited by virtual memory or language limitations). <lang rexx>/*REXX program sorts three (any value) variables (X, Y, and Z) into ascending order.*/ parse arg x y z . /*obtain the three variables from C.L. */ if x== | x=="," then x= 'lions, tigers, and' /*Not specified? Use the default*/ if y== | y=="," then y= 'bears, oh my!' /* " " " " " */ if z== | z=="," then z= '(from "The Wizard of Oz")' /* " " " " " */ say '───── original value of X: ' x say '───── original value of Y: ' y say '───── original value of Z: ' z if x>y then do; _=x; x=y; y=_; end /*swap the values of X and Y. */ /* ◄─── sorting.*/ if y>z then do; _=y; y=z; z=_; end /* " " " " Y " Z. */ /* ◄─── sorting.*/ if x>y then do; _=x; x=y; y=_; end /* " " " " X " Y. */ /* ◄─── sorting */ say /*stick a fork in it, we're all done. */ say '═════ sorted value of X: ' x say '═════ sorted value of Y: ' y say '═════ sorted value of Z: ' z</lang>
- output when using the default inputs:
───── original value of X: lions, tigers, and ───── original value of Y: bears, oh my! ───── original value of Z: (from "The Wizard of Oz") ═════ sorted value of X: (from "The Wizard of Oz") ═════ sorted value of Y: bears, oh my! ═════ sorted value of Z: lions, tigers, and
numeric only
This version will sort numbers (the numbers can be in any form, floating point and/or integer).
The maximum integer than be kept as an integer is (in this program) is 1,000 decimal digits. <lang rexx>/*REXX program sorts three (numeric) variables (X, Y, and Z) into ascending order. */ numeric digits 1000 /*handle some pretty gihugic integers. */ /*can be bigger.*/ parse arg x y z . /*obtain the three variables from C.L. */ if x== | x=="," then x= 77444 /*Not specified? Then use the default.*/ if y== | y=="," then y= -12 /* " " " " " " */ if z== | z=="," then z= 0 /* " " " " " " */ w=max( length(x), length(y), length(z) ) + 5 /*find max width of the values, plus 5.*/ say '───── original values of X, Y, and Z: ' right(x, w) right(y, w) right(z, w) low = x /*assign a temporary variable. */ /* ◄─── sorting.*/ mid = y /* " " " " */ /* ◄─── sorting.*/ high= z /* " " " " */ /* ◄─── sorting.*/
x=min(low, mid, high) /*determine the lowest value of X,Y,Z. */ /* ◄─── sorting.*/ z=max(low, mid, high) /* " " highest " " " " " */ /* ◄─── sorting.*/ y= low + mid + high - x - z /* " " middle " " " " " */ /* ◄─── sorting.*/ /*stick a fork in it, we're all done. */
say '═════ sorted values of X, Y, and Z: ' right(x, w) right(y, w) right(z, w)</lang>
- output when using the default inputs:
───── original values of X, Y, and Z: 77444 -12 0 ═════ sorted values of X, Y, and Z: -12 0 77444