Sort three variables
Sorting Algorithm
This is a sorting algorithm. It may be applied to a set of data in order to sort it.
For comparing various sorts, see compare sorts.
For other sorting algorithms, see sorting algorithms, or:
Heap sort | Merge sort | Patience sort | Quick sort
O(n log2n) sorts
Shell Sort
O(n2) sorts
Bubble sort |
Cocktail sort |
Cocktail sort with shifting bounds |
Comb sort |
Cycle sort |
Gnome sort |
Insertion sort |
Selection sort |
Strand sort
other sorts
Bead sort |
Bogo sort |
Common sorted list |
Composite structures sort |
Custom comparator sort |
Counting sort |
Disjoint sublist sort |
External sort |
Jort sort |
Lexicographical sort |
Natural sorting |
Order by pair comparisons |
Order disjoint list items |
Order two numerical lists |
Object identifier (OID) sort |
Pancake sort |
Quickselect |
Permutation sort |
Radix sort |
Ranking methods |
Remove duplicate elements |
Sleep sort |
Stooge sort |
[Sort letters of a string] |
Three variable sort |
Topological sort |
Tree sort
- Task
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.
ALGOL 68
This uses Algol 68's UNION facility which allows a variable to have values of a number of types whilst maintaining Algol 68's strong-typing.
Assignment allows any value of one of the UNION MODEs to be directly assigned to the variable.
When using the variable, its MODE (type) and value must be tested and extracted using a CASE construct.
As the task only requires sorting three values, we use a simple, three-element specific sort routine.
<lang algol68>BEGIN
# MODE that can hold integers and strings - would need to be extended to # # allow for other types # MODE INTORSTRING = UNION( INT, STRING ); # returns TRUE if a is an INT, FALSE otherwise # OP ISINT = ( INTORSTRING a )BOOL: CASE a IN (INT): TRUE OUT FALSE ESAC; # returns TRUE if a is an INT, FALSE otherwise # OP ISSTRING = ( INTORSTRING a )BOOL: CASE a IN (STRING): TRUE OUT FALSE ESAC; # returns the integer in a or 0 if a isn't an integer # OP TOINT = ( INTORSTRING a )INT: CASE a IN (INT i): i OUT 0 ESAC; # returns the string in a or "" if a isn't a string # OP TOSTRING = ( INTORSTRING a )STRING: CASE a IN (STRING s): s OUT "" ESAC; # returns TRUE if a < b, FALSE otherwise # # a and b must have the same type # PRIO LESSTHAN = 4; OP LESSTHAN = ( INTORSTRING a, b )BOOL: IF ISSTRING a AND ISSTRING b THEN # both strings # TOSTRING a < TOSTRING b ELIF ISINT a AND ISINT b THEN # both integers # TOINT a < TOINT b ELSE # different MODEs # FALSE FI # LESSTHAN # ; # exchanges the values of a and b # PRIO SWAP = 9; OP SWAP = ( REF INTORSTRING a, b )VOID: BEGIN INTORSTRING t := a; a := b; b := t END; # sorts a, b and c # PROC sort 3 = ( REF INTORSTRING a, b, c )VOID: BEGIN IF b LESSTHAN a THEN a SWAP b FI; IF c LESSTHAN a THEN a SWAP c FI; IF c LESSTHAN b THEN b SWAP c FI END # sort 3 # ;
# task test cases # INTORSTRING x, y, z; x := "lions, tigers, and"; y := "bears, oh my!"; z := "(from the ""Wizard of OZ"")"; sort 3( x, y, z ); print( ( x, newline, y, newline, z, newline ) ); x := 77444; y := -12; z := 0; sort 3( x, y, z ); print( ( x, newline, y, newline, z, newline ) )
END</lang>
- Output:
(from the "Wizard of OZ") bears, oh my! lions, tigers, and -12 +0 +77444
Perl 6
Perl 6 has a built in sort routine which uses a variation of quicksort. The built in sort routine will automatically select a numeric sort if given a list of Real numeric items and a lexical Unicode sort if given a list that contains strings. The default numeric sort won't sort complex numbers unless you give it a custom comparitor. It is trivial to modify the sort comparitor function to get whatever ordering you want though.
The list (77444, -12, 0) is a poor choice to demonstrate numeric sort since it will sort the same numerically or lexically. Instead we'll use (7.7444e4, -12, 18/2). ( A Num, an Int, and a Rat. )
<lang perl6># Sorting strings. Added a vertical bar between strings to make them discernable my ($a, $b, $c) = 'lions, tigers, and', 'bears, oh my!', '(from "The Wizard of Oz")'; say "sorting: {($a, $b, $c).join('|')}"; say ($a, $b, $c).sort.join('|'), ' - standard lexical string sort';
- Sorting numeric things
my ($x, $y, $z) = 7.7444e4, -12, 18/2; say "\nsorting: $x $y $z"; say ($x, $y, $z).sort, ' - standard numeric sort, low to high';
- Or, with a modified comparitor:
for -*, ' - numeric sort high to low',
~*, ' - lexical "string" sort', *.chars, ' - sort by string length short to long', -*.chars, ' - or long to short' -> $comparitor, $type { my ($x, $y, $z) = 7.7444e4, -12, 18/2; say ($x, $y, $z).sort( &$comparitor ), $type;
} say ;
- sort ALL THE THINGS
- sorts by lexical order with numeric values by magnitude.
.say for ($a, $b, $c, $x, $y, $z).sort;</lang>
- Output:
sorting: lions, tigers, and|bears, oh my!|(from "The Wizard of Oz") (from "The Wizard of Oz")|bears, oh my!|lions, tigers, and - standard lexical string sort sorting: 77444 -12 9 (-12 9 77444) - standard numeric sort, low to high (77444 9 -12) - numeric sort high to low (-12 77444 9) - lexical "string" sort (9 -12 77444) - sort by string length short to long (77444 -12 9) - or long to short (from "The Wizard of Oz") -12 9 77444 bears, oh my! lions, tigers, and
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
zkl
This solution uses list assignment and list sorting. Lists are not homogeneous, but sorting usually expects that. If that is a problem, you can give the sort a compare function. Numbers (real and integer) are homogeneous enough to sort. <lang zkl>x,y,z := "lions, tigers, and", "bears, oh my!", 0'|(from the "Wizard of OZ")|; x,y,z = List(x,y,z).sort(); println(x," | ",y," | ",z);
x,y,z := 77444, -12, 0; x,y,z = List(x,y,z).sort(); println(x," ",y," ",z);</lang>
- Output:
(from the "Wizard of OZ") | bears, oh my! | lions, tigers, and -12 0 77444