Jump to content

Sort three variables: Difference between revisions

Add source for Rust
(Added Wren)
(Add source for Rust)
Line 302:
=={{header|AutoHotkey}}==
<lang AutoHotkey>SortThreeVariables(ByRef x,ByRef y,ByRef z){
obj := []
for k, v in (var := StrSplit("x,y,z", ","))
obj[%v%] := true
for k, v in obj
temp := var[A_Index], %temp% := k
}</lang>
Examples:<lang AutoHotkey>x = lions, tigers, and
Line 345:
int main()
{
char values[MAX][100],tempStr[100];
int i,j,isString=0;
double val[MAX],temp;
for(i=0;i<MAX;i++){
printf("Enter %d%s value : ",i+1,(i==0)?"st":((i==1)?"nd":"rd"));
fgets(values[i],100,stdin);
for(j=0;values[i][j]!=00;j++){
if(((values[i][j]<'0' || values[i][j]>'9') && (values[i][j]!='.' ||values[i][j]!='-'||values[i][j]!='+'))
||((values[i][j]=='.' ||values[i][j]=='-'||values[i][j]=='+')&&(values[i][j+1]<'0' || values[i][j+1]>'9')))
isString = 1;
}
}
}
if(isString==0){
for(i=0;i<MAX;i++)
val[i] = atof(values[i]);
}
for(i=0;i<MAX-1;i++){
for(j=i+1;j<MAX;j++){
if(isString==0 && val[i]>val[j]){
temp = val[j];
val[j] = val[i];
val[i] = temp;
}
}
else if(values[i][0]>values[j][0]){
strcpy(tempStr,values[j]);
strcpy(values[j],values[i]);
strcpy(values[i],tempStr);
}
}
}
}
}
for(i=0;i<MAX;i++)
isString==1?printf("%c = %s",'X'+i,values[i]):printf("%c = %lf",'X'+i,val[i]);
return 0;
}
</lang>The output shows three test cases, two as specified in the task, and one which mixes numbers and strings. The output is sorted considering all of them as strings in that case.
Line 415:
int main()
{
int x = 77444,y=-12,z=0,temp;
printf("Before sorting :\nx = %d\ny = %d\nz = %d",x,y,z);
do{
temp = x;
if(temp > y){
x = y;
y = temp;
}
if(z < y){
temp = y;
y = z;
z = temp;
}
}while(x>y || y>z);
printf("\nAfter sorting :\nx = %d\ny = %d\nz = %d",x,y,z);
return 0;
}
</lang>
Line 820:
Routines that modify their parameters should not be invoked with constants (or text literals) as such parameters... Some systems allow constants to be in protected storage, and if so, an attempt to modify such storage will produce a run-time error. Otherwise, it all depends on how constants are passed as parameters. If a temporary storage item is loaded with the desired value and the address of that scratch variable is passed, then disaster will be averted - though good results may not be produced.
 
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)
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 numbers. Incidentally, not everything is done in ASCII. The EBCDIC ordering is different.
Line 1,713:
<pre># test ();;
case 1:
x: (from the "Wizard of OZ")
y: bears, oh my!
z: lions, tigers, and
case 1:
x: -12
y: 0
z: 77444
- : unit = ()</pre>
 
Line 2,237:
9/1
77444.0</pre>
 
=={{header|Rust}}==
<lang rust>fn main() {
let mut array = [5, 1, 3];
array.sort();
println!("Sorted: {:?}", array);
array.sort_by(|a, b| b.cmp(a));
println!("Reverse sorted: {:?}", array);
}
</lang>
 
==Scala==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.