all tasks
This commit is contained in:
parent
b83f433714
commit
68f8f3e56b
14735 changed files with 178959 additions and 0 deletions
1
Task/Sort-an-integer-array/0DESCRIPTION
Normal file
1
Task/Sort-an-integer-array/0DESCRIPTION
Normal file
|
|
@ -0,0 +1 @@
|
|||
Sort an array (or list) of integers in ascending numerical order. Use a sorting facility provided by the language/library if possible.
|
||||
2
Task/Sort-an-integer-array/1META.yaml
Normal file
2
Task/Sort-an-integer-array/1META.yaml
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
---
|
||||
note: Sorting
|
||||
8
Task/Sort-an-integer-array/4D/sort-an-integer-array-1.4d
Normal file
8
Task/Sort-an-integer-array/4D/sort-an-integer-array-1.4d
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
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
|
||||
8
Task/Sort-an-integer-array/4D/sort-an-integer-array-2.4d
Normal file
8
Task/Sort-an-integer-array/4D/sort-an-integer-array-2.4d
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
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
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
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))
|
||||
3
Task/Sort-an-integer-array/APL/sort-an-integer-array.apl
Normal file
3
Task/Sort-an-integer-array/APL/sort-an-integer-array.apl
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
X←63 92 51 92 39 15 43 89 36 69
|
||||
X[⍋X]
|
||||
15 36 39 43 51 63 69 89 92 92
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
//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);
|
||||
43
Task/Sort-an-integer-array/Ada/sort-an-integer-array.ada
Normal file
43
Task/Sort-an-integer-array/Ada/sort-an-integer-array.ada
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
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;
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
numbers = 5 4 1 2 3
|
||||
sort, numbers, N D%A_Space%
|
||||
Msgbox % numbers
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
INSTALL @lib$+"SORTLIB"
|
||||
sort% = FN_sortinit(0,0)
|
||||
|
||||
DIM array(8)
|
||||
array() = 8, 2, 5, 9, 1, 3, 6, 7, 4
|
||||
|
||||
C% = DIM(array(),1) + 1
|
||||
CALL sort%, array(0)
|
||||
|
||||
FOR i% = 0 TO DIM(array(),1) - 1
|
||||
PRINT ; array(i%) ", ";
|
||||
NEXT
|
||||
PRINT ; array(i%)
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
v
|
||||
> 543** > :#v_ $&> :#v_ 1 > :0g > :#v_ $ 1+: 543** `! #v_ 25*,@
|
||||
^-1p0\0:< ^-1 p0\+1 g0:&< ^-1\.:\<
|
||||
^ <
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
{sort takes a list of space-separated integers}
|
||||
(sort=
|
||||
sum elem sorted n
|
||||
. 0:?sum
|
||||
& whl
|
||||
' (!arg:%?elem ?arg&(!elem.)+!sum:?sum)
|
||||
& :?sorted
|
||||
& whl
|
||||
' ( !sum:?n*(?elem.)+?sum
|
||||
& whl
|
||||
' ( !n+-1:~<0:?n
|
||||
& !sorted !elem:?sorted
|
||||
)
|
||||
)
|
||||
& !sorted);
|
||||
|
||||
out$sort$(9 -2 1 2 8 0 1 2);
|
||||
|
|
@ -0,0 +1 @@
|
|||
{1 3 2 5 4}><
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
#include <algorithm>
|
||||
|
||||
int main()
|
||||
{
|
||||
int nums[] = {2,4,3,1,2};
|
||||
std::sort(nums, nums+5);
|
||||
return 0;
|
||||
}
|
||||
14
Task/Sort-an-integer-array/C++/sort-an-integer-array-2.cpp
Normal file
14
Task/Sort-an-integer-array/C++/sort-an-integer-array-2.cpp
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
#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());
|
||||
return 0;
|
||||
}
|
||||
13
Task/Sort-an-integer-array/C++/sort-an-integer-array-3.cpp
Normal file
13
Task/Sort-an-integer-array/C++/sort-an-integer-array-3.cpp
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
#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();
|
||||
return 0;
|
||||
}
|
||||
17
Task/Sort-an-integer-array/C/sort-an-integer-array.c
Normal file
17
Task/Sort-an-integer-array/C/sort-an-integer-array.c
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
#include <stdlib.h> /* qsort() */
|
||||
#include <stdio.h> /* printf() */
|
||||
|
||||
int intcmp(const void *aa, const void *bb)
|
||||
{
|
||||
const int *a = aa, *b = bb;
|
||||
return (*a < *b) ? -1 : (*a > *b);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int nums[5] = {2,4,3,1,2};
|
||||
qsort(nums, 5, sizeof(int), intcmp);
|
||||
printf("result: %d %d %d %d %d\n",
|
||||
nums[0], nums[1], nums[2], nums[3], nums[4]);
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
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}
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
(sort [5 4 3 2 1]) ; sort can also take a comparator function
|
||||
(1 2 3 4 5)
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
CL-USER> (sort #(9 -2 1 2 8 0 1 2) #'<)
|
||||
#(-2 0 1 1 2 2 8 9)
|
||||
7
Task/Sort-an-integer-array/D/sort-an-integer-array.d
Normal file
7
Task/Sort-an-integer-array/D/sort-an-integer-array.d
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
import std.stdio, std.algorithm;
|
||||
|
||||
void main() {
|
||||
auto data = [2, 4, 3, 1, 2];
|
||||
data.sort(); // in-place
|
||||
assert(data == [1, 2, 2, 3, 4]);
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
uses Types, Generics.Collections;
|
||||
|
||||
var
|
||||
a: TIntegerDynArray;
|
||||
begin
|
||||
a := TIntegerDynArray.Create(5, 4, 3, 2, 1);
|
||||
TArray.Sort<Integer>(a);
|
||||
1
Task/Sort-an-integer-array/E/sort-an-integer-array.e
Normal file
1
Task/Sort-an-integer-array/E/sort-an-integer-array.e
Normal file
|
|
@ -0,0 +1 @@
|
|||
[2,4,3,1,2].sort()
|
||||
16
Task/Sort-an-integer-array/EGL/sort-an-integer-array-1.egl
Normal file
16
Task/Sort-an-integer-array/EGL/sort-an-integer-array-1.egl
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
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
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
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
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
List = [2, 4, 3, 1, 2].
|
||||
SortedList = lists:sort(List).
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
include sort.e
|
||||
print(1,sort({20, 7, 65, 10, 3, 0, 8, -60}))
|
||||
|
|
@ -0,0 +1 @@
|
|||
{ 1 4 9 2 3 0 5 } natural-sort .
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
create test-data 2 , 4 , 3 , 1 , 2 ,
|
||||
test-data 5 cell-sort
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
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.
|
||||
14
Task/Sort-an-integer-array/GAP/sort-an-integer-array.gap
Normal file
14
Task/Sort-an-integer-array/GAP/sort-an-integer-array.gap
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
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 ]
|
||||
9
Task/Sort-an-integer-array/Go/sort-an-integer-array.go
Normal file
9
Task/Sort-an-integer-array/Go/sort-an-integer-array.go
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
package main
|
||||
import "fmt"
|
||||
import "sort"
|
||||
|
||||
func main() {
|
||||
nums := []int {2, 4, 3, 1, 2}
|
||||
sort.Ints(nums)
|
||||
fmt.Println(nums)
|
||||
}
|
||||
|
|
@ -0,0 +1 @@
|
|||
[2 4 3 1 2]$
|
||||
|
|
@ -0,0 +1 @@
|
|||
println ([2,4,0,3,1,2,-12].sort())
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
nums = [2,4,3,1,2] :: [Int]
|
||||
sorted = List.sort nums
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
DIMENSION array(100)
|
||||
|
||||
array = INT( RAN(100) )
|
||||
SORT(Vector=array, Sorted=array)
|
||||
1
Task/Sort-an-integer-array/IDL/sort-an-integer-array.idl
Normal file
1
Task/Sort-an-integer-array/IDL/sort-an-integer-array.idl
Normal file
|
|
@ -0,0 +1 @@
|
|||
result = array[sort(array)]
|
||||
|
|
@ -0,0 +1 @@
|
|||
S := sort(L:= [63, 92, 51, 92, 39, 15, 43, 89, 36, 69]) # will sort a list
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
let L be {5, 4, 7, 1, 18};
|
||||
sort L;
|
||||
3
Task/Sort-an-integer-array/Io/sort-an-integer-array.io
Normal file
3
Task/Sort-an-integer-array/Io/sort-an-integer-array.io
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
mums := list(2,4,3,1,2)
|
||||
sorted := nums sort # returns a new sorted array. 'nums' is unchanged
|
||||
nums sortInPlace # sort 'nums' "in-place"
|
||||
1
Task/Sort-an-integer-array/J/sort-an-integer-array-1.j
Normal file
1
Task/Sort-an-integer-array/J/sort-an-integer-array-1.j
Normal file
|
|
@ -0,0 +1 @@
|
|||
/:~
|
||||
4
Task/Sort-an-integer-array/J/sort-an-integer-array-2.j
Normal file
4
Task/Sort-an-integer-array/J/sort-an-integer-array-2.j
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
] 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
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
import java.util.Arrays;
|
||||
|
||||
public class example {
|
||||
public static void main(String[] args)
|
||||
{
|
||||
int[] nums = {2,4,3,1,2};
|
||||
Arrays.sort(nums);
|
||||
}
|
||||
}
|
||||
11
Task/Sort-an-integer-array/Java/sort-an-integer-array-2.java
Normal file
11
Task/Sort-an-integer-array/Java/sort-an-integer-array-2.java
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
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);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
function int_arr(a, b) {
|
||||
return a - b;
|
||||
}
|
||||
var numbers = [20, 7, 65, 10, 3, 0, 8, -60];
|
||||
numbers.sort(int_arr);
|
||||
document.write(numbers);
|
||||
35
Task/Sort-an-integer-array/Julia/sort-an-integer-array.julia
Normal file
35
Task/Sort-an-integer-array/Julia/sort-an-integer-array.julia
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
julia> a = [4,2,3,1]
|
||||
4-element Int32 Array:
|
||||
4
|
||||
2
|
||||
3
|
||||
1
|
||||
julia>#non-mutating sort
|
||||
sort(a)
|
||||
4-element Int32 Array:
|
||||
1
|
||||
2
|
||||
3
|
||||
4
|
||||
|
||||
julia> a
|
||||
4-element Int32 Array:
|
||||
4
|
||||
2
|
||||
3
|
||||
1
|
||||
|
||||
julia>#mutating sort
|
||||
sort!(a)
|
||||
4-element Int32 Array:
|
||||
1
|
||||
2
|
||||
3
|
||||
4
|
||||
|
||||
julia> a
|
||||
4-element Int32 Array:
|
||||
1
|
||||
2
|
||||
3
|
||||
4
|
||||
6
Task/Sort-an-integer-array/K/sort-an-integer-array.k
Normal file
6
Task/Sort-an-integer-array/K/sort-an-integer-array.k
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
num: -10?10 / Integers from 0 to 9 in random order
|
||||
5 9 4 2 0 3 6 1 8 7
|
||||
|
||||
srt: {x@<x} / Generalized sort ascending
|
||||
srt num
|
||||
0 1 2 3 4 5 6 7 8 9
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
N =20
|
||||
dim IntArray( N)
|
||||
|
||||
print "Original order"
|
||||
for i =1 to N
|
||||
t =int( 1000 *rnd( 1))
|
||||
IntArray( i) =t
|
||||
print t
|
||||
next i
|
||||
|
||||
sort IntArray(), 1, N
|
||||
|
||||
print "Sorted oprder"
|
||||
for i =1 to N
|
||||
print IntArray( i)
|
||||
next i
|
||||
3
Task/Sort-an-integer-array/Lua/sort-an-integer-array.lua
Normal file
3
Task/Sort-an-integer-array/Lua/sort-an-integer-array.lua
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
t = {4, 5, 2}
|
||||
table.sort(t)
|
||||
print(unpack(t))
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
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)
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
arr = #(5, 4, 3, 2, 1)
|
||||
arr = sort arr
|
||||
12
Task/Sort-an-integer-array/MUMPS/sort-an-integer-array.mumps
Normal file
12
Task/Sort-an-integer-array/MUMPS/sort-an-integer-array.mumps
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
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
|
||||
|
|
@ -0,0 +1 @@
|
|||
numbers = Sort[{2,4,3,1,2}]
|
||||
|
|
@ -0,0 +1 @@
|
|||
sort([9, 4, 3, 7, 6, 1, 10, 2, 8, 5]);
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
:- module sort_int_list.
|
||||
:- interface.
|
||||
:- import_module io.
|
||||
|
||||
:- pred main(io::di, uo::uo) is det.
|
||||
|
||||
:- implementation.
|
||||
:- import_module list.
|
||||
|
||||
main(!IO) :-
|
||||
Nums = [2, 4, 0, 3, 1, 2],
|
||||
list.sort(Nums, Sorted),
|
||||
io.write(Sorted, !IO),
|
||||
io.nl(!IO).
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
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.
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
/* NetRexx */
|
||||
options replace format comments java crossref savelog symbols binary
|
||||
|
||||
ia = int[]
|
||||
ia = [ 2, 4, 3, 1, 2, -1, 0, -2 ]
|
||||
|
||||
display(ia)
|
||||
Arrays.sort(ia)
|
||||
display(ia)
|
||||
|
||||
-- Display results
|
||||
method display(in = int[]) public static
|
||||
|
||||
sorted = Rexx('')
|
||||
|
||||
loop ix = 0 for in.length
|
||||
sorted = sorted || Rexx(in[ix]).right(4)
|
||||
end ix
|
||||
|
||||
say sorted.strip('t')
|
||||
|
||||
return
|
||||
|
|
@ -0,0 +1,72 @@
|
|||
/* NetRexx */
|
||||
options replace format comments java crossref savelog symbols
|
||||
|
||||
/*REXX program to sort an integer array.*/
|
||||
|
||||
numeric digits 20 /*handle larger numbers.*/
|
||||
a = ''
|
||||
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.*/
|
||||
tell('un-sorted', a, size)
|
||||
a[0] = size
|
||||
esort(a, 1)
|
||||
tell(' sorted', a, size)
|
||||
|
||||
return
|
||||
|
||||
/*----------------------------------ESORT subroutine--------------------*/
|
||||
method esort(a, size) public static
|
||||
--esort: procedure expose a.;
|
||||
|
||||
h = a[0]
|
||||
|
||||
loop while h > 1
|
||||
h = h % 2
|
||||
loop i = 1 for a[0] - h
|
||||
j = i
|
||||
k = h + i
|
||||
loop 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 i
|
||||
end
|
||||
|
||||
return
|
||||
|
||||
/*----------------------------------TELL subroutine---------------------*/
|
||||
method tell(arg, a, size) public static
|
||||
--tell:
|
||||
|
||||
say arg.center(40, '-')
|
||||
loop j = 1 for size
|
||||
say arg 'array element' j.right(size.length)'='a[j].right(25)
|
||||
end j
|
||||
say
|
||||
|
||||
return
|
||||
|
|
@ -0,0 +1,54 @@
|
|||
/* NetRexx */
|
||||
options replace format comments java crossref savelog symbols
|
||||
|
||||
/*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 = list.words
|
||||
|
||||
a = 0
|
||||
loop j = 1 for size
|
||||
a[j] = list.word(j)
|
||||
end j
|
||||
|
||||
say ' as is='list
|
||||
a[0] = size
|
||||
esort(a, size)
|
||||
bList = ''
|
||||
|
||||
loop j = 1 for size
|
||||
bList = bList a[j]
|
||||
end j
|
||||
|
||||
blist = bList.strip
|
||||
say ' sorted='bList
|
||||
|
||||
return
|
||||
|
||||
/*----------------------------------ESORT subroutine--------------------*/
|
||||
method esort(a, size) public static
|
||||
--esort: procedure expose a.;
|
||||
|
||||
h = a[0]
|
||||
|
||||
loop while h > 1
|
||||
h = h % 2
|
||||
loop i = 1 for a[0] - h
|
||||
j = i
|
||||
k = h + i
|
||||
loop 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 i
|
||||
end
|
||||
|
||||
return
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
sort >= 9 6 8 7 1 10
|
||||
= 10 9 8 7 6 1
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
2 6 1 0 3 8 sort .s
|
||||
0 1 2 3 6 8
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
let nums = [|2; 4; 3; 1; 2|]
|
||||
Array.sort compare nums
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
let nums = [2; 4; 3; 1; 2]
|
||||
let sorted = List.sort compare nums
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
bundle Default {
|
||||
class Sort {
|
||||
function : Main(args : System.String[]) ~ Nil {
|
||||
nums := Structure.IntVector->New([2,4,3,1,2]);
|
||||
nums->Sort();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
- (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:)];
|
||||
}
|
||||
|
|
@ -0,0 +1 @@
|
|||
sortedv = sort(v);
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
#include <order/interpreter.h>
|
||||
|
||||
ORDER_PP( 8seq_sort(8less, 8seq(2, 4, 3, 1, 2)) )
|
||||
5
Task/Sort-an-integer-array/Oz/sort-an-integer-array.oz
Normal file
5
Task/Sort-an-integer-array/Oz/sort-an-integer-array.oz
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
declare
|
||||
Nums = [2 4 3 1 2]
|
||||
Sorted = {List.sort Nums Value.'<'}
|
||||
in
|
||||
{Show Sorted}
|
||||
|
|
@ -0,0 +1 @@
|
|||
vecsort(v)
|
||||
4
Task/Sort-an-integer-array/PHP/sort-an-integer-array.php
Normal file
4
Task/Sort-an-integer-array/PHP/sort-an-integer-array.php
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
<?php
|
||||
$nums = array(2,4,3,1,2);
|
||||
sort($nums);
|
||||
?>
|
||||
37
Task/Sort-an-integer-array/PL-I/sort-an-integer-array.pli
Normal file
37
Task/Sort-an-integer-array/PL-I/sort-an-integer-array.pli
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
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;
|
||||
|
|
@ -0,0 +1 @@
|
|||
my @sorted = sort @a;
|
||||
|
|
@ -0,0 +1 @@
|
|||
my @sorted = sort +*, @a;
|
||||
|
|
@ -0,0 +1 @@
|
|||
@a .= sort;
|
||||
2
Task/Sort-an-integer-array/Perl/sort-an-integer-array.pl
Normal file
2
Task/Sort-an-integer-array/Perl/sort-an-integer-array.pl
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
@nums = (2,4,3,1,2);
|
||||
@sorted = sort {$a <=> $b} @nums;
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
(sort (2 4 3 1 2))
|
||||
-> (1 2 2 3 4)
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
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;
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
lvars ar = {2 4 3 1 2};
|
||||
consvector(destlist(sort(conslist(destvector(ar))))) -> ar;
|
||||
;;; print the sorted vector:
|
||||
ar =>
|
||||
** {1 2 2 3 4}
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
lvars ar = {2 4 3 1 2};
|
||||
consvector(destlist(sort(datalist(ar)))) -> ar;
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
lvars ar = {2 4 3 1 2};
|
||||
ar.datalist.sort.destlist.consvector -> ar;
|
||||
|
|
@ -0,0 +1 @@
|
|||
ARRAY SORT x()
|
||||
|
|
@ -0,0 +1 @@
|
|||
34,12,23,56,1,129,4,2,73 | Sort-Object
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
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^</@>
|
||||
Numbers sort as strings
|
||||
<@ ACTSRTENTLST>L</@>
|
||||
<@ SAYDMPLST>L</@>
|
||||
<@ ACTSRTENTLSTLIT>L|__StringDescending</@>
|
||||
<@ SAYDMPLST>L</@>
|
||||
|
||||
Construct another list of numbers
|
||||
<@ LETCNSLSTLIT>list|65^84^1^25^77^4^47^2^42^44^41^25^69^3^51^45^4^39^</@>
|
||||
Numbers sorted as numbers
|
||||
<@ ACTSRTENTLSTLIT>list|__Numeric</@>
|
||||
<@ SAYDMPLST>list</@>
|
||||
<@ ACTSRTENTLSTLIT>list|__NumericDescending</@>
|
||||
<@ SAYDMPLST>list</@>
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
Construct a list of numbers
|
||||
|
||||
Numbers sort as strings
|
||||
|
||||
1^2^25^25^3^39^4^4^41^42^44^45^47^51^65^69^77^84^
|
||||
|
||||
84^77^69^65^51^47^45^44^42^41^4^4^39^3^25^25^2^1^
|
||||
|
||||
Construct another list of numbers
|
||||
|
||||
Numbers sorted as numbers
|
||||
|
||||
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^
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
Dim numbers(20)
|
||||
For i = 0 To 20
|
||||
numbers(i) = Random(1000)
|
||||
Next
|
||||
|
||||
SortArray(numbers(), #PB_Sort_Ascending)
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
nums = [2,4,3,1,2]
|
||||
nums.sort()
|
||||
|
|
@ -0,0 +1 @@
|
|||
nums = sorted([2,4,3,1,2])
|
||||
2
Task/Sort-an-integer-array/R/sort-an-integer-array.r
Normal file
2
Task/Sort-an-integer-array/R/sort-an-integer-array.r
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
nums <- c(2,4,3,1,2)
|
||||
sorted <- sort(nums)
|
||||
|
|
@ -0,0 +1 @@
|
|||
sort [2 4 3 1 2]
|
||||
47
Task/Sort-an-integer-array/REXX/sort-an-integer-array-1.rexx
Normal file
47
Task/Sort-an-integer-array/REXX/sort-an-integer-array-1.rexx
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
/*REXX program to sort (E-sort) an array (which contains integers). */
|
||||
numeric digits 30 /*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 /*have a list of 21 Euler numbers*/
|
||||
call tell 'un-sorted'
|
||||
call esort size
|
||||
call tell ' sorted'
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
/*──────────────────────────────────ESORT subroutine────────────────────*/
|
||||
esort: procedure expose a.; parse arg N; h=N
|
||||
do while h>1; h=h%2
|
||||
do i=1 for N-h; j=i; k=h+i
|
||||
do while a.k<a.j
|
||||
parse value a.j a.k with a.k a.j /*swap two elements.*/
|
||||
if h>=j then leave; j=j-h; k=k-h
|
||||
end /*while a.k<a.j*/
|
||||
end /*i*/
|
||||
end /*while h>1*/
|
||||
return
|
||||
/*──────────────────────────────────TELL subroutine─────────────────────*/
|
||||
tell: say center(arg(1),50,'─')
|
||||
do j=1 for size
|
||||
say arg(1) 'array element' right(j,length(size))'='right(a.j,20)
|
||||
end /*j*/
|
||||
say
|
||||
return
|
||||
30
Task/Sort-an-integer-array/REXX/sort-an-integer-array-2.rexx
Normal file
30
Task/Sort-an-integer-array/REXX/sort-an-integer-array-2.rexx
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
/*REXX program sorts (using E-sort) a list of some interesting integers.*/
|
||||
|
||||
/*quotes aren't needed if all elements in a list are non-negative.*/
|
||||
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 /*throw 'em───>a pot*/
|
||||
say 'original =' list /*an annoucement... */
|
||||
size=words(list) /*nice to have SIZE.*/
|
||||
do j=1 for size /*build an array, 1 */
|
||||
a.j=word(list,j) /*element at a time.*/
|
||||
end /*j*/
|
||||
call esort size /*sort the stuff. */
|
||||
bList=a.1 /*start with 1st. */
|
||||
do k=2 to size /*build a list. */
|
||||
bList=bList a.k
|
||||
end /*k*/
|
||||
say ' sorted =' bList /*show & tell time. */
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
/*──────────────────────────────────ESORT subroutine────────────────────*/
|
||||
esort: procedure expose a.; parse arg N; h=N
|
||||
do while h>1; h=h%2
|
||||
do i=1 for N-h; j=i; k=h+i
|
||||
do while a.k<a.j
|
||||
parse value a.j a.k with a.k a.j /*swap two elements.*/
|
||||
if h>=j then leave; j=j-h; k=k-h
|
||||
end /*while a.k<a.j*/
|
||||
end /*i*/
|
||||
end /*while h>1*/
|
||||
return
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
rascal>import List;
|
||||
ok
|
||||
|
||||
rascal>a = [1, 4, 2, 3, 5];
|
||||
list[int]: [1,4,2,3,5]
|
||||
|
||||
rascal>sort(a)
|
||||
list[int]: [1,2,3,4,5]
|
||||
|
||||
rascal>sort(a, bool(int a, int b){return a >= b;})
|
||||
list[int]: [5,4,3,2,1]
|
||||
|
|
@ -0,0 +1 @@
|
|||
[ 2 4 3 1 2 ] sort
|
||||
3
Task/Sort-an-integer-array/Ruby/sort-an-integer-array.rb
Normal file
3
Task/Sort-an-integer-array/Ruby/sort-an-integer-array.rb
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
nums = [2,4,3,1,2]
|
||||
sorted = nums.sort # returns a new sorted array. 'nums' is unchanged
|
||||
nums.sort! # sort 'nums' "in-place"
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
println(List(5,2,78,2,578,-42).sortWith(_<_))
|
||||
//--> List(-42, 2, 2, 5, 78, 578)
|
||||
|
|
@ -0,0 +1 @@
|
|||
(sort #(9 -2 1 2 8 0 1 2) #'<)
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
var array integer: nums is [] (2, 4, 3, 1, 2);
|
||||
|
||||
nums := sort(nums);
|
||||
|
|
@ -0,0 +1 @@
|
|||
#(7 5 2 9 0 -1) sort
|
||||
|
|
@ -0,0 +1 @@
|
|||
#(7 5 2 9 0 -1) asSortedCollection
|
||||
|
|
@ -0,0 +1 @@
|
|||
#(7 5 2 9 0 -1) sort
|
||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Add a link
Reference in a new issue