Sort an integer array: Difference between revisions
added Fantom example |
|||
Line 212: | Line 212: | ||
=={{header|D}}== |
=={{header|D}}== |
||
<lang d> |
<lang d>import std.stdio, std.algorithm; |
||
⚫ | |||
void main() { |
|||
nums.sort; // Sort in-place</lang> |
|||
auto data = [2, 4, 3, 1, 2]; |
|||
data.sort; // in-place sort |
|||
assert(data == [1, 2, 2, 3, 4]); |
|||
data = [2, 4, 3, 1, 2]; |
|||
⚫ | |||
assert(data == [2, 4, 3, 1, 2]); |
|||
assert(sdata == [1, 2, 2, 3, 4]); |
|||
data = [2, 4, 3, 1, 2]; |
|||
data.sort(); // in-place std.algorithm.sort |
|||
assert(data == [1, 2, 2, 3, 4]); |
|||
}</lang> |
|||
=={{header|E}}== |
=={{header|E}}== |
Revision as of 20:22, 8 February 2011
You are encouraged to solve this task according to the task description, using any language you may know.
Sort an array (or list) of integers in ascending numerical order. Use a sorting facility provided by the language/library if possible.
4D
English
<lang 4d>ARRAY INTEGER($nums;0) APPEND TO ARRAY($nums;2) APPEND TO ARRAY($nums;4) APPEND TO ARRAY($nums;3) APPEND TO ARRAY($nums;1) APPEND TO ARRAY($nums;2) SORT ARRAY($nums) ` sort in ascending order SORT ARRAY($nums;<) ` sort in descending order</lang>
Français
<lang 4d>TABLEAU ENTIER($nombres;0) AJOUTER A TABLEAU($nombres;2) AJOUTER A TABLEAU($nombres;4) AJOUTER A TABLEAU($nombres;3) AJOUTER A TABLEAU($nombres;1) AJOUTER A TABLEAU($nombres;2) TRIER TABLEAU($nombres) ` pour effectuer un tri par ordre croissant TRIER TABLEAU($nombres;<) ` pour effectuer un tri par ordre décroissant</lang>
ActionScript
<lang ActionScript>//Comparison function must returns Numbers even though it deals with integers. function compare(x:int, y:int):Number { return Number(x-y); } var nums:Vector.<int> = Vector.<int>([5,12,3,612,31,523,1,234,2]); nums.sort(compare);</lang>
Ada
<lang ada>with Gnat.Heap_Sort_G;
procedure Integer_Sort is
-- Heap sort package requires data to be in index values starting at -- 1 while index value 0 is used as temporary storage type Int_Array is array(Natural range <>) of Integer; Values : Int_Array := (0,1,8,2,7,3,6,4,5); -- define move and less than subprograms for use by the heap sort package procedure Move_Int(From : Natural; To : Natural) is begin Values(To) := Values(From); end Move_Int; function Lt_Int(Left, Right : Natural) return Boolean is begin return Values(Left) < Values (Right); end Lt_Int; -- Instantiate the generic heap sort package package Heap_Sort is new Gnat.Heap_Sort_G(Move_Int, Lt_Int);
begin
Heap_Sort.Sort(8);
end Integer_Sort;
requires an Ada05 compiler, e.g GNAT GPL 2007 with Ada.Containers.Generic_Array_Sort;
procedure Integer_Sort is
-- type Int_Array is array(Natural range <>) of Integer; Values : Int_Array := (0,1,8,2,7,3,6,4,5); -- Instantiate the generic sort package from the standard Ada library procedure Sort is new Ada.Containers.Generic_Array_Sort (Index_Type => Natural, Element_Type => Integer, Array_Type => Int_Array);
begin
Sort(Values);
end Integer_Sort;</lang>
ALGOL 68
<lang algol68>CO PR READ "shell_sort.a68" PR CO MODE TYPE = INT;
PROC in place shell sort = (REF[]TYPE seq)REF[]TYPE:(
INT inc := ( UPB seq + LWB seq + 1 ) OVER 2; WHILE inc NE 0 DO FOR index FROM LWB seq TO UPB seq DO INT i := index; TYPE el = seq[i]; WHILE ( i - LWB seq >= inc | seq[i - inc] > el | FALSE ) DO seq[i] := seq[i - inc]; i -:= inc OD; seq[i] := el OD; inc := IF inc = 2 THEN 1 ELSE ENTIER(inc * 5 / 11) FI OD; seq
);
PROC shell sort = ([]TYPE seq)[]TYPE:
in place shell sort(LOC[LWB seq: UPB seq]TYPE:=seq);
print((shell sort((2, 4, 3, 1, 2)), new line))</lang> Output:
+1 +2 +2 +3 +4
APL
<lang apl> X←63 92 51 92 39 15 43 89 36 69
X[⍋X]
15 36 39 43 51 63 69 89 92 92</lang>
AutoHotkey
<lang AutoHotkey>numbers = 5 4 1 2 3 sort, numbers, N D%A_Space% Msgbox % numbers</lang>
C
<lang c>#include <stdlib.h>
int intcmp(const void *a, const void *b) {
return *(int *)a - *(int *)b;
}
int main() {
int nums[5] = {2,4,3,1,2}; qsort(nums, 5, sizeof(int), intcmp);
}</lang>
C++
Simple Array
<lang cpp>#include <algorithm>
int main() {
int nums[] = {2,4,3,1,2}; std::sort(nums, nums+5);
}</lang>
std::vector
<lang cpp>#include <algorithm>
- include <vector>
int main() {
std::vector<int> nums; nums.push_back(2); nums.push_back(4); nums.push_back(3); nums.push_back(1); nums.push_back(2); std::sort(nums.begin(), nums.end());
}</lang>
std::list
<lang cpp>#include <list>
int main() {
std::list<int> nums; nums.push_back(2); nums.push_back(4); nums.push_back(3); nums.push_back(1); nums.push_back(2); nums.sort();
}</lang>
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>
Clean
We use list and array comprehensions to convert an array to and from a list in order to use the built-in sort on lists. <lang clean>import StdEnv
sortArray :: (a e) -> a e | Array a e & Ord e sortArray array = {y \\ y <- sort [x \\ x <-: array]}
Start :: {#Int} Start = sortArray {2, 4, 3, 1, 2}</lang>
Clojure
<lang clojure>(sort [5 4 3 2 1]) ; sort can also take a comparator function (1 2 3 4 5)</lang>
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: <lang lisp>CL-USER> (sort #(9 -2 1 2 8 0 1 2) #'<)
- (-2 0 1 1 2 2 8 9)</lang>
D
<lang d>import std.stdio, std.algorithm;
void main() {
auto data = [2, 4, 3, 1, 2]; data.sort; // in-place sort assert(data == [1, 2, 2, 3, 4]);
data = [2, 4, 3, 1, 2]; auto sdata = data.dup.sort; // sort assert(data == [2, 4, 3, 1, 2]); assert(sdata == [1, 2, 2, 3, 4]);
data = [2, 4, 3, 1, 2]; data.sort(); // in-place std.algorithm.sort assert(data == [1, 2, 2, 3, 4]);
}</lang>
E
<lang e>[2,4,3,1,2].sort()</lang>
Erlang
<lang erlang>List = [2, 4, 3, 1, 2]. SortedList = lists:sort(List).</lang>
Factor
<lang factor>{ 1 4 9 2 3 0 5 } natural-sort .</lang>
Fantom
The List collection contains a sort method which uses the usual comparison method for the data in the list; the sort is done 'in place'.
fansh> a := [5, 1, 4, 2, 3] [5, 1, 4, 2, 3] fansh> a.sort [1, 2, 3, 4, 5] fansh> a [1, 2, 3, 4, 5]
Forth
<lang forth>create test-data 2 , 4 , 3 , 1 , 2 , test-data 5 cell-sort</lang>
Fortran
<lang fortran>CALL ISORT@(b, a, n) ! n = number of elements ! a = array to be sorted ! b = array of indices of a. b(1) 'points' to the minimum value etc.</lang>
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>
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>
Go
<lang go>package main import "fmt" import "sort"
func main() {
nums := []int {2, 4, 3, 1, 2} sort.SortInts(nums) fmt.Println(nums)
}</lang>
Golfscript
<lang golfscript>[2 4 3 1 2]$</lang>
Groovy
<lang groovy>println ([2,4,0,3,1,2,-12].sort())</lang>
Output:
[-12, 0, 1, 2, 2, 3, 4]
Haskell
<lang haskell>nums = [2,4,3,1,2] :: [Int] sorted = List.sort nums</lang>
HicEst
<lang hicest>DIMENSION array(100)
array = INT( RAN(100) ) SORT(Vector=array, Sorted=array) </lang>
IDL
<lang idl>result = array[sort(array)]</lang>
Icon and Unicon
Icon and Unicon lists allow mixed type and the built-in function 'sort' will deal with mixed type arrays by sorting by type first then value. Integers sort before, reals, strings, lists, tables, etc. As a result a list of mixed numeric valuess (i.e. integers and reals) will not sort by numeric value, rather the reals will appear after the integers. Sort returns a sorted copy of it's argument. It will also perform some type conversion, such converting an unordered set into an ordered list.
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>
Inform 7
<lang inform7>let L be {5, 4, 7, 1, 18}; sort L;</lang>
J
<lang j>/:~</lang> The verb /:~ sorts anything. For example:
<lang j> ] a=: 10 ?@$ 100 NB. random vector 63 92 51 92 39 15 43 89 36 69
/:~ a
15 36 39 43 51 63 69 89 92 92</lang>
Arrays of any rank are treated as lists of component arrays. Thus /:~ 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 /:~"1 sorts the atoms within the finest-grained list within the array, regardless of the overall rank of the array.
This code also applies to any data type.
Java
Array
<lang java>import java.util.Arrays;
public class example {
public static void main(String[] args) { int[] nums = {2,4,3,1,2}; Arrays.sort(nums); }
}</lang>
List
<lang java5>import java.util.Arrays; import java.util.Collections; import java.util.List;
public class example {
public static void main(String[] args) { List<Integer> nums = Arrays.asList(2,4,3,1,2); Collections.sort(nums); }
}</lang>
JavaScript
JavaScript sorts lexically by default, so "10000" comes before "2". To sort numerically, a custom comparator is used.
<lang javascript>function numberSorter(a, b) {
return a - b;
} var numbers = [20, 7, 65, 10, 3, 0, 8, -60]; numbers.sort(numberSorter); alert( numbers );</lang>
Io
<lang lua>mums := list(2,4,3,1,2) sorted := nums sort # returns a new sorted array. 'nums' is unchanged nums sortInPlace # sort 'nums' "in-place"</lang>
Lua
<lang lua>t = {4, 5, 2} table.sort(t) print(unpack(t))</lang>
Mathematica
<lang mathematica>numbers = Sort[{2,4,3,1,2}]</lang>
MATLAB
<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)</lang>
MAXScript
<lang maxscript>arr = #(5, 4, 3, 2, 1) arr = sort arr</lang>
Modula-3
Modula-3 provides a generic ArraySort module, as well as an instance of that module for integers called IntArraySort. <lang modula3>MODULE ArraySort EXPORTS Main;
IMPORT IntArraySort;
VAR arr := ARRAY [1..10] OF INTEGER{3, 6, 1, 2, 10, 7, 9, 4, 8, 5};
BEGIN
IntArraySort.Sort(arr);
END ArraySort.</lang>
MUMPS
<lang MUMPS>SORTARRAY(X,SEP)
;X is the list of items to sort ;X1 is the temporary array ;SEP is the separator string between items in the list X ;Y is the returned list ;This routine uses the inherent sorting of the arrays NEW I,X1,Y SET Y="" FOR I=1:1:$LENGTH(X,SEP) SET X1($PIECE(X,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 QUIT Y</lang>
Output:
USER>W $$SORTARRAY^ROSETTA("3,5,1,99,27,16,0,-1",",") -1,0,1,3,5,16,27,99
Nial
<lang nial>sort >= 9 6 8 7 1 10 = 10 9 8 7 6 1</lang>
Niue
Library <lang Niue>2 6 1 0 3 8 sort .s 0 1 2 3 6 8 </lang>
Objective-C
<lang objc>- (void)example {
NSArray *nums, *sorted; nums = [NSArray arrayWithObjects: [NSNumber numberWithInt:2], [NSNumber numberWithInt:4], [NSNumber numberWithInt:3], [NSNumber numberWithInt:1], [NSNumber numberWithInt:2], nil]; sorted = [nums sortedArrayUsingSelector:@selector(compare:)];
}</lang>
Objeck
<lang objeck>bundle Default {
class Sort { function : Main(args : System.String[]) ~ Nil { nums := Structure.IntVector->New([2,4,3,1,2]); nums->Sort(); } }
}</lang>
OCaml
Array
<lang ocaml>let nums = [|2; 4; 3; 1; 2|] Array.sort compare nums</lang>
List
<lang ocaml>let nums = [2; 4; 3; 1; 2] let sorted = List.sort compare nums</lang>
Octave
The variable v can be a vector or a matrix (columns will be sorted).
<lang octave>sortedv = sort(v);</lang>
Oz
<lang oz>declare
Nums = [2 4 3 1 2] Sorted = {List.sort Nums Value.'<'}
in
{Show Sorted}</lang>
PARI/GP
<lang>vecsort(v)</lang>
Perl
<lang perl>@nums = (2,4,3,1,2); @sorted = sort {$a <=> $b} @nums;</lang>
Perl 6
If @a
contains only numbers:
<lang perl6>my @sorted = sort @a;</lang>
If some elements of @a
are strings or are otherwise non-numeric but you want to treat them as numbers:
<lang perl6>my @sorted = sort +*, @a;</lang>
For an in-place sort:
<lang perl6>@a .= sort;</lang>
PHP
<lang php><?php $nums = array(2,4,3,1,2); sort($nums); ?></lang>
PicoLisp
The sort function in PicoLisp returns already by default an ascending list (of any type, not only integers): <lang PicoLisp>(sort (2 4 3 1 2)) -> (1 2 2 3 4)</lang>
PL/I
<lang pli>DCL (T(10)) FIXED BIN(31); /* scratch space of length N/2 */
MERGE: PROCEDURE (A,LA,B,LB,C);
DECLARE (A(*),B(*),C(*)) FIXED BIN(31); DECLARE (LA,LB) FIXED BIN(31) NONASGN; DECLARE (I,J,K) FIXED BIN(31); I=1; J=1; K=1; DO WHILE ((I <= LA) & (J <= LB)); IF(A(I) <= B(J)) THEN DO; C(K)=A(I); K=K+1; I=I+1; END; ELSE DO; C(K)=B(J); K=K+1; J=J+1; END; END; DO WHILE (I <= LA); C(K)=A(I); I=I+1; K=K+1; END; RETURN;
END MERGE;
MERGESORT: PROCEDURE (A,N) RECURSIVE ;
DECLARE (A(*)) FIXED BINARY(31); DECLARE N FIXED BINARY(31) NONASGN; DECLARE Temp FIXED BINARY; DECLARE (M,I) FIXED BINARY; DECLARE AMP1(N) FIXED BINARY(31) BASED(P); DECLARE P POINTER; IF (N=1) THEN RETURN; M = trunc((N+1)/2); IF (M>1) THEN CALL MERGESORT(A,M); P=ADDR(A(M+1)); IF (N-M > 1) THEN CALL MERGESORT(AMP1,N-M); IF A(M) <= AMP1(1) THEN RETURN; DO I=1 to M; T(I)=A(I); END; CALL MERGE(T,M,AMP1,N-M,A); RETURN;
END MERGESORT;</lang>
Pop11
Pop11 library function sorts lists. So we first convert array to list, then sort and finally convert back:
<lang pop11>lvars ar = {2 4 3 1 2};
- Convert array to list.
- destvector leaves its results and on the pop11 stack + an integer saying how many there were
destvector(ar);
- conslist uses the items left on the stack plus the integer, to make a list of those items.
lvars ls = conslist();
- Sort it
sort(ls) -> ls;
- Convert list to array
destlist(ls); consvector() -> ar;</lang>
The above can be abbreviated to more economical, but possibly more opaque, syntax, using pop11 as a functional language:
<lang pop11>lvars ar = {2 4 3 1 2}; consvector(destlist(sort(conslist(destvector(ar))))) -> ar;
- print the sorted vector
ar =>
- {1 2 2 3 4}</lang>
(The list created by conslist will be garbage-collected.)
Alternatively, using the datalist function, even more economically:
<lang pop11>lvars ar = {2 4 3 1 2}; consvector(destlist(sort(datalist(ar)))) -> ar;</lang>
or in Forth-like pop11 postfix syntax:
<lang pop11>lvars ar = {2 4 3 1 2}; ar.datalist.sort.destlist.consvector -> ar;</lang>
PowerShell
<lang powershell>34,12,23,56,1,129,4,2,73 | Sort-Object</lang>
PureBasic
<lang PureBasic>Dim numbers(20) For i = 0 To 20
numbers(i) = Random(1000)
Next
SortArray(numbers(), #PB_Sort_Ascending)</lang>
Python
<lang python>nums = [2,4,3,1,2] nums.sort()</lang>
Note: The array nums is sorted in place.
Interpreter: Python 2.4 (and above)
You could also use the built-in sorted() function
<lang python>nums = sorted([2,4,3,1,2])</lang>
R
<lang r>nums <- c(2,4,3,1,2) sorted <- sort(nums)</lang>
Raven
Sort list in place:
<lang raven>[ 2 4 3 1 2 ] sort</lang>
REBOL
<lang rebol>sort [2 4 3 1 2]</lang>
REXX
This version creates an array with 20 random integers, then sorts it. <lang rexx> /*REXX program to sort an integer array.*/
numeric digits 20 /*handle larger numbers.*/ a.1= 1 a.2= 0 a.3= -1 a.4= 0 a.5= 5 a.6= 0 a.7= -61 a.8= 0 a.9= 1385
a.10= 0 a.11= -50521 a.12= 0 a.13= 2702765 a.14= 0 a.15= -199360981 a.16= 0 a.17= 19391512145 a.18= 0 a.19= -2404879675441 a.20= 0 a.21= 370371188237525
size=21 /*we have a list of 21 Euler numbers.*/ call tell 'un-sorted' a.0=size call esort 1,size call tell ' sorted' exit
/*----------------------------------ESORT subroutine--------------------*/
esort: procedure expose a.; h=a.0
do while h>1; h=h%2 do i=1 for a.0-h; j=i; k=h+i do while a.k<a.j t=a.j; a.j=a.k; a.k=t; if h>=j then leave; j=j-h; k=k-h end end end
return
/*----------------------------------TELL subroutine---------------------*/
tell: say center(arg(1),40,'-')
do j=1 for size say arg(1) 'array element' right(j,length(size))'='right(a.j,20) end
say return </lang> Output:
---------------un-sorted---------------- un-sorted array element 1= 1 un-sorted array element 2= 0 un-sorted array element 3= -1 un-sorted array element 4= 0 un-sorted array element 5= 5 un-sorted array element 6= 0 un-sorted array element 7= -61 un-sorted array element 8= 0 un-sorted array element 9= 1385 un-sorted array element 10= 0 un-sorted array element 11= -50521 un-sorted array element 12= 0 un-sorted array element 13= 2702765 un-sorted array element 14= 0 un-sorted array element 15= -199360981 un-sorted array element 16= 0 un-sorted array element 17= 19391512145 un-sorted array element 18= 0 un-sorted array element 19= -2404879675441 un-sorted array element 20= 0 un-sorted array element 21= 370371188237525 --------------- sorted---------------- sorted array element 1= -2404879675441 sorted array element 2= -199360981 sorted array element 3= -50521 sorted array element 4= -61 sorted array element 5= -1 sorted array element 6= 0 sorted array element 7= 0 sorted array element 8= 0 sorted array element 9= 0 sorted array element 10= 0 sorted array element 11= 0 sorted array element 12= 0 sorted array element 13= 0 sorted array element 14= 0 sorted array element 15= 0 sorted array element 16= 1 sorted array element 17= 5 sorted array element 18= 1385 sorted array element 19= 2702765 sorted array element 20= 19391512145 sorted array element 21= 370371188237525
This version creates a list with a bunch of interesting integers,
then sorts it.
Because it so much more efficient to sort an array, an array is
built from the list, sorted, than the list is re-constituted.
<lang rexx>
/*REXX program to sort an interesting integer list.*/
bell='1 1 2 5 15 52 203 877 4140 21147 115975' /*some Bell numbers.*/ bern='1 -1 1 0 -1 0 1 0 -1 0 5 0 -691 0 7 0 -3617' /*some Bernoulli num*/ perrin='3 0 2 3 2 5 5 7 10 12 17 22 29 39 51 68 90' /*some Perrin nums. */ list=bell bern perrin /*combine the three.*/
size=words(list)
do j=1 for size a.j=word(list,j) end
say ' as is='list a.0=size call esort 1,size bList=
do j=1 for size bList=bList a.j end
blist=strip(bList) say 'sorted='bList exit
/*----------------------------------ESORT subroutine--------------------*/
esort:procedure expose a.;h=a.0
do while h>1;h=h%2 do i=1 for a.0-h;j=i; k=h+i do while a.k<a.j;_=a.j;a.j=a.k;a.k=_;if h>=j then leave;j=j-h;k=k-h;end end end
return </lang> Output:
as is=1 1 2 5 15 52 203 877 4140 21147 115975 1 -1 1 0 -1 0 1 0 -1 0 5 0 -691 0 7 0 -3617 3 0 2 3 2 5 5 7 10 12 17 22 29 39 51 68 90 sorted=-3617 -691 -1 -1 -1 0 0 0 0 0 0 0 0 1 1 1 1 1 2 2 2 3 3 5 5 5 5 7 7 10 12 15 17 22 29 39 51 52 68 90 203 877 4140 21147 115975
Ruby
<lang ruby>nums = [2,4,3,1,2] sorted = nums.sort # returns a new sorted array. 'nums' is unchanged nums.sort! # sort 'nums' "in-place"</lang>
Scala
<lang scala>println(List(5,2,78,2,578,-42).sortWith(_<_)) //--> List(-42, 2, 2, 5, 78, 578)</lang>
Seed7
<lang seed7>var array integer: nums is [] (2, 4, 3, 1, 2);
nums := sort(nums);</lang>
Slate
<lang slate> #(7 5 2 9 0 -1) sort</lang>
Smalltalk
<lang smalltalk> #(7 5 2 9 0 -1) asSortedCollection</lang>
Standard ML
Array
<lang sml>val nums = Array.fromList [2, 4, 3, 1, 2]; ArrayQSort.sort Int.compare nums;</lang>
List
<lang sml>val nums = [2, 4, 3, 1, 2]; val sorted = ListMergeSort.sort (op >) nums;</lang>
Tcl
<lang tcl>set result [lsort -integer $unsorted_list]</lang>
TI-83 BASIC
Store input into L1, run prgmSORTBTIN, and L2 will be L1, only sorted.
:L1→L2 :SortA(L2)
SortA is found via: [LIST] → ENTER. SortD is also available for a descending sort.
Toka
This can be done by using the bubble sort library:
<lang toka>needs bsort arrayname number_elements bsort</lang>
See the Toka entry on Bubble Sort for a full example.
UNIX Shell
<lang bash>nums=(2 4 3 1 2) sorted=($(for i in ${nums[*]}; do echo $i; done | sort -n))</lang> Another way: <lang bash>nums=(2 4 3 1 5) sorted=($(echo "${nums[@]}" | tr ' ' '\12' | sort -n))</lang>
Ursala
using the built in sort operator, -<, with the nleq library function for comparing natural numbers <lang Ursala>#import nat
- cast %nL
example = nleq-< <39,47,40,53,14,23,88,52,78,62,41,92,88,66,5,40></lang> output:
<5,14,23,39,40,40,41,47,52,53,62,66,78,88,88,92>
Yorick
In Yorick, sort returns an index list into the array that will put it in sorted order. <lang yorick> nums = [2,4,3,1,2]; nums = nums(sort(nums)); </lang>
- Programming Tasks
- Sorting
- 4D
- ActionScript
- Ada
- ALGOL 68
- APL
- AutoHotkey
- C
- C++
- C sharp
- Clean
- Clojure
- Common Lisp
- D
- E
- Erlang
- Factor
- Fantom
- Forth
- Fortran
- F Sharp
- GAP
- Go
- Golfscript
- Groovy
- Haskell
- HicEst
- IDL
- Icon
- Unicon
- Inform 7
- J
- Java
- JavaScript
- Io
- Lua
- Mathematica
- MATLAB
- MAXScript
- Modula-3
- MUMPS
- Nial
- Niue
- Objective-C
- Objeck
- OCaml
- Octave
- Oz
- PARI/GP
- Perl
- Perl 6
- PHP
- PicoLisp
- PL/I
- Pop11
- PowerShell
- PureBasic
- Python
- R
- Raven
- REBOL
- REXX
- Ruby
- Scala
- Seed7
- Slate
- Smalltalk
- Standard ML
- Tcl
- TI-83 BASIC
- Toka
- UNIX Shell
- Ursala
- Yorick