Jump to content

Sort three variables: Difference between revisions

(Added Kotlin)
(→‎{{header|Kotlin}}: Add Fortran.)
Line 123:
+0
+77444
</pre>
 
=={{header|Fortran}}==
Fortran does not offer a multi-mode facility whereby a variable can be the union of various basic types such as integer or floating-point or character. The EQUIVALENCE statement can cause three such different variables to be placed at the same storage location or indeed, because parameter type-checking is rarely performed by a Fortran compiler (especially with routines compiled separately) it is often possible to pass a floating-point variable to a subroutine expecting an integer, and so forth. Leaving aside questions of storage size mismatch (as when a subroutine stores a sixty-four bit double-precision value into a parameter it had been supplied as a sixteen-bit integer, say) the bit patterns of disparate types will rarely involve a useful ordering. For example, an eight-byte floating-point variable could contain the bit pattern of "Hi There" but the numerical value that will manifest will not likely be useful in sorting text.
 
It is possible to use floating-point variables to store integer values and their ordering will be sensible, but the bit patterns of integer and floating-point values are very different. Again, a subroutine that is supplied integer variables (even of the right size) but which works with floating-point variables will not produce a sensible ordering.
 
The most flexible type of variable is text, as the text can represent any type of value - but as text, not as a proper number. The ordering resulting from numbers represented as text will be very dependent. This is discussed in [[Natural_sorting]] for example.
 
The example source uses F90 mainly to enable the contained routine SWAPC though it could be made into a separate subroutine. Further F90 facilities would allow the definition of a "generic" SORT3 routine, with a lot of syntax that would enable a SORT3I2, SORT3I4, SORT3F4, ''etc.'' routines for INTEGER*2, INTEGER*4, REAL*4, ''etc.'' to be selected for each case. A suitable pre-processor scheme could perhaps generate these variations, but alas, it is not standard. There would be similar requirements for a SWAP routine for each type of parameter, for alas, Fortran does not define a SWAP statement. The temporary variable needed for the SWAP process is defined rather intimidatingly as having the size of the largest of the three parameters, a facility possibly not available until F90. Rather than have this redefined for each invocation of SWAPC (where it would be the larger of the two parameters) it is defined once in SORT3. However, all three parameters should be the same size, or risk truncation. Using a great deal more syntax (or, as standardised in F2003) it is possible to have character variables be resized on each assignment to them to accommodate the length of text being assigned.
 
For convenience in setting up the two examples, an array is used to hold the test data. The subroutine is not invoked with an array parameter, it is invoked with three separate elements of the array. The DATA statement initialising the array looks to be the transpose of the desired ordering, because of the way Fortran orders elements in storage. </lang Fortran> SUBROUTINE SORT3(X,Y,Z) !Perpetrate a bubblesort in-line.
CHARACTER*(*) X,Y,Z !Just three to rearrange.
CHARACTER*(MAX(LEN(X),LEN(Y),LEN(Z))) T !Really, they should all be the same length.
IF (X.GT.Y) CALL SWAPC(X,Y) !The first pass: for i:=2:3 do if a(i - 1) > a(i) swap
IF (Y.GT.Z) CALL SWAPC(Y,Z) !The second test of the first pass.
IF (X.GT.Y) CALL SWAPC(X,Y) !The second pass: for i:=2:2...
CONTAINS !Alas, Fortran does not offer a SWAP statement.
SUBROUTINE SWAPC(A,B) !So, one must be devised for each case.
CHARACTER*(*) A,B !To have their content swapped.
T = A !Ccpy the first to a safe space.
A = B !Copy the second on to the first.
B = T !Copy what had been first to the second.
END SUBROUTINE SWAPC !One of these will be needed for each type of datum.
END SUBROUTINE SORT3 !No attempt is made to stop early, as for already-ordered data.
 
PROGRAM POKE
CHARACTER*28 XYZ(3,2) !Encompass the two examples.
DATA XYZ/ !Storage order is "column-major".
1 'lions, tigers, and','bears, oh my!','(from the "Wizard of OZ")', !So (1,1), (2,1), (3,1)
2 '77444',' -12',' 0'/ !So this looks like a transposed array. But (1,2), (2,2), (3,2) (1,2), (2,2), (3,2)
INTEGER I !A stepper for the loop.
DO I = 1,2 !Two examples.
WRITE (6,66) "Supplied: ", XYZ(1:3,I) !As given.
66 FORMAT (A12,3(" >",A,"<")) !Show as >text< for clarity.
 
CALL SORT3(XYZ(1,I),XYZ(2,I),XYZ(3,I)) !Three separate variables, that happen to be in an array.
 
WRITE (6,66) "Sorted, ? ", XYZ(1:3,I) !The result.
END DO !On to the next example.
END !Nothing much.
</lang>
Output: the texts showing numbers appear in text order, not the order of their texts. Incidentally, not everything is done in ASCII. The EBCDIC ordering is different.
<pre>
Supplied: >lions, tigers, and < >bears, oh my! < >(from the "Wizard of OZ") <
Sorted, ? >(from the "Wizard of OZ") < >bears, oh my! < >lions, tigers, and <
Supplied: >77444 < > -12 < > 0 <
Sorted, ? > 0 < > -12 < >77444 <
</pre>
 
1,220

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.