Sort an integer array: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) (Rename Perl 6 -> Raku, alphabetize, minor clean-up) |
|||
Line 28: | Line 28: | ||
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</lang> |
TRIER TABLEAU($nombres;<) ` pour effectuer un tri par ordre décroissant</lang> |
||
=={{header|8th}}== |
=={{header|8th}}== |
||
<lang forth> |
<lang forth> |
||
Line 152: | Line 153: | ||
X[⍋X] |
X[⍋X] |
||
15 36 39 43 51 63 69 89 92 92</lang> |
15 36 39 43 51 63 69 89 92 92</lang> |
||
=={{header|AppleScript}}== |
=={{header|AppleScript}}== |
||
Line 230: | Line 230: | ||
8 |
8 |
||
9</pre> |
9</pre> |
||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
Line 403: | Line 402: | ||
''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>. |
||
=={{header|C sharp|C#}}== |
|||
<lang csharp>using System; |
|||
using System.Collections.Generic; |
|||
public class Program { |
|||
static void Main() { |
|||
int[] unsorted = { 6, 2, 7, 8, 3, 1, 10, 5, 4, 9 }; |
|||
Array.Sort(unsorted); |
|||
} |
|||
}</lang> |
|||
=={{header|C++}}== |
=={{header|C++}}== |
||
Line 446: | Line 457: | ||
nums.sort(); |
nums.sort(); |
||
return 0; |
return 0; |
||
}</lang> |
|||
=={{header|C sharp|C#}}== |
|||
<lang csharp>using System; |
|||
using System.Collections.Generic; |
|||
public class Program { |
|||
static void Main() { |
|||
int[] unsorted = { 6, 2, 7, 8, 3, 1, 10, 5, 4, 9 }; |
|||
Array.Sort(unsorted); |
|||
} |
|||
}</lang> |
}</lang> |
||
Line 537: | Line 536: | ||
TArray.Sort<Integer>(a); |
TArray.Sort<Integer>(a); |
||
end;</lang> |
end;</lang> |
||
=={{header|Déjà Vu}}== |
|||
<lang dejavu>!. sort [ 5 4 3 2 1 ]</lang> |
|||
{{out}} |
|||
<pre>[ 1 2 3 4 5 ]</pre> |
|||
=={{header|DWScript}}== |
=={{header|DWScript}}== |
||
Line 546: | Line 541: | ||
a.Sort; // ascending natural sort |
a.Sort; // ascending natural sort |
||
PrintLn(a.Map(IntToStr).Join(',')); // 1,2,3,4,5</lang> |
PrintLn(a.Map(IntToStr).Join(',')); // 1,2,3,4,5</lang> |
||
=={{header|Déjà Vu}}== |
|||
<lang dejavu>!. sort [ 5 4 3 2 1 ]</lang> |
|||
{{out}} |
|||
<pre>[ 1 2 3 4 5 ]</pre> |
|||
=={{header|E}}== |
=={{header|E}}== |
||
<lang e>[2,4,3,1,2].sort()</lang> |
<lang e>[2,4,3,1,2].sort()</lang> |
||
=={{header|EGL}}== |
|||
{{works with|EDT}} |
|||
The following works in EDT with Rich UI and stand-alone programs. |
|||
<lang EGL>program SortExample |
|||
function main() |
|||
test1 int[] = [1,-1,8,-8,2,-2,7,-7,3,-3,6,-6,9,-9,4,-4,5,-5,0]; |
|||
test1.sort(sortFunction); |
|||
for(i int from 1 to test1.getSize()) |
|||
SysLib.writeStdout(test1[i]); |
|||
end |
|||
end |
|||
function sortFunction(a any in, b any in) returns (int) |
|||
return (a as int) - (b as int); |
|||
end |
|||
end</lang> |
|||
{{works with|RBD}} |
|||
The following works in RBD but only with Rich UI programs. |
|||
<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); |
|||
function sortFunction(a any in, b any in) returns (int) |
|||
return ((a as int) - (b as int)); |
|||
end</lang> |
|||
=={{header|Elena}}== |
=={{header|Elena}}== |
||
ELENA 5.0 : |
ELENA 5.0 : |
||
Line 580: | Line 610: | ||
print(1,sort({20, 7, 65, 10, 3, 0, 8, -60}))</lang> |
print(1,sort({20, 7, 65, 10, 3, 0, 8, -60}))</lang> |
||
=={{header| |
=={{header|F_Sharp|F#}}== |
||
<lang fsharp>// sorting an array in place |
|||
{{works with|EDT}} |
|||
let nums = [| 2; 4; 3; 1; 2 |] |
|||
The following works in EDT with Rich UI and stand-alone programs. |
|||
Array.sortInPlace nums |
|||
<lang EGL>program SortExample |
|||
// create a sorted copy of a list |
|||
function main() |
|||
let nums2 = [2; 4; 3; 1; 2] |
|||
test1 int[] = [1,-1,8,-8,2,-2,7,-7,3,-3,6,-6,9,-9,4,-4,5,-5,0]; |
|||
let sorted = List.sort nums2</lang> |
|||
test1.sort(sortFunction); |
|||
for(i int from 1 to test1.getSize()) |
|||
SysLib.writeStdout(test1[i]); |
|||
end |
|||
end |
|||
function sortFunction(a any in, b any in) returns (int) |
|||
return (a as int) - (b as int); |
|||
end |
|||
end</lang> |
|||
{{works with|RBD}} |
|||
The following works in RBD but only with Rich UI programs. |
|||
<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); |
|||
function sortFunction(a any in, b any in) returns (int) |
|||
return ((a as int) - (b as int)); |
|||
end</lang> |
|||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
Line 759: | Line 769: | ||
<lang frink>a = [5, 2, 4, 1, 6, 7, 9, 3, 8, 0] |
<lang frink>a = [5, 2, 4, 1, 6, 7, 9, 3, 8, 0] |
||
sort[a]</lang> |
sort[a]</lang> |
||
=={{header|F_Sharp|F#}}== |
|||
<lang fsharp>// sorting an array in place |
|||
let nums = [| 2; 4; 3; 1; 2 |] |
|||
Array.sortInPlace nums |
|||
// create a sorted copy of a list |
|||
let nums2 = [2; 4; 3; 1; 2] |
|||
let sorted = List.sort nums2</lang> |
|||
=={{header|FunL}}== |
=={{header|FunL}}== |
||
Line 780: | Line 781: | ||
[578, 78, 5, 2, 2, -42] |
[578, 78, 5, 2, 2, -42] |
||
</pre> |
</pre> |
||
=={{header|GAP}}== |
|||
<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) |
|||
b := ShallowCopy(a); |
|||
# Sort in place |
|||
Sort(a); |
|||
a; |
|||
# [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ] |
|||
# Sort without changing the argument |
|||
SortedList(b); |
|||
# [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ] |
|||
b; |
|||
# [ 8, 2, 5, 9, 1, 3, 6, 7, 4 ]</lang> |
|||
=={{header|Gambas}}== |
=={{header|Gambas}}== |
||
Line 816: | Line 801: | ||
1, 2, 3, 4, 5, 6, 7, 8, 9 |
1, 2, 3, 4, 5, 6, 7, 8, 9 |
||
</pre> |
</pre> |
||
=={{header|GAP}}== |
|||
<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) |
|||
b := ShallowCopy(a); |
|||
# Sort in place |
|||
Sort(a); |
|||
a; |
|||
# [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ] |
|||
# Sort without changing the argument |
|||
SortedList(b); |
|||
# [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ] |
|||
b; |
|||
# [ 8, 2, 5, 9, 1, 3, 6, 7, 4 ]</lang> |
|||
=={{header|Go}}== |
=={{header|Go}}== |
||
Line 854: | Line 855: | ||
nums.sort(); |
nums.sort(); |
||
}</lang> |
}</lang> |
||
=={{header|IDL}}== |
|||
<lang idl>result = array[sort(array)]</lang> |
|||
=={{header|Icon}} and {{header|Unicon}}== |
=={{header|Icon}} and {{header|Unicon}}== |
||
Line 863: | Line 861: | ||
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. |
||
<lang Icon>S := sort(L:= [63, 92, 51, 92, 39, 15, 43, 89, 36, 69]) # will sort a list</lang> |
<lang Icon>S := sort(L:= [63, 92, 51, 92, 39, 15, 43, 89, 36, 69]) # will sort a list</lang> |
||
=={{header|IDL}}== |
|||
<lang idl>result = array[sort(array)]</lang> |
|||
=={{header|Inform 7}}== |
=={{header|Inform 7}}== |
||
Line 922: | Line 923: | ||
numbers.sort(int_arr); |
numbers.sort(int_arr); |
||
document.write(numbers);</lang> |
document.write(numbers);</lang> |
||
=={{header|Kotlin}}== |
|||
<lang scala>// version 1.0.6 |
|||
fun main(args: Array<String>) { |
|||
val ints = intArrayOf(6, 2, 7, 8, 3, 1, 10, 5, 4, 9) |
|||
ints.sort() |
|||
println(ints.joinToString(prefix = "[", postfix = "]")) |
|||
}</lang> |
|||
{{out}} |
|||
<pre> |
|||
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] |
|||
</pre> |
|||
=={{header|Lasso}}== |
|||
<lang Lasso>local(array) = array(5,20,3,2,6,1,4) |
|||
#array->sort |
|||
#array // 1, 2, 3, 4, 5, 6, 20 |
|||
// Reverse the sort order |
|||
#array->sort(false) |
|||
#array // 20, 6, 5, 4, 3, 2, 1</lang> |
|||
=={{header|jq}}== |
=={{header|jq}}== |
||
Line 993: | Line 971: | ||
srt num |
srt num |
||
0 1 2 3 4 5 6 7 8 9</lang> |
0 1 2 3 4 5 6 7 8 9</lang> |
||
=={{header|Kotlin}}== |
|||
<lang scala>// version 1.0.6 |
|||
fun main(args: Array<String>) { |
|||
val ints = intArrayOf(6, 2, 7, 8, 3, 1, 10, 5, 4, 9) |
|||
ints.sort() |
|||
println(ints.joinToString(prefix = "[", postfix = "]")) |
|||
}</lang> |
|||
{{out}} |
|||
<pre> |
|||
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] |
|||
</pre> |
|||
=={{header|Lasso}}== |
|||
<lang Lasso>local(array) = array(5,20,3,2,6,1,4) |
|||
#array->sort |
|||
#array // 1, 2, 3, 4, 5, 6, 20 |
|||
// Reverse the sort order |
|||
#array->sort(false) |
|||
#array // 20, 6, 5, 4, 3, 2, 1</lang> |
|||
=={{header|Liberty BASIC}}== |
=={{header|Liberty BASIC}}== |
||
Line 1,416: | Line 1,417: | ||
<lang Niue>2 6 1 0 3 8 sort .s |
<lang Niue>2 6 1 0 3 8 sort .s |
||
0 1 2 3 6 8</lang> |
0 1 2 3 6 8</lang> |
||
=={{header|Objective-C}}== |
|||
<lang objc>NSArray *nums = @[@2, @4, @3, @1, @2]; |
|||
NSArray *sorted = [nums sortedArrayUsingSelector:@selector(compare:)];</lang> |
|||
=={{header|Objeck}}== |
=={{header|Objeck}}== |
||
Line 1,430: | Line 1,427: | ||
} |
} |
||
}</lang> |
}</lang> |
||
=={{header|Objective-C}}== |
|||
<lang objc>NSArray *nums = @[@2, @4, @3, @1, @2]; |
|||
NSArray *sorted = [nums sortedArrayUsingSelector:@selector(compare:)];</lang> |
|||
=={{header|OCaml}}== |
=={{header|OCaml}}== |
||
Line 1,520: | Line 1,521: | ||
<lang perl>@nums = (2,4,3,1,2); |
<lang perl>@nums = (2,4,3,1,2); |
||
@sorted = sort {$a <=> $b} @nums;</lang> |
@sorted = sort {$a <=> $b} @nums;</lang> |
||
=={{header|Perl 6}}== |
|||
If <code>@a</code> contains only numbers: |
|||
<lang perl6>my @sorted = sort @a;</lang> |
|||
For an in-place sort: |
|||
<lang perl6>@a .= sort;</lang> |
|||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
Line 1,669: | Line 1,661: | ||
'(1 2 3 4 5 6 7 8 9) |
'(1 2 3 4 5 6 7 8 9) |
||
</lang> |
</lang> |
||
=={{header|Raku}}== |
|||
(formerly Perl 6) |
|||
If <code>@a</code> contains only numbers: |
|||
<lang perl6>my @sorted = sort @a;</lang> |
|||
For an in-place sort: |
|||
<lang perl6>@a .= sort;</lang> |
|||
=={{header|Rascal}}== |
=={{header|Rascal}}== |