Sort an integer array: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
|||
Line 12: | Line 12: | ||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="11l">nums = [2,4,3,1,2] |
||
nums.sort()</ |
nums.sort()</syntaxhighlight> |
||
You could also use the built-in sorted() function: |
You could also use the built-in sorted() function: |
||
< |
<syntaxhighlight lang="11l">nums = sorted([2,4,3,1,2])</syntaxhighlight> |
||
=={{header|4D}}== |
=={{header|4D}}== |
||
===English=== |
===English=== |
||
< |
<syntaxhighlight lang="4d">ARRAY INTEGER($nums;0) |
||
APPEND TO ARRAY($nums;2) |
APPEND TO ARRAY($nums;2) |
||
APPEND TO ARRAY($nums;4) |
APPEND TO ARRAY($nums;4) |
||
Line 27: | Line 27: | ||
APPEND TO ARRAY($nums;2) |
APPEND TO ARRAY($nums;2) |
||
SORT ARRAY($nums) ` sort in ascending order |
SORT ARRAY($nums) ` sort in ascending order |
||
SORT ARRAY($nums;<) ` sort in descending order</ |
SORT ARRAY($nums;<) ` sort in descending order</syntaxhighlight> |
||
===Français=== |
===Français=== |
||
< |
<syntaxhighlight lang="4d">TABLEAU ENTIER($nombres;0) |
||
AJOUTER A TABLEAU($nombres;2) |
AJOUTER A TABLEAU($nombres;2) |
||
AJOUTER A TABLEAU($nombres;4) |
AJOUTER A TABLEAU($nombres;4) |
||
Line 38: | Line 38: | ||
AJOUTER A TABLEAU($nombres;2) |
AJOUTER A TABLEAU($nombres;2) |
||
TRIER TABLEAU($nombres) ` pour effectuer un tri par ordre croissant |
TRIER TABLEAU($nombres) ` pour effectuer un tri par ordre croissant |
||
TRIER TABLEAU($nombres;<) ` pour effectuer un tri par ordre décroissant</ |
TRIER TABLEAU($nombres;<) ` pour effectuer un tri par ordre décroissant</syntaxhighlight> |
||
=={{header|8th}}== |
=={{header|8th}}== |
||
< |
<syntaxhighlight lang="forth"> |
||
[ 10,2,100 ] ' n:cmp a:sort . cr |
[ 10,2,100 ] ' n:cmp a:sort . cr |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output is: [2,10,100] |
Output is: [2,10,100] |
||
=={{header|AArch64 Assembly}}== |
=={{header|AArch64 Assembly}}== |
||
{{works with|as|Raspberry Pi 3B version Buster 64 bits}} |
{{works with|as|Raspberry Pi 3B version Buster 64 bits}} |
||
<syntaxhighlight lang="aarch64 assembly"> |
|||
<lang AArch64 Assembly> |
|||
/* ARM assembly AARCH64 Raspberry PI 3B */ |
/* ARM assembly AARCH64 Raspberry PI 3B */ |
||
/* program integerSort64.s with selection sort */ |
/* program integerSort64.s with selection sort */ |
||
Line 213: | Line 213: | ||
.include "../includeARM64.inc" |
.include "../includeARM64.inc" |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Action!}}== |
=={{header|Action!}}== |
||
{{libheader|Action! Tool Kit}} |
{{libheader|Action! Tool Kit}} |
||
< |
<syntaxhighlight lang="action!">INCLUDE "D2:SORT.ACT" ;from the Action! Tool Kit |
||
PROC PrintArray(INT ARRAY a INT size) |
PROC PrintArray(INT ARRAY a INT size) |
||
Line 255: | Line 255: | ||
Test(c,8,ASCENDING) |
Test(c,8,ASCENDING) |
||
Test(d,12,ASCENDING) |
Test(d,12,ASCENDING) |
||
RETURN</ |
RETURN</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Sort_an_integer_array.png Screenshot from Atari 8-bit computer] |
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Sort_an_integer_array.png Screenshot from Atari 8-bit computer] |
||
Line 281: | Line 281: | ||
=={{header|ActionScript}}== |
=={{header|ActionScript}}== |
||
< |
<syntaxhighlight lang="actionscript">//Comparison function must returns Numbers even though it deals with integers. |
||
function compare(x:int, y:int):Number |
function compare(x:int, y:int):Number |
||
{ |
{ |
||
Line 287: | Line 287: | ||
} |
} |
||
var nums:Vector.<int> = Vector.<int>([5,12,3,612,31,523,1,234,2]); |
var nums:Vector.<int> = Vector.<int>([5,12,3,612,31,523,1,234,2]); |
||
nums.sort(compare);</ |
nums.sort(compare);</syntaxhighlight> |
||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
{{works with|GNAT|GPL 2006}} |
{{works with|GNAT|GPL 2006}} |
||
< |
<syntaxhighlight lang="ada">with Gnat.Heap_Sort_G; |
||
procedure Integer_Sort is |
procedure Integer_Sort is |
||
Line 333: | Line 333: | ||
begin |
begin |
||
Sort(Values); |
Sort(Values); |
||
end Integer_Sort;</ |
end Integer_Sort;</syntaxhighlight> |
||
=={{header|ALGOL 68}}== |
=={{header|ALGOL 68}}== |
||
Line 341: | Line 341: | ||
{{works with|ALGOL 68G|Any - tested with release mk15-0.8b.fc9.i386}} |
{{works with|ALGOL 68G|Any - tested with release mk15-0.8b.fc9.i386}} |
||
{{works with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release 1.8.8d.fc9.i386}} |
{{works with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release 1.8.8d.fc9.i386}} |
||
< |
<syntaxhighlight lang="algol68">CO PR READ "shell_sort.a68" PR CO |
||
MODE TYPE = INT; |
MODE TYPE = INT; |
||
Line 364: | Line 364: | ||
in place shell sort(LOC[LWB seq: UPB seq]TYPE:=seq); |
in place shell sort(LOC[LWB seq: UPB seq]TYPE:=seq); |
||
print((shell sort((2, 4, 3, 1, 2)), new line))</ |
print((shell sort((2, 4, 3, 1, 2)), new line))</syntaxhighlight> |
||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 372: | Line 372: | ||
=={{header|ALGOL W}}== |
=={{header|ALGOL W}}== |
||
Algol W doesn't have standard sorting facilities. This uses the Algol W quicksort sample in the Sorting Algorithms Quicksort task. |
Algol W doesn't have standard sorting facilities. This uses the Algol W quicksort sample in the Sorting Algorithms Quicksort task. |
||
< |
<syntaxhighlight lang="algolw">begin |
||
% use the quicksort procedure from the Sorting_Algorithms/Quicksort task % |
% use the quicksort procedure from the Sorting_Algorithms/Quicksort task % |
||
% Quicksorts in-place the array of integers v, from lb to ub - external % |
% Quicksorts in-place the array of integers v, from lb to ub - external % |
||
Line 387: | Line 387: | ||
for i := 1 until 5 do writeon( i_w := 1, s_w := 1, t( i ) ) |
for i := 1 until 5 do writeon( i_w := 1, s_w := 1, t( i ) ) |
||
end |
end |
||
end.</ |
end.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 395: | Line 395: | ||
=={{header|APL}}== |
=={{header|APL}}== |
||
{{works with|APL2}} |
{{works with|APL2}} |
||
< |
<syntaxhighlight lang="apl"> X←63 92 51 92 39 15 43 89 36 69 |
||
X[⍋X] |
X[⍋X] |
||
15 36 39 43 51 63 69 89 92 92</ |
15 36 39 43 51 63 69 89 92 92</syntaxhighlight> |
||
=={{header|AppleScript}}== |
=={{header|AppleScript}}== |
||
Line 409: | Line 409: | ||
for which AppleScript was presumably designed. |
for which AppleScript was presumably designed. |
||
< |
<syntaxhighlight lang="applescript">use framework "Foundation" |
||
-- sort :: [a] -> [a] |
-- sort :: [a] -> [a] |
||
Line 451: | Line 451: | ||
end script |
end script |
||
end if |
end if |
||
end mReturn</ |
end mReturn</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>{{0, 1, 2, 3, 4, 5, 6, 7, 8, 8, 9}, |
<pre>{{0, 1, 2, 3, 4, 5, 6, 7, 8, 8, 9}, |
||
Line 458: | Line 458: | ||
=={{header|ARM Assembly}}== |
=={{header|ARM Assembly}}== |
||
{{works with|as|Raspberry Pi}} |
{{works with|as|Raspberry Pi}} |
||
<syntaxhighlight lang="arm assembly"> |
|||
<lang ARM Assembly> |
|||
/* ARM assembly Raspberry PI */ |
/* ARM assembly Raspberry PI */ |
||
Line 616: | Line 616: | ||
/***************************************************/ |
/***************************************************/ |
||
.include "../affichage.inc" |
.include "../affichage.inc" |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Arturo}}== |
=={{header|Arturo}}== |
||
< |
<syntaxhighlight lang="rebol">arr: [2 3 5 8 4 1 6 9 7] |
||
sort 'arr ; in-place |
sort 'arr ; in-place |
||
loop arr => print</ |
loop arr => print</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 637: | Line 637: | ||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
< |
<syntaxhighlight lang="autohotkey">numbers = 5 4 1 2 3 |
||
sort, numbers, N D%A_Space% |
sort, numbers, N D%A_Space% |
||
Msgbox % numbers</ |
Msgbox % numbers</syntaxhighlight> |
||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
<syntaxhighlight lang="awk"> |
|||
<lang AWK> |
|||
# syntax: GAWK -f SORT_AN_INTEGER_ARRAY.AWK |
# syntax: GAWK -f SORT_AN_INTEGER_ARRAY.AWK |
||
BEGIN { |
BEGIN { |
||
Line 658: | Line 658: | ||
printf("\t%s\n",description) |
printf("\t%s\n",description) |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
<p>output:</p> |
<p>output:</p> |
||
<pre> |
<pre> |
||
Line 669: | Line 669: | ||
There is no ascending sort function in Axe, but there is a descending sort function. One can either implement a custom ascending sorting function or simply reverse the output from SortD. |
There is no ascending sort function in Axe, but there is a descending sort function. One can either implement a custom ascending sorting function or simply reverse the output from SortD. |
||
< |
<syntaxhighlight lang="axe">2→{L₁} |
||
4→{L₁+1} |
4→{L₁+1} |
||
3→{L₁+2} |
3→{L₁+2} |
||
Line 675: | Line 675: | ||
2→{L₁+4} |
2→{L₁+4} |
||
SortD(L₁,5)</ |
SortD(L₁,5)</syntaxhighlight> |
||
=={{header|Babel}}== |
=={{header|Babel}}== |
||
Line 681: | Line 681: | ||
Use the sortval operator to sort an array of integers (val-array in Babel terminology). The following code creates a list of random values, converts it to a val-array, sorts that val-array, then converts it back to a list for display using the lsnum utility. |
Use the sortval operator to sort an array of integers (val-array in Babel terminology). The following code creates a list of random values, converts it to a val-array, sorts that val-array, then converts it back to a list for display using the lsnum utility. |
||
< |
<syntaxhighlight lang="babel">babel> nil { zap {1 randlf 100 rem} 20 times collect ! } nest dup lsnum ! --> Create a list of random numbers |
||
( 20 47 69 71 18 10 92 9 56 68 71 92 45 92 12 7 59 55 54 24 ) |
( 20 47 69 71 18 10 92 9 56 68 71 92 45 92 12 7 59 55 54 24 ) |
||
babel> ls2lf --> Convert list to array for sorting |
babel> ls2lf --> Convert list to array for sorting |
||
babel> dup {fnord} merge_sort --> The internal sort operator |
babel> dup {fnord} merge_sort --> The internal sort operator |
||
babel> ar2ls lsnum ! --> Display the results |
babel> ar2ls lsnum ! --> Display the results |
||
( 7 9 10 12 18 20 24 45 47 54 55 56 59 68 69 71 71 92 92 92 )</ |
( 7 9 10 12 18 20 24 45 47 54 55 56 59 68 69 71 71 92 92 92 )</syntaxhighlight> |
||
In Babel, lists and arrays are distinct. If you want to sort a list, use the lssort utility: |
In Babel, lists and arrays are distinct. If you want to sort a list, use the lssort utility: |
||
< |
<syntaxhighlight lang="babel">babel> ( 68 73 63 83 54 67 46 53 88 86 49 75 89 83 28 9 34 21 20 90 ) |
||
babel> {lt?} lssort ! lsnum ! |
babel> {lt?} lssort ! lsnum ! |
||
( 9 20 21 28 34 46 49 53 54 63 67 68 73 75 83 83 86 88 89 90 )</ |
( 9 20 21 28 34 46 49 53 54 63 67 68 73 75 83 83 86 88 89 90 )</syntaxhighlight> |
||
To reverse the sort-order, use the 'gt?' predicate instead of the 'lt?' predicate: |
To reverse the sort-order, use the 'gt?' predicate instead of the 'lt?' predicate: |
||
< |
<syntaxhighlight lang="babel">babel> ( 68 73 63 83 54 67 46 53 88 86 49 75 89 83 28 9 34 21 20 90 ) {gt?} lssort ! lsnum ! |
||
( 90 89 88 86 83 83 75 73 68 67 63 54 53 49 46 34 28 21 20 9 )</ |
( 90 89 88 86 83 83 75 73 68 67 63 54 53 49 46 34 28 21 20 9 )</syntaxhighlight> |
||
=={{header|BaCon}}== |
=={{header|BaCon}}== |
||
< |
<syntaxhighlight lang="freebasic">' Sort an integer array |
||
DECLARE values[5] TYPE NUMBER |
DECLARE values[5] TYPE NUMBER |
||
values[0] = 23 |
values[0] = 23 |
||
Line 713: | Line 713: | ||
PRINT values[i], ", "; |
PRINT values[i], ", "; |
||
NEXT |
NEXT |
||
PRINT values[4]</ |
PRINT values[4]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 723: | Line 723: | ||
{{works with|BBC BASIC for Windows}} |
{{works with|BBC BASIC for Windows}} |
||
Uses the supplied SORTLIB library. |
Uses the supplied SORTLIB library. |
||
< |
<syntaxhighlight lang="bbcbasic"> INSTALL @lib$+"SORTLIB" |
||
sort% = FN_sortinit(0,0) |
sort% = FN_sortinit(0,0) |
||
Line 735: | Line 735: | ||
PRINT ; array(i%) ", "; |
PRINT ; array(i%) ", "; |
||
NEXT |
NEXT |
||
PRINT ; array(i%)</ |
PRINT ; array(i%)</syntaxhighlight> |
||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 742: | Line 742: | ||
=={{header|Beads}}== |
=={{header|Beads}}== |
||
< |
<syntaxhighlight lang="beads">beads 1 program 'Sort an integer array' |
||
calc main_init |
calc main_init |
||
var arr = [4, 1, 2, -1, 3, 0, 2] |
var arr = [4, 1, 2, -1, 3, 0, 2] |
||
var newarr : array of num |
var newarr : array of num |
||
loop across:arr sort:val count:c val:v |
loop across:arr sort:val count:c val:v |
||
newarr[c] = v</ |
newarr[c] = v</syntaxhighlight> |
||
=={{header|Befunge}}== |
=={{header|Befunge}}== |
||
{{works with|befungee}} |
{{works with|befungee}} |
||
Elements of the array are read from standard input, preceded by their quantity. The algorithm uses counting sort and allows numbers between 1 and 60, inclusive. |
Elements of the array are read from standard input, preceded by their quantity. The algorithm uses counting sort and allows numbers between 1 and 60, inclusive. |
||
<syntaxhighlight lang="befunge">v |
|||
<lang Befunge>v |
|||
> 543** > :#v_ $&> :#v_ 1 > :0g > :#v_ $ 1+: 543** `! #v_ 25*,@ |
> 543** > :#v_ $&> :#v_ 1 > :0g > :#v_ $ 1+: 543** `! #v_ 25*,@ |
||
^-1p0\0:< ^-1 p0\+1 g0:&< ^-1\.:\< |
^-1p0\0:< ^-1 p0\+1 g0:&< ^-1\.:\< |
||
^ <</ |
^ <</syntaxhighlight> |
||
=={{header|Bracmat}}== |
=={{header|Bracmat}}== |
||
Line 769: | Line 769: | ||
{!} 21</pre> |
{!} 21</pre> |
||
To complete the task need to unfold the terms with a numerical factor >1: |
To complete the task need to unfold the terms with a numerical factor >1: |
||
< |
<syntaxhighlight lang="bracmat">{sort takes a list of space-separated integers} |
||
(sort= |
(sort= |
||
sum elem sorted n |
sum elem sorted n |
||
Line 785: | Line 785: | ||
& !sorted); |
& !sorted); |
||
out$sort$(9 -2 1 2 8 0 1 2);</ |
out$sort$(9 -2 1 2 8 0 1 2);</syntaxhighlight> |
||
Output: |
Output: |
||
<pre>-2 0 1 1 2 2 8 9</pre> |
<pre>-2 0 1 1 2 2 8 9</pre> |
||
Line 792: | Line 792: | ||
=={{header|Burlesque}}== |
=={{header|Burlesque}}== |
||
< |
<syntaxhighlight lang="burlesque">{1 3 2 5 4}><</syntaxhighlight> |
||
=={{header|C}}== |
=={{header|C}}== |
||
< |
<syntaxhighlight lang="c">#include <stdlib.h> /* qsort() */ |
||
#include <stdio.h> /* printf() */ |
#include <stdio.h> /* printf() */ |
||
Line 811: | Line 811: | ||
nums[0], nums[1], nums[2], nums[3], nums[4]); |
nums[0], nums[1], nums[2], nums[3], nums[4]); |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
''Caution:'' An older version of <tt>intcmp()</tt> did <tt>return *a - *b</tt>. This is only correct when the subtraction does not overflow. Suppose that <tt>*a = 2000000000</tt> and <tt>*b = -2000000000</tt> on a machine with 32-bit <tt>int</tt>. The subtraction <tt>*a - *b</tt> would overflow to <tt>-294967296</tt>, and <tt>intcmp()</tt> would believe <tt>*a < *b</tt>, but the correct answer is <tt>*a > *b</tt>. |
''Caution:'' An older version of <tt>intcmp()</tt> did <tt>return *a - *b</tt>. This is only correct when the subtraction does not overflow. Suppose that <tt>*a = 2000000000</tt> and <tt>*b = -2000000000</tt> on a machine with 32-bit <tt>int</tt>. The subtraction <tt>*a - *b</tt> would overflow to <tt>-294967296</tt>, and <tt>intcmp()</tt> would believe <tt>*a < *b</tt>, but the correct answer is <tt>*a > *b</tt>. |
||
Line 817: | Line 817: | ||
=={{header|C sharp|C#}}== |
=={{header|C sharp|C#}}== |
||
< |
<syntaxhighlight lang="csharp">using System; |
||
using System.Collections.Generic; |
using System.Collections.Generic; |
||
Line 825: | Line 825: | ||
Array.Sort(unsorted); |
Array.Sort(unsorted); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|C++}}== |
=={{header|C++}}== |
||
Line 831: | Line 831: | ||
===Simple Array=== |
===Simple Array=== |
||
< |
<syntaxhighlight lang="cpp">#include <algorithm> |
||
int main() |
int main() |
||
Line 838: | Line 838: | ||
std::sort(nums, nums+sizeof(nums)/sizeof(int)); |
std::sort(nums, nums+sizeof(nums)/sizeof(int)); |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
===<tt>std::vector</tt>=== |
===<tt>std::vector</tt>=== |
||
< |
<syntaxhighlight lang="cpp">#include <algorithm> |
||
#include <vector> |
#include <vector> |
||
Line 854: | Line 854: | ||
std::sort(nums.begin(), nums.end()); |
std::sort(nums.begin(), nums.end()); |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
===<tt>std::list</tt>=== |
===<tt>std::list</tt>=== |
||
< |
<syntaxhighlight lang="cpp">#include <list> |
||
int main() |
int main() |
||
Line 869: | Line 869: | ||
nums.sort(); |
nums.sort(); |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Clean}}== |
=={{header|Clean}}== |
||
We use list and array comprehensions to convert an array to and from a list in order to use the built-in <tt>sort</tt> on lists. |
We use list and array comprehensions to convert an array to and from a list in order to use the built-in <tt>sort</tt> on lists. |
||
< |
<syntaxhighlight lang="clean">import StdEnv |
||
sortArray :: (a e) -> a e | Array a e & Ord e |
sortArray :: (a e) -> a e | Array a e & Ord e |
||
Line 879: | Line 879: | ||
Start :: {#Int} |
Start :: {#Int} |
||
Start = sortArray {2, 4, 3, 1, 2}</ |
Start = sortArray {2, 4, 3, 1, 2}</syntaxhighlight> |
||
=={{header|Clojure}}== |
=={{header|Clojure}}== |
||
< |
<syntaxhighlight lang="clojure">(sort [5 4 3 2 1]) ; sort can also take a comparator function |
||
(1 2 3 4 5)</ |
(1 2 3 4 5)</syntaxhighlight> |
||
=={{header|COBOL}}== |
=={{header|COBOL}}== |
||
{{works with|Visual COBOL}} |
{{works with|Visual COBOL}} |
||
< |
<syntaxhighlight lang="cobol"> PROGRAM-ID. sort-ints. |
||
DATA DIVISION. |
DATA DIVISION. |
||
Line 908: | Line 908: | ||
END-PERFORM |
END-PERFORM |
||
DISPLAY SPACE |
DISPLAY SPACE |
||
.</ |
.</syntaxhighlight> |
||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
In Common Lisp, the ''sort'' function takes a predicate that is used as the comparator. This parameter can be any two-argument function. To sort a sequence (list or array) of integers, call ''sort'' with the < operator as the predicate: |
In Common Lisp, the ''sort'' function takes a predicate that is used as the comparator. This parameter can be any two-argument function. To sort a sequence (list or array) of integers, call ''sort'' with the < operator as the predicate: |
||
< |
<syntaxhighlight lang="lisp">CL-USER> (sort #(9 -2 1 2 8 0 1 2) #'<) |
||
#(-2 0 1 1 2 2 8 9)</ |
#(-2 0 1 1 2 2 8 9)</syntaxhighlight> |
||
=={{header|Crystal}}== |
=={{header|Crystal}}== |
||
Example demonstrating the support for copy sort and in-place sort (like Ruby) |
Example demonstrating the support for copy sort and in-place sort (like Ruby) |
||
<syntaxhighlight lang="ruby"> |
|||
<lang Ruby> |
|||
a = [5, 4, 3, 2, 1] |
a = [5, 4, 3, 2, 1] |
||
puts a.sort |
puts a.sort |
||
Line 928: | Line 928: | ||
puts a |
puts a |
||
# => [1, 2, 3, 4, 5] |
# => [1, 2, 3, 4, 5] |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|D}}== |
=={{header|D}}== |
||
< |
<syntaxhighlight lang="d">import std.stdio, std.algorithm; |
||
void main() { |
void main() { |
||
Line 937: | Line 937: | ||
data.sort(); // in-place |
data.sort(); // in-place |
||
assert(data == [1, 2, 2, 3, 4]); |
assert(data == [1, 2, 2, 3, 4]); |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Delphi}}== |
=={{header|Delphi}}== |
||
< |
<syntaxhighlight lang="delphi">uses Types, Generics.Collections; |
||
var |
var |
||
Line 947: | Line 947: | ||
a := TIntegerDynArray.Create(5, 4, 3, 2, 1); |
a := TIntegerDynArray.Create(5, 4, 3, 2, 1); |
||
TArray.Sort<Integer>(a); |
TArray.Sort<Integer>(a); |
||
end;</ |
end;</syntaxhighlight> |
||
=={{header|DWScript}}== |
=={{header|DWScript}}== |
||
< |
<syntaxhighlight lang="delphi">var a : array of Integer := [5, 4, 3, 2, 1]; |
||
a.Sort; // ascending natural sort |
a.Sort; // ascending natural sort |
||
PrintLn(a.Map(IntToStr).Join(',')); // 1,2,3,4,5</ |
PrintLn(a.Map(IntToStr).Join(',')); // 1,2,3,4,5</syntaxhighlight> |
||
=={{header|Déjà Vu}}== |
=={{header|Déjà Vu}}== |
||
< |
<syntaxhighlight lang="dejavu">!. sort [ 5 4 3 2 1 ]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>[ 1 2 3 4 5 ]</pre> |
<pre>[ 1 2 3 4 5 ]</pre> |
||
=={{header|E}}== |
=={{header|E}}== |
||
< |
<syntaxhighlight lang="e">[2,4,3,1,2].sort()</syntaxhighlight> |
||
=={{header|EGL}}== |
=={{header|EGL}}== |
||
{{works with|EDT}} |
{{works with|EDT}} |
||
The following works in EDT with Rich UI and stand-alone programs. |
The following works in EDT with Rich UI and stand-alone programs. |
||
< |
<syntaxhighlight lang="egl">program SortExample |
||
function main() |
function main() |
||
Line 980: | Line 980: | ||
end |
end |
||
end</ |
end</syntaxhighlight> |
||
{{works with|RBD}} |
{{works with|RBD}} |
||
The following works in RBD but only with Rich UI programs. |
The following works in RBD but only with Rich UI programs. |
||
< |
<syntaxhighlight lang="egl">test1 int[] = [1,-1,8,-8,2,-2,7,-7,3,-3,6,-6,9,-9,4,-4,5,-5,0]; |
||
RUILib.sort(test1, sortFunction); |
RUILib.sort(test1, sortFunction); |
||
Line 989: | Line 989: | ||
function sortFunction(a any in, b any in) returns (int) |
function sortFunction(a any in, b any in) returns (int) |
||
return ((a as int) - (b as int)); |
return ((a as int) - (b as int)); |
||
end</ |
end</syntaxhighlight> |
||
=={{header|Eiffel}}== |
=={{header|Eiffel}}== |
||
Line 998: | Line 998: | ||
The list can be easily sorted in reverse. There is a call for `sort' to manually initiate sorting. |
The list can be easily sorted in reverse. There is a call for `sort' to manually initiate sorting. |
||
< |
<syntaxhighlight lang="eiffel"> |
||
local |
local |
||
l_array: SORTED_TWO_WAY_LIST [INTEGER] |
l_array: SORTED_TWO_WAY_LIST [INTEGER] |
||
Line 1,004: | Line 1,004: | ||
create l_array.make_from_iterable (<<9,8,7,6,5,4,3,2,1,0>>) |
create l_array.make_from_iterable (<<9,8,7,6,5,4,3,2,1,0>>) |
||
end |
end |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Elena}}== |
=={{header|Elena}}== |
||
ELENA 5.0 : |
ELENA 5.0 : |
||
< |
<syntaxhighlight lang="elena">import system'routines; |
||
import extensions; |
import extensions; |
||
Line 1,016: | Line 1,016: | ||
console.printLine(unsorted.clone().sort(ifOrdered).asEnumerable()) |
console.printLine(unsorted.clone().sort(ifOrdered).asEnumerable()) |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Elixir}}== |
=={{header|Elixir}}== |
||
< |
<syntaxhighlight lang="elixir">list = [2, 4, 3, 1, 2] |
||
IO.inspect Enum.sort(list) |
IO.inspect Enum.sort(list) |
||
IO.inspect Enum.sort(list, &(&1>&2))</ |
IO.inspect Enum.sort(list, &(&1>&2))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,030: | Line 1,030: | ||
=={{header|Erlang}}== |
=={{header|Erlang}}== |
||
< |
<syntaxhighlight lang="erlang">List = [2, 4, 3, 1, 2]. |
||
SortedList = lists:sort(List).</ |
SortedList = lists:sort(List).</syntaxhighlight> |
||
=={{header|Euphoria}}== |
=={{header|Euphoria}}== |
||
< |
<syntaxhighlight lang="euphoria">include sort.e |
||
print(1,sort({20, 7, 65, 10, 3, 0, 8, -60}))</ |
print(1,sort({20, 7, 65, 10, 3, 0, 8, -60}))</syntaxhighlight> |
||
=={{header|F_Sharp|F#}}== |
=={{header|F_Sharp|F#}}== |
||
< |
<syntaxhighlight lang="fsharp">// sorting an array in place |
||
let nums = [| 2; 4; 3; 1; 2 |] |
let nums = [| 2; 4; 3; 1; 2 |] |
||
Array.sortInPlace nums |
Array.sortInPlace nums |
||
Line 1,044: | Line 1,044: | ||
// create a sorted copy of a list |
// create a sorted copy of a list |
||
let nums2 = [2; 4; 3; 1; 2] |
let nums2 = [2; 4; 3; 1; 2] |
||
let sorted = List.sort nums2</ |
let sorted = List.sort nums2</syntaxhighlight> |
||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
< |
<syntaxhighlight lang="factor">{ 1 4 9 2 3 0 5 } natural-sort .</syntaxhighlight> |
||
=={{header|Fantom}}== |
=={{header|Fantom}}== |
||
Line 1,065: | Line 1,065: | ||
{{works with|Win32Forth|4.2}} |
{{works with|Win32Forth|4.2}} |
||
===Win32Forth=== |
===Win32Forth=== |
||
< |
<syntaxhighlight lang="forth">create test-data 2 , 4 , 3 , 1 , 2 , |
||
test-data 5 cell-sort |
test-data 5 cell-sort |
||
</syntaxhighlight> |
|||
</lang> |
|||
===ANS/ISO Forth=== |
===ANS/ISO Forth=== |
||
{{works with|GForth}} |
{{works with|GForth}} |
||
Line 1,073: | Line 1,073: | ||
Standard Forth does not have a library sort |
Standard Forth does not have a library sort |
||
< |
<syntaxhighlight lang="forth">100000 CONSTANT SIZE |
||
CREATE MYARRAY SIZE CELLS ALLOT |
CREATE MYARRAY SIZE CELLS ALLOT |
||
Line 1,115: | Line 1,115: | ||
DUP 2 < IF 2DROP EXIT THEN 1- CELLS OVER + QSORT ;</LANG> |
DUP 2 < IF 2DROP EXIT THEN 1- CELLS OVER + QSORT ;</LANG> |
||
Test at the console |
Test at the console |
||
< |
<syntaxhighlight lang="forth">FILLIT ok |
||
MYARRAY SIZE QUICKSORT ok</ |
MYARRAY SIZE QUICKSORT ok</syntaxhighlight> |
||
=={{header|Fortran}}== |
=={{header|Fortran}}== |
||
{{works with|Silverfrost FTN95}} |
{{works with|Silverfrost FTN95}} |
||
< |
<syntaxhighlight lang="fortran">CALL ISORT@(b, a, n) |
||
! n = number of elements |
! n = number of elements |
||
! a = array to be sorted |
! a = array to be sorted |
||
! b = array of indices of a. b(1) 'points' to the minimum value etc.</ |
! b = array of indices of a. b(1) 'points' to the minimum value etc.</syntaxhighlight> |
||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
Qsort is not buildin, but include in the compiler package. |
Qsort is not buildin, but include in the compiler package. |
||
< |
<syntaxhighlight lang="freebasic">' version 11-03-2016 |
||
' compile with: fbc -s console |
' compile with: fbc -s console |
||
Line 1,184: | Line 1,184: | ||
Print : Print "hit any key to end program" |
Print : Print "hit any key to end program" |
||
Sleep |
Sleep |
||
End</ |
End</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>unsorted array |
<pre>unsorted array |
||
Line 1,194: | Line 1,194: | ||
=={{header|Frink}}== |
=={{header|Frink}}== |
||
The following sorts an array in-place. |
The following sorts an array in-place. |
||
< |
<syntaxhighlight lang="frink">a = [5, 2, 4, 1, 6, 7, 9, 3, 8, 0] |
||
sort[a]</ |
sort[a]</syntaxhighlight> |
||
=={{header|FunL}}== |
=={{header|FunL}}== |
||
< |
<syntaxhighlight lang="funl">nums = [5, 2, 78, 2, 578, -42] |
||
println( sort(nums) ) // sort in ascending order |
println( sort(nums) ) // sort in ascending order |
||
println( nums.sortWith((>)) ) // sort in descending order</ |
println( nums.sortWith((>)) ) // sort in descending order</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,219: | Line 1,219: | ||
=={{header|Gambas}}== |
=={{header|Gambas}}== |
||
'''[https://gambas-playground.proko.eu/?gist=1f1d244aa95c329eb87cb538f0d5fc4a Click this link to run this code]''' |
'''[https://gambas-playground.proko.eu/?gist=1f1d244aa95c329eb87cb538f0d5fc4a Click this link to run this code]''' |
||
< |
<syntaxhighlight lang="gambas">Public Sub Main() |
||
Dim iArray As Integer[] = [8, 2, 5, 9, 1, 3, 6, 7, 4] |
Dim iArray As Integer[] = [8, 2, 5, 9, 1, 3, 6, 7, 4] |
||
Dim iTemp As Integer |
Dim iTemp As Integer |
||
Line 1,230: | Line 1,230: | ||
Print Left(sOutput, -2) |
Print Left(sOutput, -2) |
||
End</ |
End</syntaxhighlight> |
||
Output: |
Output: |
||
Line 1,238: | Line 1,238: | ||
=={{header|GAP}}== |
=={{header|GAP}}== |
||
< |
<syntaxhighlight lang="gap">a := [ 8, 2, 5, 9, 1, 3, 6, 7, 4 ]; |
||
# Make a copy (with "b := a;", b and a would point to the same list) |
# Make a copy (with "b := a;", b and a would point to the same list) |
||
b := ShallowCopy(a); |
b := ShallowCopy(a); |
||
Line 1,251: | Line 1,251: | ||
# [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ] |
# [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ] |
||
b; |
b; |
||
# [ 8, 2, 5, 9, 1, 3, 6, 7, 4 ]</ |
# [ 8, 2, 5, 9, 1, 3, 6, 7, 4 ]</syntaxhighlight> |
||
=={{header|Go}}== |
=={{header|Go}}== |
||
< |
<syntaxhighlight lang="go">package main |
||
import "fmt" |
import "fmt" |
||
import "sort" |
import "sort" |
||
Line 1,262: | Line 1,262: | ||
sort.Ints(nums) |
sort.Ints(nums) |
||
fmt.Println(nums) |
fmt.Println(nums) |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Golfscript}}== |
=={{header|Golfscript}}== |
||
< |
<syntaxhighlight lang="golfscript">[2 4 3 1 2]$</syntaxhighlight> |
||
=={{header|Groovy}}== |
=={{header|Groovy}}== |
||
< |
<syntaxhighlight lang="groovy">println ([2,4,0,3,1,2,-12].sort())</syntaxhighlight> |
||
Output: |
Output: |
||
Line 1,276: | Line 1,276: | ||
{{works with|GHC|GHCi|6.6}} |
{{works with|GHC|GHCi|6.6}} |
||
< |
<syntaxhighlight lang="haskell">nums = [2,4,3,1,2] :: [Int] |
||
sorted = List.sort nums</ |
sorted = List.sort nums</syntaxhighlight> |
||
=={{header|HicEst}}== |
=={{header|HicEst}}== |
||
< |
<syntaxhighlight lang="hicest">DIMENSION array(100) |
||
array = INT( RAN(100) ) |
array = INT( RAN(100) ) |
||
SORT(Vector=array, Sorted=array) </ |
SORT(Vector=array, Sorted=array) </syntaxhighlight> |
||
=={{header|Huginn}}== |
=={{header|Huginn}}== |
||
< |
<syntaxhighlight lang="huginn">main() { |
||
nums = [2, 4, 3, 1, 2]; |
nums = [2, 4, 3, 1, 2]; |
||
nums.sort(); |
nums.sort(); |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Icon}} and {{header|Unicon}}== |
=={{header|Icon}} and {{header|Unicon}}== |
||
Line 1,295: | Line 1,295: | ||
In the example below, L will remain an unsorted list and S will be sorted. |
In the example below, L will remain an unsorted list and S will be sorted. |
||
< |
<syntaxhighlight lang="icon">S := sort(L:= [63, 92, 51, 92, 39, 15, 43, 89, 36, 69]) # will sort a list</syntaxhighlight> |
||
=={{header|IDL}}== |
=={{header|IDL}}== |
||
< |
<syntaxhighlight lang="idl">result = array[sort(array)]</syntaxhighlight> |
||
=={{header|Inform 7}}== |
=={{header|Inform 7}}== |
||
< |
<syntaxhighlight lang="inform7">let L be {5, 4, 7, 1, 18}; |
||
sort L;</ |
sort L;</syntaxhighlight> |
||
=={{header|Io}}== |
=={{header|Io}}== |
||
< |
<syntaxhighlight lang="lua">mums := list(2,4,3,1,2) |
||
sorted := nums sort # returns a new sorted array. 'nums' is unchanged |
sorted := nums sort # returns a new sorted array. 'nums' is unchanged |
||
nums sortInPlace # sort 'nums' "in-place"</ |
nums sortInPlace # sort 'nums' "in-place"</syntaxhighlight> |
||
=={{header|J}}== |
=={{header|J}}== |
||
<lang |
<syntaxhighlight lang="j">/:~</syntaxhighlight> |
||
The verb<tt> /:~ </tt>sorts <i>anything</i> that J can represent. For example: |
The verb<tt> /:~ </tt>sorts <i>anything</i> that J can represent. For example: |
||
< |
<syntaxhighlight lang="j"> ] a=: 10 ?@$ 100 NB. random vector |
||
63 92 51 92 39 15 43 89 36 69 |
63 92 51 92 39 15 43 89 36 69 |
||
/:~ a |
/:~ a |
||
15 36 39 43 51 63 69 89 92 92</ |
15 36 39 43 51 63 69 89 92 92</syntaxhighlight> |
||
Arrays of any rank are treated as lists of component arrays. Thus <tt>/:~</tt> sorts not only atoms within a list, but whole lists within a table, tables within a three-axis array, and so on. The level of structure at which sorting occurs may also be specified, so that <tt>/:~"1</tt> sorts the atoms within the finest-grained list within the array, regardless of the overall rank of the array. See the [[j:Essays/The_TAO_of_J|Total Array Ordering essay]] on the JWiki for more details. |
Arrays of any rank are treated as lists of component arrays. Thus <tt>/:~</tt> sorts not only atoms within a list, but whole lists within a table, tables within a three-axis array, and so on. The level of structure at which sorting occurs may also be specified, so that <tt>/:~"1</tt> sorts the atoms within the finest-grained list within the array, regardless of the overall rank of the array. See the [[j:Essays/The_TAO_of_J|Total Array Ordering essay]] on the JWiki for more details. |
||
Line 1,323: | Line 1,323: | ||
=={{header|Java}}== |
=={{header|Java}}== |
||
===Array=== |
===Array=== |
||
< |
<syntaxhighlight lang="java">import java.util.Arrays; |
||
public class Example { |
public class Example { |
||
Line 1,331: | Line 1,331: | ||
Arrays.sort(nums); |
Arrays.sort(nums); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
===List=== |
===List=== |
||
{{works with|Java|1.5+}} |
{{works with|Java|1.5+}} |
||
< |
<syntaxhighlight lang="java5">import java.util.Arrays; |
||
import java.util.Collections; |
import java.util.Collections; |
||
import java.util.List; |
import java.util.List; |
||
Line 1,345: | Line 1,345: | ||
Collections.sort(nums); |
Collections.sort(nums); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|JavaScript}}== |
=={{header|JavaScript}}== |
||
Line 1,352: | Line 1,352: | ||
JavaScript sorts lexically by default, so "10000" comes before "2". To sort numerically, a custom comparator is used. |
JavaScript sorts lexically by default, so "10000" comes before "2". To sort numerically, a custom comparator is used. |
||
< |
<syntaxhighlight lang="javascript">function int_arr(a, b) { |
||
return a - b; |
return a - b; |
||
} |
} |
||
var numbers = [20, 7, 65, 10, 3, 0, 8, -60]; |
var numbers = [20, 7, 65, 10, 3, 0, 8, -60]; |
||
numbers.sort(int_arr); |
numbers.sort(int_arr); |
||
document.write(numbers);</ |
document.write(numbers);</syntaxhighlight> |
||
=={{header|Jinja}}== |
=={{header|Jinja}}== |
||
< |
<syntaxhighlight lang="jinja"> |
||
from jinja2 import Template |
from jinja2 import Template |
||
print(Template("{{ [53, 17, 42, 61, 35] | sort }}").render()) |
print(Template("{{ [53, 17, 42, 61, 35] | sort }}").render()) |
||
</syntaxhighlight> |
|||
</lang> |
|||
Descending order: |
Descending order: |
||
< |
<syntaxhighlight lang="jinja"> |
||
from jinja2 import Template |
from jinja2 import Template |
||
print(Template("{{ [53, 17, 42, 61, 35] | sort(reverse=true) }}").render()) |
print(Template("{{ [53, 17, 42, 61, 35] | sort(reverse=true) }}").render()) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|jq}}== |
=={{header|jq}}== |
||
jq's builtin <code>sort</code> filter sorts the elements of an array in ascending order: |
jq's builtin <code>sort</code> filter sorts the elements of an array in ascending order: |
||
< |
<syntaxhighlight lang="jq">[2,1,3] | sort # => [1,2,3]</syntaxhighlight> |
||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
Julia has both out-of-place (<code>sort</code>) and in-place (<code>sort!</code>) sorting functions in its standard-library: |
Julia has both out-of-place (<code>sort</code>) and in-place (<code>sort!</code>) sorting functions in its standard-library: |
||
< |
<syntaxhighlight lang="julia">julia> a = [4,2,3,1] |
||
4-element Int32 Array: |
4-element Int32 Array: |
||
4 |
4 |
||
Line 1,408: | Line 1,408: | ||
2 |
2 |
||
3 |
3 |
||
4</ |
4</syntaxhighlight> |
||
=={{header|K}}== |
=={{header|K}}== |
||
< |
<syntaxhighlight lang="k"> num: -10?10 / Integers from 0 to 9 in random order |
||
5 9 4 2 0 3 6 1 8 7 |
5 9 4 2 0 3 6 1 8 7 |
||
srt: {x@<x} / Generalized sort ascending |
srt: {x@<x} / Generalized sort ascending |
||
srt num |
srt num |
||
0 1 2 3 4 5 6 7 8 9</ |
0 1 2 3 4 5 6 7 8 9</syntaxhighlight> |
||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
< |
<syntaxhighlight lang="scala">// version 1.0.6 |
||
fun main(args: Array<String>) { |
fun main(args: Array<String>) { |
||
Line 1,425: | Line 1,425: | ||
ints.sort() |
ints.sort() |
||
println(ints.joinToString(prefix = "[", postfix = "]")) |
println(ints.joinToString(prefix = "[", postfix = "]")) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,433: | Line 1,433: | ||
=={{header|Lambdatalk}}== |
=={{header|Lambdatalk}}== |
||
< |
<syntaxhighlight lang="scheme"> |
||
1) sorting digits in a number returns a new number of ordered digits |
1) sorting digits in a number returns a new number of ordered digits |
||
{W.sort < 51324} |
{W.sort < 51324} |
||
Line 1,445: | Line 1,445: | ||
{A.sort! < {A.new 51 111 33 2 41}} |
{A.sort! < {A.new 51 111 33 2 41}} |
||
-> [2,33,41,51,111] |
-> [2,33,41,51,111] |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Lasso}}== |
=={{header|Lasso}}== |
||
< |
<syntaxhighlight lang="lasso">local(array) = array(5,20,3,2,6,1,4) |
||
#array->sort |
#array->sort |
||
#array // 1, 2, 3, 4, 5, 6, 20 |
#array // 1, 2, 3, 4, 5, 6, 20 |
||
Line 1,454: | Line 1,454: | ||
// Reverse the sort order |
// Reverse the sort order |
||
#array->sort(false) |
#array->sort(false) |
||
#array // 20, 6, 5, 4, 3, 2, 1</ |
#array // 20, 6, 5, 4, 3, 2, 1</syntaxhighlight> |
||
=={{header|Liberty BASIC}}== |
=={{header|Liberty BASIC}}== |
||
LB has an array-sort command. Parameters are arrayname, start term, finish term. |
LB has an array-sort command. Parameters are arrayname, start term, finish term. |
||
< |
<syntaxhighlight lang="lb">N =20 |
||
dim IntArray( N) |
dim IntArray( N) |
||
Line 1,473: | Line 1,473: | ||
for i =1 to N |
for i =1 to N |
||
print IntArray( i) |
print IntArray( i) |
||
next i</ |
next i</syntaxhighlight> |
||
=={{header|Lingo}}== |
=={{header|Lingo}}== |
||
< |
<syntaxhighlight lang="lingo">l = [7, 4, 23] |
||
l.sort() |
l.sort() |
||
put l |
put l |
||
-- [4, 7, 23]</ |
-- [4, 7, 23]</syntaxhighlight> |
||
=={{header|LiveCode}}== |
=={{header|LiveCode}}== |
||
LiveCode can sort lines or items natively. The delimiter for items can be set to any single character, but defaults to comma. |
LiveCode can sort lines or items natively. The delimiter for items can be set to any single character, but defaults to comma. |
||
< |
<syntaxhighlight lang="livecode">put "3,2,5,4,1" into X |
||
sort items of X numeric |
sort items of X numeric |
||
put X |
put X |
||
-- outputs "1,2,3,4,5"</ |
-- outputs "1,2,3,4,5"</syntaxhighlight> |
||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
< |
<syntaxhighlight lang="lua">t = {4, 5, 2} |
||
table.sort(t) |
table.sort(t) |
||
print(unpack(t))</ |
print(unpack(t))</syntaxhighlight> |
||
=={{header|Maple}}== |
=={{header|Maple}}== |
||
< |
<syntaxhighlight lang="maple">sort([5,7,8,3,6,1]); |
||
sort(Array([5,7,8,3,6,1]))</ |
sort(Array([5,7,8,3,6,1]))</syntaxhighlight> |
||
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
||
< |
<syntaxhighlight lang="mathematica">numbers=Sort[{2,4,3,1,2}]</syntaxhighlight> |
||
=={{header|MATLAB}}== |
=={{header|MATLAB}}== |
||
< |
<syntaxhighlight lang="matlab">a = [4,3,7,-2,9,1]; b = sort(a) % b contains elements of a in ascending order |
||
[b,idx] = sort(a) % b contains a(idx)</ |
[b,idx] = sort(a) % b contains a(idx)</syntaxhighlight> |
||
=={{header|Maxima}}== |
=={{header|Maxima}}== |
||
< |
<syntaxhighlight lang="maxima">sort([9, 4, 3, 7, 6, 1, 10, 2, 8, 5]);</syntaxhighlight> |
||
=={{header|MAXScript}}== |
=={{header|MAXScript}}== |
||
< |
<syntaxhighlight lang="maxscript">arr = #(5, 4, 3, 2, 1) |
||
arr = sort arr</ |
arr = sort arr</syntaxhighlight> |
||
=={{header|Mercury}}== |
=={{header|Mercury}}== |
||
<lang>:- module sort_int_list. |
<syntaxhighlight lang="text">:- module sort_int_list. |
||
:- interface. |
:- interface. |
||
:- import_module io. |
:- import_module io. |
||
Line 1,525: | Line 1,525: | ||
list.sort(Nums, Sorted), |
list.sort(Nums, Sorted), |
||
io.write(Sorted, !IO), |
io.write(Sorted, !IO), |
||
io.nl(!IO).</ |
io.nl(!IO).</syntaxhighlight> |
||
=={{header|min}}== |
=={{header|min}}== |
||
{{works with|min|0.19.3}} |
{{works with|min|0.19.3}} |
||
< |
<syntaxhighlight lang="min">(5 2 1 3 4) '> sort print</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,537: | Line 1,537: | ||
=={{header|Modula-3}}== |
=={{header|Modula-3}}== |
||
Modula-3 provides a generic <tt>ArraySort</tt> module, as well as an instance of that module for integers called <tt>IntArraySort</tt>. |
Modula-3 provides a generic <tt>ArraySort</tt> module, as well as an instance of that module for integers called <tt>IntArraySort</tt>. |
||
< |
<syntaxhighlight lang="modula3">MODULE ArraySort EXPORTS Main; |
||
IMPORT IntArraySort; |
IMPORT IntArraySort; |
||
Line 1,545: | Line 1,545: | ||
BEGIN |
BEGIN |
||
IntArraySort.Sort(arr); |
IntArraySort.Sort(arr); |
||
END ArraySort.</ |
END ArraySort.</syntaxhighlight> |
||
=={{header|MUMPS}}== |
=={{header|MUMPS}}== |
||
< |
<syntaxhighlight lang="mumps">SORTARRAY(X,SEP) |
||
;X is the list of items to sort |
;X is the list of items to sort |
||
;X1 is the temporary array |
;X1 is the temporary array |
||
Line 1,559: | Line 1,559: | ||
SET I="" FOR SET I=$O(X1(I)) Q:I="" SET Y=$SELECT($L(Y)=0:I,1:Y_SEP_I) |
SET I="" FOR SET I=$O(X1(I)) Q:I="" SET Y=$SELECT($L(Y)=0:I,1:Y_SEP_I) |
||
KILL I,X1 |
KILL I,X1 |
||
QUIT Y</ |
QUIT Y</syntaxhighlight> |
||
Output:<pre>USER>W $$SORTARRAY^ROSETTA("3,5,1,99,27,16,0,-1",",") |
Output:<pre>USER>W $$SORTARRAY^ROSETTA("3,5,1,99,27,16,0,-1",",") |
||
-1,0,1,3,5,16,27,99 |
-1,0,1,3,5,16,27,99 |
||
Line 1,566: | Line 1,566: | ||
=={{header|Nanoquery}}== |
=={{header|Nanoquery}}== |
||
'sort' in the Nanoquery standard library has a Quicksort function. |
'sort' in the Nanoquery standard library has a Quicksort function. |
||
< |
<syntaxhighlight lang="nanoquery">% import sort |
||
% println sort({2,4,3,1,2}) |
% println sort({2,4,3,1,2}) |
||
[1, 2, 2, 3, 4]</ |
[1, 2, 2, 3, 4]</syntaxhighlight> |
||
=={{header|Neko}}== |
=={{header|Neko}}== |
||
<syntaxhighlight lang="actionscript">/** |
|||
<lang ActionScript>/** |
|||
<doc><h2>Sort integer array, in Neko</h2> |
<doc><h2>Sort integer array, in Neko</h2> |
||
<p>Array sort function modified from Haxe codegen with -D neko-source</p> |
<p>Array sort function modified from Haxe codegen with -D neko-source</p> |
||
Line 1,610: | Line 1,610: | ||
/* Also returns the sorted array for chaining */ |
/* Also returns the sorted array for chaining */ |
||
$print(sort($array(3,1,4,1,5,9,2,6,5,3,5,8)), "\n")</ |
$print(sort($array(3,1,4,1,5,9,2,6,5,3,5,8)), "\n")</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,620: | Line 1,620: | ||
=={{header|Nemerle}}== |
=={{header|Nemerle}}== |
||
< |
<syntaxhighlight lang="nemerle">using System.Console; |
||
module IntSort |
module IntSort |
||
Line 1,632: | Line 1,632: | ||
WriteLine(sorted); |
WriteLine(sorted); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
Output: |
Output: |
||
<pre>[1, 5, 3, 7, 2, 8, 3, 9] |
<pre>[1, 5, 3, 7, 2, 8, 3, 9] |
||
Line 1,638: | Line 1,638: | ||
=={{header|NetRexx}}== |
=={{header|NetRexx}}== |
||
< |
<syntaxhighlight lang="netrexx">/* NetRexx */ |
||
options replace format comments java crossref savelog symbols binary |
options replace format comments java crossref savelog symbols binary |
||
Line 1,659: | Line 1,659: | ||
say sorted.strip('t') |
say sorted.strip('t') |
||
return</ |
return</syntaxhighlight> |
||
'''Output''' |
'''Output''' |
||
Line 1,669: | Line 1,669: | ||
NetRexx reimplementations of the [[#REXX|Rexx]] samples from below: |
NetRexx reimplementations of the [[#REXX|Rexx]] samples from below: |
||
< |
<syntaxhighlight lang="netrexx">/* NetRexx */ |
||
options replace format comments java crossref savelog symbols |
options replace format comments java crossref savelog symbols |
||
Line 1,740: | Line 1,740: | ||
say |
say |
||
return</ |
return</syntaxhighlight> |
||
'''Output''' |
'''Output''' |
||
Line 1,791: | Line 1,791: | ||
</pre> |
</pre> |
||
< |
<syntaxhighlight lang="netrexx">/* NetRexx */ |
||
options replace format comments java crossref savelog symbols |
options replace format comments java crossref savelog symbols |
||
Line 1,844: | Line 1,844: | ||
end |
end |
||
return</ |
return</syntaxhighlight> |
||
'''Output''' |
'''Output''' |
||
Line 1,853: | Line 1,853: | ||
=={{header|Nial}}== |
=={{header|Nial}}== |
||
< |
<syntaxhighlight lang="nial">sort >= 9 6 8 7 1 10 |
||
= 10 9 8 7 6 1</ |
= 10 9 8 7 6 1</syntaxhighlight> |
||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
< |
<syntaxhighlight lang="nim">import algorithm |
||
var a: array[0..8, int] = [2, 3, 5, 8, 4, 1, 6, 9, 7] |
var a: array[0..8, int] = [2, 3, 5, 8, 4, 1, 6, 9, 7] |
||
a.sort(Ascending) |
a.sort(Ascending) |
||
for x in a: |
for x in a: |
||
echo x</ |
echo x</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>1 |
<pre>1 |
||
Line 1,876: | Line 1,876: | ||
=={{header|Niue}}== |
=={{header|Niue}}== |
||
'''Library''' |
'''Library''' |
||
< |
<syntaxhighlight lang="niue">2 6 1 0 3 8 sort .s |
||
0 1 2 3 6 8</ |
0 1 2 3 6 8</syntaxhighlight> |
||
=={{header|Objeck}}== |
=={{header|Objeck}}== |
||
< |
<syntaxhighlight lang="objeck">bundle Default { |
||
class Sort { |
class Sort { |
||
function : Main(args : System.String[]) ~ Nil { |
function : Main(args : System.String[]) ~ Nil { |
||
Line 1,887: | Line 1,887: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Objective-C}}== |
=={{header|Objective-C}}== |
||
< |
<syntaxhighlight lang="objc">NSArray *nums = @[@2, @4, @3, @1, @2]; |
||
NSArray *sorted = [nums sortedArrayUsingSelector:@selector(compare:)];</ |
NSArray *sorted = [nums sortedArrayUsingSelector:@selector(compare:)];</syntaxhighlight> |
||
=={{header|OCaml}}== |
=={{header|OCaml}}== |
||
===Array=== |
===Array=== |
||
< |
<syntaxhighlight lang="ocaml">let nums = [|2; 4; 3; 1; 2|] |
||
Array.sort compare nums</ |
Array.sort compare nums</syntaxhighlight> |
||
===List=== |
===List=== |
||
< |
<syntaxhighlight lang="ocaml">let nums = [2; 4; 3; 1; 2] |
||
let sorted = List.sort compare nums</ |
let sorted = List.sort compare nums</syntaxhighlight> |
||
=={{header|Octave}}== |
=={{header|Octave}}== |
||
Line 1,906: | Line 1,906: | ||
The variable <tt>v</tt> can be a vector or a matrix (columns will be sorted). |
The variable <tt>v</tt> can be a vector or a matrix (columns will be sorted). |
||
< |
<syntaxhighlight lang="octave">sortedv = sort(v);</syntaxhighlight> |
||
=={{header|Oforth}}== |
=={{header|Oforth}}== |
||
< |
<syntaxhighlight lang="oforth">[ 8, 2, 5, 9, 1, 3, 6, 7, 4 ] sort</syntaxhighlight> |
||
=={{header|ooRexx}}== |
=={{header|ooRexx}}== |
||
< |
<syntaxhighlight lang="rexx">a = .array~of(4, 1, 6, -2, 99, -12) |
||
say "The sorted numbers are" |
say "The sorted numbers are" |
||
say a~sortWith(.numericComparator~new)~makeString</ |
say a~sortWith(.numericComparator~new)~makeString</syntaxhighlight> |
||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 1,929: | Line 1,929: | ||
=={{header|Order}}== |
=={{header|Order}}== |
||
Passing the less-than operator to the built-in sequence (i.e. list) sort function: |
Passing the less-than operator to the built-in sequence (i.e. list) sort function: |
||
< |
<syntaxhighlight lang="c">#include <order/interpreter.h> |
||
ORDER_PP( 8seq_sort(8less, 8seq(2, 4, 3, 1, 2)) )</ |
ORDER_PP( 8seq_sort(8less, 8seq(2, 4, 3, 1, 2)) )</syntaxhighlight> |
||
=={{header|Oz}}== |
=={{header|Oz}}== |
||
< |
<syntaxhighlight lang="oz">declare |
||
Nums = [2 4 3 1 2] |
Nums = [2 4 3 1 2] |
||
Sorted = {List.sort Nums Value.'<'} |
Sorted = {List.sort Nums Value.'<'} |
||
in |
in |
||
{Show Sorted}</ |
{Show Sorted}</syntaxhighlight> |
||
=={{header|PARI/GP}}== |
=={{header|PARI/GP}}== |
||
<lang |
<syntaxhighlight lang="parigp">vecsort(v)</syntaxhighlight> |
||
=={{header|Peloton}}== |
=={{header|Peloton}}== |
||
Sorting a list of numbers as strings and as numbers (from the manual.) |
Sorting a list of numbers as strings and as numbers (from the manual.) |
||
< |
<syntaxhighlight lang="sgml">Construct a list of numbers |
||
<@ LETCNSLSTLIT>L|65^84^1^25^77^4^47^2^42^44^41^25^69^3^51^45^4^39^</@> |
<@ LETCNSLSTLIT>L|65^84^1^25^77^4^47^2^42^44^41^25^69^3^51^45^4^39^</@> |
||
Numbers sort as strings |
Numbers sort as strings |
||
Line 1,959: | Line 1,959: | ||
<@ SAYDMPLST>list</@> |
<@ SAYDMPLST>list</@> |
||
<@ ACTSRTENTLSTLIT>list|__NumericDescending</@> |
<@ ACTSRTENTLSTLIT>list|__NumericDescending</@> |
||
<@ SAYDMPLST>list</@></ |
<@ SAYDMPLST>list</@></syntaxhighlight> |
||
Output |
Output |
||
< |
<syntaxhighlight lang="html">Construct a list of numbers |
||
Numbers sort as strings |
Numbers sort as strings |
||
Line 1,976: | Line 1,976: | ||
1^2^3^4^4^25^25^39^41^42^44^45^47^51^65^69^77^84^ |
1^2^3^4^4^25^25^39^41^42^44^45^47^51^65^69^77^84^ |
||
84^77^69^65^51^47^45^44^42^41^39^25^25^4^4^3^2^1^</ |
84^77^69^65^51^47^45^44^42^41^39^25^25^4^4^3^2^1^</syntaxhighlight> |
||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
{{works with|Perl|5.8.6}} |
{{works with|Perl|5.8.6}} |
||
< |
<syntaxhighlight lang="perl">@nums = (2,4,3,1,2); |
||
@sorted = sort {$a <=> $b} @nums;</ |
@sorted = sort {$a <=> $b} @nums;</syntaxhighlight> |
||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
{{libheader|Phix/basics}} |
{{libheader|Phix/basics}} |
||
<!--< |
<!--<syntaxhighlight lang="phix">--> |
||
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">sort</span><span style="color: #0000FF;">({</span><span style="color: #000000;">9</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">10</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">3</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">4</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">5</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">8</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">7</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">6</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">})</span> |
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">sort</span><span style="color: #0000FF;">({</span><span style="color: #000000;">9</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">10</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">3</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">4</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">5</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">8</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">7</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">6</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">})</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
=={{header|Phixmonti}}== |
=={{header|Phixmonti}}== |
||
< |
<syntaxhighlight lang="phixmonti">include ..\Utilitys.pmt |
||
( 9 10 3 1 4 5 8 7 6 2 ) sort print</ |
( 9 10 3 1 4 5 8 7 6 2 ) sort print</syntaxhighlight> |
||
=={{header|PHP}}== |
=={{header|PHP}}== |
||
{{works with|PHP|4.4.4 CLI}} |
{{works with|PHP|4.4.4 CLI}} |
||
< |
<syntaxhighlight lang="php"><?php |
||
$nums = array(2,4,3,1,2); |
$nums = array(2,4,3,1,2); |
||
sort($nums); |
sort($nums); |
||
?></ |
?></syntaxhighlight> |
||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |
||
The [http://software-lab.de/doc/refS.html#sort sort] function in PicoLisp |
The [http://software-lab.de/doc/refS.html#sort sort] function in PicoLisp |
||
returns already by default an ascending list (of any type, not only integers): |
returns already by default an ascending list (of any type, not only integers): |
||
< |
<syntaxhighlight lang="picolisp">(sort (2 4 3 1 2)) |
||
-> (1 2 2 3 4)</ |
-> (1 2 2 3 4)</syntaxhighlight> |
||
=={{header|PL/I}}== |
=={{header|PL/I}}== |
||
{{works with|IBM PL/I|7.5}} |
{{works with|IBM PL/I|7.5}} |
||
< |
<syntaxhighlight lang="pli">DCL (T(10)) FIXED BIN(31); /* scratch space of length N/2 */ |
||
MERGE: PROCEDURE (A,LA,B,LB,C); |
MERGE: PROCEDURE (A,LA,B,LB,C); |
||
Line 2,045: | Line 2,045: | ||
CALL MERGE(T,M,AMP1,N-M,A); |
CALL MERGE(T,M,AMP1,N-M,A); |
||
RETURN; |
RETURN; |
||
END MERGESORT;</ |
END MERGESORT;</syntaxhighlight> |
||
=={{header|Pop11}}== |
=={{header|Pop11}}== |
||
Pop11 library function sorts lists. So we first convert array to list, then sort and finally convert back: |
Pop11 library function sorts lists. So we first convert array to list, then sort and finally convert back: |
||
< |
<syntaxhighlight lang="pop11">lvars ar = {2 4 3 1 2}; |
||
;;; Convert array to list. |
;;; Convert array to list. |
||
;;; destvector leaves its results and on the pop11 stack + an integer saying how many there were |
;;; destvector leaves its results and on the pop11 stack + an integer saying how many there were |
||
Line 2,060: | Line 2,060: | ||
;;; Convert list to array |
;;; Convert list to array |
||
destlist(ls); |
destlist(ls); |
||
consvector() -> ar;</ |
consvector() -> ar;</syntaxhighlight> |
||
The above can be abbreviated to more economical, but possibly more opaque, syntax, using pop11 as a functional language: |
The above can be abbreviated to more economical, but possibly more opaque, syntax, using pop11 as a functional language: |
||
< |
<syntaxhighlight lang="pop11">lvars ar = {2 4 3 1 2}; |
||
consvector(destlist(sort(conslist(destvector(ar))))) -> ar; |
consvector(destlist(sort(conslist(destvector(ar))))) -> ar; |
||
;;; print the sorted vector: |
;;; print the sorted vector: |
||
ar => |
ar => |
||
** {1 2 2 3 4}</ |
** {1 2 2 3 4}</syntaxhighlight> |
||
(The list created by conslist will be garbage-collected.) |
(The list created by conslist will be garbage-collected.) |
||
Line 2,074: | Line 2,074: | ||
Alternatively, using the datalist function, even more economically: |
Alternatively, using the datalist function, even more economically: |
||
< |
<syntaxhighlight lang="pop11">lvars ar = {2 4 3 1 2}; |
||
consvector(destlist(sort(datalist(ar)))) -> ar;</ |
consvector(destlist(sort(datalist(ar)))) -> ar;</syntaxhighlight> |
||
or in Forth-like pop11 postfix syntax: |
or in Forth-like pop11 postfix syntax: |
||
< |
<syntaxhighlight lang="pop11">lvars ar = {2 4 3 1 2}; |
||
ar.datalist.sort.destlist.consvector -> ar;</ |
ar.datalist.sort.destlist.consvector -> ar;</syntaxhighlight> |
||
=={{header|Potion}}== |
=={{header|Potion}}== |
||
< |
<syntaxhighlight lang="potion">(7, 5, 1, 2, 3, 8, 9) sort join(", ") print</syntaxhighlight> |
||
=={{header|PowerBASIC}}== |
=={{header|PowerBASIC}}== |
||
PowerBASIC has several options available for sorting. At its simplest, an array (of any type) is sorted using <code>ARRAY SORT</code>: |
PowerBASIC has several options available for sorting. At its simplest, an array (of any type) is sorted using <code>ARRAY SORT</code>: |
||
<lang |
<syntaxhighlight lang="powerbasic">ARRAY SORT x()</syntaxhighlight> |
||
Options are available to limit sorting to only part of the array, collate string arrays, sort multiple arrays together, etc. (Details [http://www.powerbasic.com/support/help/pbwin/html/ARRAY_SORT_statement.htm here].) |
Options are available to limit sorting to only part of the array, collate string arrays, sort multiple arrays together, etc. (Details [http://www.powerbasic.com/support/help/pbwin/html/ARRAY_SORT_statement.htm here].) |
||
=={{header|PowerShell}}== |
=={{header|PowerShell}}== |
||
< |
<syntaxhighlight lang="powershell">34,12,23,56,1,129,4,2,73 | Sort-Object</syntaxhighlight> |
||
=={{header|Prolog}}== |
=={{header|Prolog}}== |
||
Line 2,101: | Line 2,101: | ||
=={{header|PureBasic}}== |
=={{header|PureBasic}}== |
||
< |
<syntaxhighlight lang="purebasic">Dim numbers(20) |
||
For i = 0 To 20 |
For i = 0 To 20 |
||
numbers(i) = Random(1000) |
numbers(i) = Random(1000) |
||
Next |
Next |
||
SortArray(numbers(), #PB_Sort_Ascending)</ |
SortArray(numbers(), #PB_Sort_Ascending)</syntaxhighlight> |
||
=={{header|Python}}== |
=={{header|Python}}== |
||
{{works with|Python|2.3}} |
{{works with|Python|2.3}} |
||
< |
<syntaxhighlight lang="python">nums = [2,4,3,1,2] |
||
nums.sort()</ |
nums.sort()</syntaxhighlight> |
||
'''Note:''' The array <tt>nums</tt> is sorted in place. |
'''Note:''' The array <tt>nums</tt> is sorted in place. |
||
Line 2,119: | Line 2,119: | ||
You could also use the built-in sorted() function |
You could also use the built-in sorted() function |
||
< |
<syntaxhighlight lang="python">nums = sorted([2,4,3,1,2])</syntaxhighlight> |
||
=={{header|Quackery}}== |
=={{header|Quackery}}== |
||
Line 2,135: | Line 2,135: | ||
=={{header|R}}== |
=={{header|R}}== |
||
< |
<syntaxhighlight lang="r">nums <- c(2,4,3,1,2) |
||
sorted <- sort(nums)</ |
sorted <- sort(nums)</syntaxhighlight> |
||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
<syntaxhighlight lang="racket"> |
|||
<lang Racket> |
|||
-> (sort '(1 9 2 8 3 7 4 6 5) <) |
-> (sort '(1 9 2 8 3 7 4 6 5) <) |
||
'(1 2 3 4 5 6 7 8 9) |
'(1 2 3 4 5 6 7 8 9) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
Line 2,148: | Line 2,148: | ||
If <code>@a</code> contains only numbers: |
If <code>@a</code> contains only numbers: |
||
<lang |
<syntaxhighlight lang="raku" line>my @sorted = sort @a;</syntaxhighlight> |
||
For an in-place sort: |
For an in-place sort: |
||
<lang |
<syntaxhighlight lang="raku" line>@a .= sort;</syntaxhighlight> |
||
=={{header|Rascal}}== |
=={{header|Rascal}}== |
||
Rascal has a built-in sort function that sort the elements of a list. Additionally, one can give a LessThenOrEqual function to compare the elements (See [http://tutor.rascal-mpl.org/Courses/Rascal/Rascal.html#/Courses/Rascal/Libraries/Prelude/List/sort/sort.html documentation]). |
Rascal has a built-in sort function that sort the elements of a list. Additionally, one can give a LessThenOrEqual function to compare the elements (See [http://tutor.rascal-mpl.org/Courses/Rascal/Rascal.html#/Courses/Rascal/Libraries/Prelude/List/sort/sort.html documentation]). |
||
< |
<syntaxhighlight lang="rascal">rascal>import List; |
||
ok |
ok |
||
Line 2,166: | Line 2,166: | ||
rascal>sort(a, bool(int a, int b){return a >= b;}) |
rascal>sort(a, bool(int a, int b){return a >= b;}) |
||
list[int]: [5,4,3,2,1]</ |
list[int]: [5,4,3,2,1]</syntaxhighlight> |
||
=={{header|Raven}}== |
=={{header|Raven}}== |
||
Sort list in place: |
Sort list in place: |
||
< |
<syntaxhighlight lang="raven">[ 2 4 3 1 2 ] sort</syntaxhighlight> |
||
=={{header|REBOL}}== |
=={{header|REBOL}}== |
||
< |
<syntaxhighlight lang="rebol">sort [2 4 3 1 2]</syntaxhighlight> |
||
=={{header|Red}}== |
=={{header|Red}}== |
||
< |
<syntaxhighlight lang="red">>> nums: [3 2 6 4 1 9 0 5 7] |
||
== [3 2 6 4 1 9 0 5 7] |
== [3 2 6 4 1 9 0 5 7] |
||
>> sort nums |
>> sort nums |
||
== [0 1 2 3 4 5 6 7 9]</ |
== [0 1 2 3 4 5 6 7 9]</syntaxhighlight> |
||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
===sort an array=== |
===sort an array=== |
||
This REXX version creates an array with over a score of Euler numbers (integers), then sorts it. |
This REXX version creates an array with over a score of Euler numbers (integers), then sorts it. |
||
< |
<syntaxhighlight lang="rexx">/*REXX program sorts an array (using E─sort), in this case, the array contains integers.*/ |
||
numeric digits 30 /*enables handling larger Euler numbers*/ |
numeric digits 30 /*enables handling larger Euler numbers*/ |
||
@. = 0; @.1 = 1 |
@. = 0; @.1 = 1 |
||
Line 2,213: | Line 2,213: | ||
do j=1 for #; say _ arg(1) 'array element' right(j, w)"="right(@.j, 20) |
do j=1 for #; say _ arg(1) 'array element' right(j, w)"="right(@.j, 20) |
||
end /*j*/ |
end /*j*/ |
||
return</ |
return</syntaxhighlight> |
||
{{out|output|text= when using the default internal input:}} |
{{out|output|text= when using the default internal input:}} |
||
<pre> |
<pre> |
||
Line 2,267: | Line 2,267: | ||
Because it so much more efficient to sort an array, an array is built from the list, |
Because it so much more efficient to sort an array, an array is built from the list, |
||
<br>it is then sorted, and then the list is re-constituted. |
<br>it is then sorted, and then the list is re-constituted. |
||
< |
<syntaxhighlight lang="rexx">/*REXX program sorts (using E─sort) and displays a list of some interesting integers. */ |
||
Bell= 1 1 2 5 15 52 203 877 4140 21147 115975 /*a few Bell " */ |
Bell= 1 1 2 5 15 52 203 877 4140 21147 115975 /*a few Bell " */ |
||
Bern= '1 -1 1 0 -1 0 1 0 -1 0 5 0 -691 0 7 0 -3617' /*" " Bernoulli " */ |
Bern= '1 -1 1 0 -1 0 1 0 -1 0 5 0 -691 0 7 0 -3617' /*" " Bernoulli " */ |
||
Line 2,292: | Line 2,292: | ||
end /*i*/ |
end /*i*/ |
||
end /*while h>1*/ |
end /*while h>1*/ |
||
return</ |
return</syntaxhighlight> |
||
{{out|output|text= when using the default internal inputs:}} |
{{out|output|text= when using the default internal inputs:}} |
||
Line 2,303: | Line 2,303: | ||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
< |
<syntaxhighlight lang="ring">aArray = [2,4,3,1,2] |
||
see sort(aArray)</ |
see sort(aArray)</syntaxhighlight> |
||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
< |
<syntaxhighlight lang="ruby">nums = [2,4,3,1,2] |
||
sorted = nums.sort # returns a new sorted array. 'nums' is unchanged |
sorted = nums.sort # returns a new sorted array. 'nums' is unchanged |
||
p sorted #=> [1, 2, 2, 3, 4] |
p sorted #=> [1, 2, 2, 3, 4] |
||
Line 2,313: | Line 2,313: | ||
nums.sort! # sort 'nums' "in-place" |
nums.sort! # sort 'nums' "in-place" |
||
p nums #=> [1, 2, 2, 3, 4]</ |
p nums #=> [1, 2, 2, 3, 4]</syntaxhighlight> |
||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
Uses merge sort in place (undocumented), allocating ~2*n memory where n is a length of an array. |
Uses merge sort in place (undocumented), allocating ~2*n memory where n is a length of an array. |
||
< |
<syntaxhighlight lang="rust">fn main() { |
||
let mut a = vec!(9, 8, 7, 6, 5, 4, 3, 2, 1, 0); |
let mut a = vec!(9, 8, 7, 6, 5, 4, 3, 2, 1, 0); |
||
a.sort(); |
a.sort(); |
||
println!("{:?}", a); |
println!("{:?}", a); |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Sather}}== |
=={{header|Sather}}== |
||
< |
<syntaxhighlight lang="sather">class MAIN is |
||
main is |
main is |
||
arr: ARRAY{INT} := |4, 6, 7, 2, 1, 0, 100, 21, 34|; |
arr: ARRAY{INT} := |4, 6, 7, 2, 1, 0, 100, 21, 34|; |
||
Line 2,335: | Line 2,335: | ||
#OUT+" sorted: " + arr + "\n"; |
#OUT+" sorted: " + arr + "\n"; |
||
end; |
end; |
||
end;</ |
end;</syntaxhighlight> |
||
Output: |
Output: |
||
<lang>unsorted: {4,6,7,2,1,0,100,21,34} |
<syntaxhighlight lang="text">unsorted: {4,6,7,2,1,0,100,21,34} |
||
sorted: {0,1,2,4,6,7,21,34,100}</ |
sorted: {0,1,2,4,6,7,21,34,100}</syntaxhighlight> |
||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
{{libheader|Scala}} |
{{libheader|Scala}} |
||
===Array=== |
===Array=== |
||
Scala's "default" Array is a ''mutable'' data structure, very close to Java's Array. Generally speaking, that means an "array" is not very Scala-lesque, even as mutable data structures go. It can serves a purpose, though. If array is the right data type for your need, then that is how you sort it.< |
Scala's "default" Array is a ''mutable'' data structure, very close to Java's Array. Generally speaking, that means an "array" is not very Scala-lesque, even as mutable data structures go. It can serves a purpose, though. If array is the right data type for your need, then that is how you sort it.<syntaxhighlight lang="scala">import scala.compat.Platform |
||
object Sort_an_integer_array extends App { |
object Sort_an_integer_array extends App { |
||
Line 2,358: | Line 2,358: | ||
println(s"Array in sorted order.\nSuccessfully completed without errors. [total ${Platform.currentTime - executionStart} ms]") |
println(s"Array in sorted order.\nSuccessfully completed without errors. [total ${Platform.currentTime - executionStart} ms]") |
||
}</ |
}</syntaxhighlight> |
||
===List=== |
===List=== |
||
< |
<syntaxhighlight lang="scala">println(List(5,2,78,2,578,-42).sorted) |
||
//--> List(-42, 2, 2, 5, 78, 578)</ |
//--> List(-42, 2, 2, 5, 78, 578)</syntaxhighlight> |
||
=={{header|Scheme}}== |
=={{header|Scheme}}== |
||
{{works with|Guile}} |
{{works with|Guile}} |
||
Same as [[Common Lisp]] |
Same as [[Common Lisp]] |
||
< |
<syntaxhighlight lang="scheme">(sort #(9 -2 1 2 8 0 1 2) #'<)</syntaxhighlight> |
||
{{libheader|Scheme/SRFIs}} |
{{libheader|Scheme/SRFIs}} |
||
Line 2,372: | Line 2,372: | ||
Sorting is also available through SRFIs. SRFI 132 provides separate list-sort and vector-sort routines: |
Sorting is also available through SRFIs. SRFI 132 provides separate list-sort and vector-sort routines: |
||
< |
<syntaxhighlight lang="scheme"> |
||
> (import (srfi 132)) |
> (import (srfi 132)) |
||
> (list-sort < '(9 -2 1 2 8 0 1 2)) |
> (list-sort < '(9 -2 1 2 8 0 1 2)) |
||
Line 2,379: | Line 2,379: | ||
> (vector-sort < #(9 -2 1 2 8 0 1 2)) |
> (vector-sort < #(9 -2 1 2 8 0 1 2)) |
||
#(-2 0 1 1 2 2 8 9) |
#(-2 0 1 1 2 2 8 9) |
||
</syntaxhighlight> |
|||
</lang> |
|||
SRFI 132 replaced the older SRFI 95, which is still found in many implementations. SRFI 95 provides a generic sort function (but note the order of the sequence and comparator!): |
SRFI 132 replaced the older SRFI 95, which is still found in many implementations. SRFI 95 provides a generic sort function (but note the order of the sequence and comparator!): |
||
< |
<syntaxhighlight lang="scheme"> |
||
> (import (srfi 95)) |
> (import (srfi 95)) |
||
> (sort '(9 -2 1 2 8 0 1 2) <) |
> (sort '(9 -2 1 2 8 0 1 2) <) |
||
Line 2,389: | Line 2,389: | ||
> (sort #(9 -2 1 2 8 0 1 2) <) |
> (sort #(9 -2 1 2 8 0 1 2) <) |
||
#(-2 0 1 1 2 2 8 9) |
#(-2 0 1 1 2 2 8 9) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Seed7}}== |
=={{header|Seed7}}== |
||
< |
<syntaxhighlight lang="seed7">var array integer: nums is [] (2, 4, 3, 1, 2); |
||
nums := sort(nums);</ |
nums := sort(nums);</syntaxhighlight> |
||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
< |
<syntaxhighlight lang="ruby">var nums = [2,4,3,1,2]; |
||
var sorted = nums.sort; # returns a new sorted array. |
var sorted = nums.sort; # returns a new sorted array. |
||
nums.sort!; # sort 'nums' "in-place"</ |
nums.sort!; # sort 'nums' "in-place"</syntaxhighlight> |
||
=={{header|Slate}}== |
=={{header|Slate}}== |
||
< |
<syntaxhighlight lang="slate"> #(7 5 2 9 0 -1) sort</syntaxhighlight> |
||
=={{header|Smalltalk}}== |
=={{header|Smalltalk}}== |
||
< |
<syntaxhighlight lang="smalltalk"> #(7 5 2 9 0 -1) asSortedCollection</syntaxhighlight> |
||
or destructive: |
or destructive: |
||
< |
<syntaxhighlight lang="smalltalk"> #(7 5 2 9 0 -1) sort</syntaxhighlight> |
||
=={{header|Sparkling}}== |
=={{header|Sparkling}}== |
||
< |
<syntaxhighlight lang="sparkling">var arr = { 2, 8, 1, 4, 6, 5, 3, 7, 0, 9 }; |
||
sort(arr);</ |
sort(arr);</syntaxhighlight> |
||
=={{header|Standard ML}}== |
=={{header|Standard ML}}== |
||
Line 2,418: | Line 2,418: | ||
===Array=== |
===Array=== |
||
{{works with|SML/NJ}} |
{{works with|SML/NJ}} |
||
< |
<syntaxhighlight lang="sml">- val nums = Array.fromList [2, 4, 3, 1, 2]; |
||
val nums = [|2,4,3,1,2|] : int array |
val nums = [|2,4,3,1,2|] : int array |
||
- ArrayQSort.sort Int.compare nums; |
- ArrayQSort.sort Int.compare nums; |
||
val it = () : unit |
val it = () : unit |
||
- nums; |
- nums; |
||
val it = [|1,2,2,3,4|] : int array</ |
val it = [|1,2,2,3,4|] : int array</syntaxhighlight> |
||
{{works with|Moscow ML}} |
{{works with|Moscow ML}} |
||
< |
<syntaxhighlight lang="sml">- load "Arraysort"; |
||
> val it = () : unit |
> val it = () : unit |
||
- load "Int"; |
- load "Int"; |
||
Line 2,435: | Line 2,435: | ||
> val it = () : unit |
> val it = () : unit |
||
- Array.foldr op:: [] nums; |
- Array.foldr op:: [] nums; |
||
> val it = [1, 2, 2, 3, 4] : int list</ |
> val it = [1, 2, 2, 3, 4] : int list</syntaxhighlight> |
||
===List=== |
===List=== |
||
{{works with|SML/NJ}} |
{{works with|SML/NJ}} |
||
< |
<syntaxhighlight lang="sml">- val nums = [2, 4, 3, 1, 2]; |
||
val nums = [2,4,3,1,2] : int list |
val nums = [2,4,3,1,2] : int list |
||
- val sorted = ListMergeSort.sort op> nums; |
- val sorted = ListMergeSort.sort op> nums; |
||
val sorted = [1,2,2,3,4] : int list</ |
val sorted = [1,2,2,3,4] : int list</syntaxhighlight> |
||
{{works with|Moscow ML}} |
{{works with|Moscow ML}} |
||
< |
<syntaxhighlight lang="sml">- load "Listsort"; |
||
> val it = () : unit |
> val it = () : unit |
||
- load "Int"; |
- load "Int"; |
||
Line 2,452: | Line 2,452: | ||
> val nums = [2, 4, 3, 1, 2] : int list |
> val nums = [2, 4, 3, 1, 2] : int list |
||
- val sorted = Listsort.sort Int.compare nums; |
- val sorted = Listsort.sort Int.compare nums; |
||
> val sorted = [1, 2, 2, 3, 4] : int list</ |
> val sorted = [1, 2, 2, 3, 4] : int list</syntaxhighlight> |
||
=={{header|Stata}}== |
=={{header|Stata}}== |
||
Line 2,458: | Line 2,458: | ||
See '''[https://www.stata.com/help.cgi?sort sort]''' in Stata help. |
See '''[https://www.stata.com/help.cgi?sort sort]''' in Stata help. |
||
< |
<syntaxhighlight lang="stata">. clear |
||
. matrix a=(2,9,4,7,5,3,6,1,8)' |
. matrix a=(2,9,4,7,5,3,6,1,8)' |
||
. qui svmat a |
. qui svmat a |
||
Line 2,477: | Line 2,477: | ||
8. | 8 | |
8. | 8 | |
||
9. | 9 | |
9. | 9 | |
||
+----+</ |
+----+</syntaxhighlight> |
||
=== Sort a macro list === |
=== Sort a macro list === |
||
See '''[https://www.stata.com/help.cgi?macrolists macrolists]''' in Stata help for other functions on lists stored in macros. |
See '''[https://www.stata.com/help.cgi?macrolists macrolists]''' in Stata help for other functions on lists stored in macros. |
||
< |
<syntaxhighlight lang="stata">. local a 2 9 4 7 5 3 6 1 8 |
||
. di "`: list sort a'" |
. di "`: list sort a'" |
||
1 2 3 4 5 6 7 8 9</ |
1 2 3 4 5 6 7 8 9</syntaxhighlight> |
||
=== Mata === |
=== Mata === |
||
See Mata's '''[http://www.stata.com/help.cgi?mf_sort sort]''' function. |
See Mata's '''[http://www.stata.com/help.cgi?mf_sort sort]''' function. |
||
< |
<syntaxhighlight lang="stata">mata |
||
: a=2\9\4\7\5\3\6\1\8 |
: a=2\9\4\7\5\3\6\1\8 |
||
Line 2,505: | Line 2,505: | ||
9 | 9 | |
9 | 9 | |
||
+-----+ |
+-----+ |
||
end</ |
end</syntaxhighlight> |
||
=={{header|Swift}}== |
=={{header|Swift}}== |
||
===Sort in place=== |
===Sort in place=== |
||
{{works with|Swift|2.x+}} |
{{works with|Swift|2.x+}} |
||
< |
<syntaxhighlight lang="swift">var nums = [2, 4, 3, 1, 2] |
||
nums.sortInPlace() |
nums.sortInPlace() |
||
print(nums)</ |
print(nums)</syntaxhighlight> |
||
or |
or |
||
< |
<syntaxhighlight lang="swift">var nums = [2, 4, 3, 1, 2] |
||
nums.sortInPlace(<) |
nums.sortInPlace(<) |
||
print(nums)</ |
print(nums)</syntaxhighlight> |
||
{{works with|Swift|1.x}} |
{{works with|Swift|1.x}} |
||
< |
<syntaxhighlight lang="swift">var nums = [2, 4, 3, 1, 2] |
||
nums.sort(<) |
nums.sort(<) |
||
println(nums)</ |
println(nums)</syntaxhighlight> |
||
or |
or |
||
< |
<syntaxhighlight lang="swift">var nums = [2, 4, 3, 1, 2] |
||
sort(&nums) |
sort(&nums) |
||
println(nums)</ |
println(nums)</syntaxhighlight> |
||
or |
or |
||
< |
<syntaxhighlight lang="swift">var nums = [2, 4, 3, 1, 2] |
||
sort(&nums, <) |
sort(&nums, <) |
||
println(nums)</ |
println(nums)</syntaxhighlight> |
||
===Return new array=== |
===Return new array=== |
||
Line 2,535: | Line 2,535: | ||
{{works with|Swift|2.x+}} |
{{works with|Swift|2.x+}} |
||
< |
<syntaxhighlight lang="swift">let nums = [2,4,3,1,2].sort() |
||
print(nums)</ |
print(nums)</syntaxhighlight> |
||
or |
or |
||
< |
<syntaxhighlight lang="swift">let nums = [2,4,3,1,2].sort(<) |
||
print(nums)</ |
print(nums)</syntaxhighlight> |
||
{{works with|Swift|1.x}} |
{{works with|Swift|1.x}} |
||
< |
<syntaxhighlight lang="swift">let nums = sorted([2,4,3,1,2]) |
||
println(nums)</ |
println(nums)</syntaxhighlight> |
||
or |
or |
||
< |
<syntaxhighlight lang="swift">let nums = [2,4,3,1,2].sorted(<) |
||
println(nums)</ |
println(nums)</syntaxhighlight> |
||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
< |
<syntaxhighlight lang="tcl">set result [lsort -integer $unsorted_list]</syntaxhighlight> |
||
=={{header|TI-83 BASIC}}== |
=={{header|TI-83 BASIC}}== |
||
Line 2,560: | Line 2,560: | ||
This can be done by using the bubble sort library: |
This can be done by using the bubble sort library: |
||
< |
<syntaxhighlight lang="toka">needs bsort |
||
arrayname number_elements bsort</ |
arrayname number_elements bsort</syntaxhighlight> |
||
See the Toka entry on [[Bubble Sort]] for a full example. |
See the Toka entry on [[Bubble Sort]] for a full example. |
||
Line 2,568: | Line 2,568: | ||
Each shell parameter separates the integers using the default IFS whitespace (space, tab, newline). |
Each shell parameter separates the integers using the default IFS whitespace (space, tab, newline). |
||
< |
<syntaxhighlight lang="bash">nums="2 4 3 1 5" |
||
sorted=`printf "%s\n" $nums | sort -n` |
sorted=`printf "%s\n" $nums | sort -n` |
||
echo $sorted # prints 1 2 3 4 5</ |
echo $sorted # prints 1 2 3 4 5</syntaxhighlight> |
||
Alternate solution: <tt>sorted=`for i in $nums; do echo $i; done | sort -n`</tt> |
Alternate solution: <tt>sorted=`for i in $nums; do echo $i; done | sort -n`</tt> |
||
Line 2,578: | Line 2,578: | ||
{{works with|pdksh|5.2.14}} |
{{works with|pdksh|5.2.14}} |
||
< |
<syntaxhighlight lang="bash">set -A nums 2 4 3 1 5 |
||
set -A sorted $(printf "%s\n" ${nums[*]} | sort -n) |
set -A sorted $(printf "%s\n" ${nums[*]} | sort -n) |
||
echo ${sorted[*]} # prints 1 2 3 4 5</ |
echo ${sorted[*]} # prints 1 2 3 4 5</syntaxhighlight> |
||
Users of [[bash]], [[ksh93]] and [[mksh]] can probably use the <tt>nums=(2 4 3 1 2)</tt> syntax. |
Users of [[bash]], [[ksh93]] and [[mksh]] can probably use the <tt>nums=(2 4 3 1 2)</tt> syntax. |
||
=={{header|Ursa}}== |
=={{header|Ursa}}== |
||
< |
<syntaxhighlight lang="ursa">decl int<> nums |
||
append 2 4 3 1 2 nums |
append 2 4 3 1 2 nums |
||
sort nums</ |
sort nums</syntaxhighlight> |
||
=={{header|Ursala}}== |
=={{header|Ursala}}== |
||
using the built in sort operator, -<, with the nleq library function |
using the built in sort operator, -<, with the nleq library function |
||
for comparing natural numbers |
for comparing natural numbers |
||
< |
<syntaxhighlight lang="ursala">#import nat |
||
#cast %nL |
#cast %nL |
||
example = nleq-< <39,47,40,53,14,23,88,52,78,62,41,92,88,66,5,40></ |
example = nleq-< <39,47,40,53,14,23,88,52,78,62,41,92,88,66,5,40></syntaxhighlight> |
||
output: |
output: |
||
<pre><5,14,23,39,40,40,41,47,52,53,62,66,78,88,88,92></pre> |
<pre><5,14,23,39,40,40,41,47,52,53,62,66,78,88,88,92></pre> |
||
=={{header|Vlang}}== |
=={{header|Vlang}}== |
||
< |
<syntaxhighlight lang="vlang">fn main() { |
||
mut nums := [3, 2, 4, 1, 6, 7, 5, 0] |
mut nums := [3, 2, 4, 1, 6, 7, 5, 0] |
||
nums.sort() |
nums.sort() |
||
println(nums) |
println(nums) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,613: | Line 2,613: | ||
=={{header|WDTE}}== |
=={{header|WDTE}}== |
||
< |
<syntaxhighlight lang="wdte">let a => import 'arrays'; |
||
a.sort [39; 47; 40; 53; 14; 23; 88; 52; 78; 62; 41; 92; 88; 66; 5; 40] < -- io.writeln io.stdout;</ |
a.sort [39; 47; 40; 53; 14; 23; 88; 52; 78; 62; 41; 92; 88; 66; 5; 40] < -- io.writeln io.stdout;</syntaxhighlight> |
||
=={{header|Wortel}}== |
=={{header|Wortel}}== |
||
< |
<syntaxhighlight lang="wortel">@sort [39 47 40 53 14 23 88 52 78 62 41 92 88 66 5 40]</syntaxhighlight> |
||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
{{libheader|Wren-sort}} |
{{libheader|Wren-sort}} |
||
< |
<syntaxhighlight lang="ecmascript">import "/sort" for Sort |
||
var a = [7, 10, 2, 4, 6, 1, 8, 3, 9, 5] |
var a = [7, 10, 2, 4, 6, 1, 8, 3, 9, 5] |
||
Sort.quick(a) |
Sort.quick(a) |
||
System.print(a)</ |
System.print(a)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,633: | Line 2,633: | ||
=={{header|XPL0}}== |
=={{header|XPL0}}== |
||
< |
<syntaxhighlight lang="xpl0">include c:\cxpl\codes; \intrinsic 'code' declarations |
||
proc SSort(A, N); \Shell sort array in ascending order |
proc SSort(A, N); \Shell sort array in ascending order |
||
Line 2,659: | Line 2,659: | ||
for I:= 0 to 10-1 do [IntOut(0, A(I)); ChOut(0, ^ )]; |
for I:= 0 to 10-1 do [IntOut(0, A(I)); ChOut(0, ^ )]; |
||
CrLf(0); |
CrLf(0); |
||
]</ |
]</syntaxhighlight> |
||
Output: |
Output: |
||
Line 2,667: | Line 2,667: | ||
=={{header|Yabasic}}== |
=={{header|Yabasic}}== |
||
< |
<syntaxhighlight lang="yabasic">export sub shell_sort(x()) |
||
// Shell sort based on insertion sort |
// Shell sort based on insertion sort |
||
Line 2,722: | Line 2,722: | ||
next n |
next n |
||
end if</ |
end if</syntaxhighlight> |
||
=={{header|Yorick}}== |
=={{header|Yorick}}== |
||
In Yorick, ''sort'' returns an index list into the array that will put it in sorted order. |
In Yorick, ''sort'' returns an index list into the array that will put it in sorted order. |
||
< |
<syntaxhighlight lang="yorick"> |
||
nums = [2,4,3,1,2]; |
nums = [2,4,3,1,2]; |
||
nums = nums(sort(nums)); |
nums = nums(sort(nums)); |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
In place sorting read/write list: |
In place sorting read/write list: |
||
< |
<syntaxhighlight lang="zkl">a:=L(4,5,2,6); a.sort(); a.println() //--> L(2,4,5,6)</syntaxhighlight> |
||
Sort a read only list: |
Sort a read only list: |
||
< |
<syntaxhighlight lang="zkl">a:=T(4,5,2,6); b:=a.sort(); |
||
b.println(); //--> L(2,4,5,6) |
b.println(); //--> L(2,4,5,6) |
||
a.println(); //--> L(4,5,2,6)</ |
a.println(); //--> L(4,5,2,6)</syntaxhighlight> |
||
=={{header|Zoea}}== |
=={{header|Zoea}}== |
||
<syntaxhighlight lang="zoea"> |
|||
<lang Zoea> |
|||
program: sort_integer_array |
program: sort_integer_array |
||
input: [2,4,3,1] |
input: [2,4,3,1] |
||
output: [1,2,3,4] |
output: [1,2,3,4] |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Zoea Visual}}== |
=={{header|Zoea Visual}}== |