How to resolve the algorithm Sort three variables step by step in the ALGOL 68 programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Sort three variables step by step in the ALGOL 68 programming language

Table of Contents

Problem Statement

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: After sorting, the three variables would hold: For numeric value sorting, use: I.E.:   (for the three variables   x,   y,   and   z),   where: After sorting, the three variables would hold: 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:

Another algorithm   (only for numeric values):

Show the results of the sort here on this page using at least the values of those shown above.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Sort three variables step by step in the ALGOL 68 programming language

Source code in the algol programming language

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

  

You may also check:How to resolve the algorithm Caesar cipher step by step in the QBasic programming language
You may also check:How to resolve the algorithm Regular expressions step by step in the langur programming language
You may also check:How to resolve the algorithm Globally replace text in several files step by step in the Jsish programming language
You may also check:How to resolve the algorithm Environment variables step by step in the Liberty BASIC programming language
You may also check:How to resolve the algorithm Sorting algorithms/Heapsort step by step in the Ada programming language