Sort numbers lexicographically: Difference between revisions

m
m (→‎{{header|Wren}}: Minor tidy)
 
(8 intermediate revisions by 5 users not shown)
Line 129:
</pre>
{{out}}
 
=={{header|ALGOL 68}}==
Uses code from the [[Sorting algorithms/Insertion sort]] tasl - included here for convenience.<br>
<syntaxhighlight lang="algol68">
BEGIN # sort numbers lexicographically #
 
# code from the Sorting algorithms/Insertion sort task #
MODE DATA = STRING;
 
PROC in place insertion sort = (REF[]DATA item)VOID:
BEGIN
INT first := LWB item;
INT last := UPB item;
INT j;
DATA value;
FOR i FROM first + 1 TO last DO
value := item[i];
j := i - 1;
WHILE ( j >= LWB item AND j <= UPB item | item[j]>value | FALSE ) DO
item[j + 1] := item[j];
j -:= 1
OD;
item[j + 1] := value
OD
END # in place insertion sort #;
 
# end code from the Sorting algorithms/Insertion sort task #
 
# returns s converted to an integer, NB: no error checking #
OP TOINT = ( STRING s )INT:
BEGIN
INT result := 0;
FOR i FROM LWB s TO UPB s DO
result *:= 10 +:= ( ABS s[ i ] - ABS "0" )
OD;
result
END # TOINT # ;
 
# returns a array of integers 1..n sorted lexicographically #
PROC lexicographic order = ( INT n )[]INT:
BEGIN
[ 1 : n ]STRING v; FOR i TO n DO v[ i ] := whole( i, 0 ) OD;
in place insertion sort( v );
[ 1 : n ]INT result;
FOR i TO n DO result[ i ] := TOINT v[ i ] OD;
result
END # lexicographic order # ;
 
# prints the elements of a #
PROC show int array = ( []INT a )VOID:
BEGIN
print( ( "[" ) );
FOR i FROM LWB a TO UPB a DO print( ( " ", whole( a[ i ], 0 ) ) ) OD;
print( ( " ]", newline ) )
END # show int array # ;
 
# test cases #
show int array( lexicographic order( 13 ) );
show int array( lexicographic order( 21 ) )
 
END
</syntaxhighlight>
{{out}}
<pre>
[ 1 10 11 12 13 2 3 4 5 6 7 8 9 ]
[ 1 10 11 12 13 14 15 16 17 18 19 2 20 21 3 4 5 6 7 8 9 ]
</pre>
 
=={{header|APL}}==
Line 481 ⟶ 548:
{{out}}
<pre>[1,10,11,12,13,2,3,4,5,6,7,8,9]</pre>
 
=={{header|BQN}}==
<syntaxhighlight lang="bqn">Task ← ⍋∘(•Fmt¨)⊸⊏1+↕
 
Task 13</syntaxhighlight>
{{out}}
<pre>⟨ 1 10 11 12 13 2 3 4 5 6 7 8 9 ⟩</pre>
 
=={{header|C}}==
Line 534 ⟶ 608:
return 0;
}</syntaxhighlight>
 
{{out}}
<pre>
Line 697 ⟶ 770:
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Sort_numbers_lexicographically}}
 
'''Solution'''
 
[[File:Fōrmulæ - Sort numbers lexicographically 01.png]]
 
'''Test case'''
 
[[File:Fōrmulæ - Sort numbers lexicographically 02.png]]
 
[[File:Fōrmulæ - Sort numbers lexicographically 03.png]]
 
=={{header|FreeBASIC}}==
Line 897 ⟶ 980:
-22: [-1, -10, -11, -12, -13, -14, -15, -16, -17, -18, -19, -2, -20, -21, -22, -3, -4, -5, -6, -7, -8, -9, 0, 1]
</pre>
 
=={{header|K}}==
<syntaxhighlight lang="k">task: 1+<$1+!:
 
task 13</syntaxhighlight>
{{out}}
<pre>1 10 11 12 13 2 3 4 5 6 7 8 9</pre>
 
=={{header|Ksh}}==
Line 936 ⟶ 1,026:
 
print -- ${arr[*]}</syntaxhighlight>
{{out}}<pre>
<pre>1 10 11 12 13 2 3 4 5 6 7 8 9</pre>
 
=={{header|jq}}==
Line 1,126 ⟶ 1,216:
13:1,10,11,12,13,2,3,4,5,6,7,8,9
21:1,10,11,12,13,14,15,16,17,18,19,2,20,21,3,4,5,6,7,8,9
 
=={{header|MiniScript}}==
Output from REPL.
{{out}}
<pre>
> n = 13
> rng = range(1, 13)
> rng.join(" ").split(" ").sort
["1", "10", "11", "12", "13", "2", "3", "4", "5", "6", "7", "8", "9"]
</pre>
 
=={{header|MUMPS}}==
Line 1,469 ⟶ 1,569:
<pre>
Lexicographical numbers = [1,10,11,12,13,2,3,4,5,6,7,8,9]
</pre>
 
=={{header|RPL}}==
{{works with|HP|48}}
≪ ≪ n →STR ≫ 'n' 1 4 ROLL 1 SEQ
SORT ≪ STR→ ≫ DOLIST
≫ '<span style="color:blue">LEXICON</span>' STO
 
13 <span style="color:blue">LEXICON</span>
{{out}}
<pre>
1: { 1 10 11 12 13 2 3 4 5 6 7 8 9 }
</pre>
 
Line 1,581 ⟶ 1,693:
=={{header|Wren}}==
{{libheader|Wren-sort}}
<syntaxhighlight lang="ecmascriptwren">import "./sort" for Sort
 
var a = (1..13).map { |i| "%(i)" }.toList
9,476

edits