Sort numbers lexicographically: Difference between revisions

m
m (→‎{{header|AppleScript}}: New sort handler URL and tidy-up in second script.)
m (→‎{{header|Wren}}: Minor tidy)
(9 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 696 ⟶ 769:
=={{header|Fōrmulæ}}==
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Sort_numbers_lexicographically}}
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text. Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation &mdash;i.e. XML, JSON&mdash; they are intended for storage and transfer purposes more than visualization and edition.
 
'''Solution'''
Programs in Fōrmulæ are created/edited online in its [https://formulae.org website], However they run on execution servers. By default remote servers are used, but they are limited in memory and processing power, since they are intended for demonstration and casual use. A local server can be downloaded and installed, it has no limitations (it runs in your own computer). Because of that, example programs can be fully visualized and edited, but some of them will not run if they require a moderate or heavy computation/memory resources, and no local server is being used.
 
[[File:Fōrmulæ - Sort numbers lexicographically 01.png]]
In '''[https://formulae.org/?example=Sort_numbers_lexicographically this]''' page you can see the program(s) related to this task and their results.
 
'''Test case'''
 
[[File:Fōrmulæ - Sort numbers lexicographically 02.png]]
 
[[File:Fōrmulæ - Sort numbers lexicographically 03.png]]
 
=={{header|FreeBASIC}}==
Line 901 ⟶ 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 940 ⟶ 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,130 ⟶ 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,473 ⟶ 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,585 ⟶ 1,693:
=={{header|Wren}}==
{{libheader|Wren-sort}}
<syntaxhighlight lang="ecmascriptwren">import "./sort" for Sort
 
var a = (1..13).map { |i| "%(i)" }.toList
9,482

edits