Initial data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 72d218235f
commit f23f22d71c
199087 changed files with 3378941 additions and 0 deletions

View file

@ -0,0 +1,3 @@
---
from: http://rosettacode.org/wiki/Sort_an_integer_array
note: Sorting

View file

@ -0,0 +1,9 @@
{{Sorting Algorithm}}
;Task:
Sort an array (or list) of integers in ascending numerical order.
Use a sorting facility provided by the language/library if possible.
<br><br>

View file

@ -0,0 +1,2 @@
nums = [2,4,3,1,2]
nums.sort()

View file

@ -0,0 +1 @@
nums = sorted([2,4,3,1,2])

View 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

View 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

View file

@ -0,0 +1 @@
[ 10,2,100 ] ' n:cmp a:sort . cr

View file

@ -0,0 +1,164 @@
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program integerSort64.s with selection sort */
/*******************************************/
/* Constantes file */
/*******************************************/
/* for this file see task include a file in language AArch64 assembly */
.include "../includeConstantesARM64.inc"
/*********************************/
/* Initialized data */
/*********************************/
.data
szMessSortOk: .asciz "Table sorted.\n"
szMessSortNok: .asciz "Table not sorted !!!!!.\n"
sMessResult: .asciz "Value : @ \n"
szCarriageReturn: .asciz "\n"
.align 4
#TableNumber: .quad 1,3,6,2,5,9,10,8,4,7
TableNumber: .quad 10,9,8,7,6,5,4,3,2,1
.equ NBELEMENTS, (. - TableNumber) / 8
/*********************************/
/* UnInitialized data */
/*********************************/
.bss
sZoneConv: .skip 24
/*********************************/
/* code section */
/*********************************/
.text
.global main
main: // entry of program
ldr x0,qAdrTableNumber // address number table
mov x1,0
mov x2,NBELEMENTS // number of élements
bl selectionSort
ldr x0,qAdrTableNumber // address number table
bl displayTable
ldr x0,qAdrTableNumber // address number table
mov x1,NBELEMENTS // number of élements
bl isSorted // control sort
cmp x0,1 // sorted ?
beq 1f
ldr x0,qAdrszMessSortNok // no !! error sort
bl affichageMess
b 100f
1: // yes
ldr x0,qAdrszMessSortOk
bl affichageMess
100: // standard end of the program
mov x0,0 // return code
mov x8,EXIT // request to exit program
svc 0 // perform the system call
qAdrsZoneConv: .quad sZoneConv
qAdrszCarriageReturn: .quad szCarriageReturn
qAdrsMessResult: .quad sMessResult
qAdrTableNumber: .quad TableNumber
qAdrszMessSortOk: .quad szMessSortOk
qAdrszMessSortNok: .quad szMessSortNok
/******************************************************************/
/* control sorted table */
/******************************************************************/
/* x0 contains the address of table */
/* x1 contains the number of elements > 0 */
/* x0 return 0 if not sorted 1 if sorted */
isSorted:
stp x2,lr,[sp,-16]! // save registers
stp x3,x4,[sp,-16]! // save registers
mov x2,0
ldr x4,[x0,x2,lsl 3]
1:
add x2,x2,1
cmp x2,x1
bge 99f
ldr x3,[x0,x2, lsl 3]
cmp x3,x4
blt 98f
mov x4,x3
b 1b
98:
mov x0,0 // not sorted
b 100f
99:
mov x0,1 // sorted
100:
ldp x3,x4,[sp],16 // restaur 2 registers
ldp x2,lr,[sp],16 // restaur 2 registers
ret // return to address lr x30
/******************************************************************/
/* selection sort */
/******************************************************************/
/* x0 contains the address of table */
/* x1 contains the first element */
/* x2 contains the number of element */
selectionSort:
stp x1,lr,[sp,-16]! // save registers
stp x2,x3,[sp,-16]! // save registers
stp x4,x5,[sp,-16]! // save registers
stp x6,x7,[sp,-16]! // save registers
mov x3,x1 // start index i
sub x7,x2,1 // compute n - 1
1: // start loop
mov x4,x3
add x5,x3,1 // init index 2
2:
ldr x1,[x0,x4,lsl 3] // load value A[mini]
ldr x6,[x0,x5,lsl 3] // load value A[j]
cmp x6,x1 // compare value
csel x4,x5,x4,lt // j -> mini
add x5,x5,1 // increment index j
cmp x5,x2 // end ?
blt 2b // no -> loop
cmp x4,x3 // mini <> j ?
beq 3f // no
ldr x1,[x0,x4,lsl 3] // yes swap A[i] A[mini]
ldr x6,[x0,x3,lsl 3]
str x1,[x0,x3,lsl 3]
str x6,[x0,x4,lsl 3]
3:
add x3,x3,1 // increment i
cmp x3,x7 // end ?
blt 1b // no -> loop
100:
ldp x6,x7,[sp],16 // restaur 2 registers
ldp x4,x5,[sp],16 // restaur 2 registers
ldp x2,x3,[sp],16 // restaur 2 registers
ldp x1,lr,[sp],16 // restaur 2 registers
ret // return to address lr x30
/******************************************************************/
/* Display table elements */
/******************************************************************/
/* x0 contains the address of table */
displayTable:
stp x1,lr,[sp,-16]! // save registers
stp x2,x3,[sp,-16]! // save registers
mov x2,x0 // table address
mov x3,0
1: // loop display table
ldr x0,[x2,x3,lsl 3]
ldr x1,qAdrsZoneConv
bl conversion10 // décimal conversion
ldr x0,qAdrsMessResult
ldr x1,qAdrsZoneConv
bl strInsertAtCharInc // insert result at @ character
bl affichageMess // display message
add x3,x3,1
cmp x3,NBELEMENTS - 1
ble 1b
ldr x0,qAdrszCarriageReturn
bl affichageMess
100:
ldp x2,x3,[sp],16 // restaur 2 registers
ldp x1,lr,[sp],16 // restaur 2 registers
ret // return to address lr x30
/********************************************************/
/* File Include fonctions */
/********************************************************/
/* for this file see task include a file in language AArch64 assembly */
.include "../includeARM64.inc"

View file

@ -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))

View file

@ -0,0 +1,16 @@
begin
% use the quicksort procedure from the Sorting_Algorithms/Quicksort task %
% Quicksorts in-place the array of integers v, from lb to ub - external %
procedure quicksort ( integer array v( * )
; integer value lb, ub
) ; algol "sortingAlgorithms_Quicksort" ;
% sort an integer array with the quicksort routine %
begin
integer array t ( 1 :: 5 );
integer p;
p := 1;
for v := 2, 3, 1, 9, -2 do begin t( p ) := v; p := p + 1; end;
quicksort( t, 1, 5 );
for i := 1 until 5 do writeon( i_w := 1, s_w := 1, t( i ) )
end
end.

View file

@ -0,0 +1,3 @@
X63 92 51 92 39 15 43 89 36 69
X[X]
15 36 39 43 51 63 69 89 92 92

View file

@ -0,0 +1,156 @@
/* ARM assembly Raspberry PI */
/* program integerSort.s with selection sort */
/* REMARK 1 : this program use routines in a include file
see task Include a file language arm assembly
for the routine affichageMess conversion10
see at end of this program the instruction include */
/* for constantes see task include a file in arm assembly */
/************************************/
/* Constantes */
/************************************/
.include "../constantes.inc"
/*********************************/
/* Initialized data */
/*********************************/
.data
szMessSortOk: .asciz "Table sorted.\n"
szMessSortNok: .asciz "Table not sorted !!!!!.\n"
sMessResult: .asciz "Value : @ \n"
szCarriageReturn: .asciz "\n"
.align 4
TableNumber: .int 1,3,6,2,5,9,10,8,4,7
#TableNumber: .int 10,9,8,7,6,5,4,3,2,1
.equ NBELEMENTS, (. - TableNumber) / 4
/*********************************/
/* UnInitialized data */
/*********************************/
.bss
sZoneConv: .skip 24
/*********************************/
/* code section */
/*********************************/
.text
.global main
main: @ entry of program
1:
ldr r0,iAdrTableNumber @ address number table
mov r1,#0
mov r2,#NBELEMENTS @ number of élements
bl selectionSort
ldr r0,iAdrTableNumber @ address number table
bl displayTable
ldr r0,iAdrTableNumber @ address number table
mov r1,#NBELEMENTS @ number of élements
bl isSorted @ control sort
cmp r0,#1 @ sorted ?
beq 2f
ldr r0,iAdrszMessSortNok @ no !! error sort
bl affichageMess
b 100f
2: @ yes
ldr r0,iAdrszMessSortOk
bl affichageMess
100: @ standard end of the program
mov r0, #0 @ return code
mov r7, #EXIT @ request to exit program
svc #0 @ perform the system call
iAdrszCarriageReturn: .int szCarriageReturn
iAdrsMessResult: .int sMessResult
iAdrTableNumber: .int TableNumber
iAdrszMessSortOk: .int szMessSortOk
iAdrszMessSortNok: .int szMessSortNok
/******************************************************************/
/* control sorted table */
/******************************************************************/
/* r0 contains the address of table */
/* r1 contains the number of elements > 0 */
/* r0 return 0 if not sorted 1 if sorted */
isSorted:
push {r2-r4,lr} @ save registers
mov r2,#0
ldr r4,[r0,r2,lsl #2]
1:
add r2,#1
cmp r2,r1
movge r0,#1
bge 100f
ldr r3,[r0,r2, lsl #2]
cmp r3,r4
movlt r0,#0
blt 100f
mov r4,r3
b 1b
100:
pop {r2-r4,lr}
bx lr @ return
/******************************************************************/
/* selection sort */
/******************************************************************/
/* r0 contains the address of table */
/* r1 contains the first element */
/* r2 contains the number of element */
selectionSort:
push {r1-r7,lr} @ save registers
mov r3,r1 @ start index i
sub r7,r2,#1 @ compute n - 1
1: @ start loop
mov r4,r3
add r5,r3,#1 @ init index 2
2:
ldr r1,[r0,r4,lsl #2] @ load value A[mini]
ldr r6,[r0,r5,lsl #2] @ load value A[j]
cmp r6,r1 @ compare value
movlt r4,r5 @ j -> mini
add r5,#1 @ increment index j
cmp r5,r2 @ end ?
blt 2b @ no -> loop
cmp r4,r3 @ mini <> j ?
beq 3f @ no
ldr r1,[r0,r4,lsl #2] @ yes swap A[i] A[mini]
ldr r6,[r0,r3,lsl #2]
str r1,[r0,r3,lsl #2]
str r6,[r0,r4,lsl #2]
3:
add r3,#1 @ increment i
cmp r3,r7 @ end ?
blt 1b @ no -> loop
100:
pop {r1-r7,lr}
bx lr @ return
/******************************************************************/
/* Display table elements */
/******************************************************************/
/* r0 contains the address of table */
displayTable:
push {r0-r3,lr} @ save registers
mov r2,r0 @ table address
mov r3,#0
1: @ loop display table
ldr r0,[r2,r3,lsl #2]
ldr r1,iAdrsZoneConv @
bl conversion10 @ décimal conversion
ldr r0,iAdrsMessResult
ldr r1,iAdrsZoneConv @ insert conversion
bl strInsertAtCharInc
bl affichageMess @ display message
add r3,#1
cmp r3,#NBELEMENTS - 1
ble 1b
ldr r0,iAdrszCarriageReturn
bl affichageMess
100:
pop {r0-r3,lr}
bx lr
iAdrsZoneConv: .int sZoneConv
/***************************************************/
/* ROUTINES INCLUDE */
/***************************************************/
.include "../affichage.inc"

View file

@ -0,0 +1,15 @@
# syntax: GAWK -f SORT_AN_INTEGER_ARRAY.AWK
BEGIN {
split("9,10,3,1234,99,1,200,2,0,-2",arr,",")
show("@unsorted","unsorted")
show("@val_num_asc","sorted ascending")
show("@val_num_desc","sorted descending")
exit(0)
}
function show(sequence,description, i) {
PROCINFO["sorted_in"] = sequence
for (i in arr) {
printf("%s ",arr[i])
}
printf("\t%s\n",description)
}

View file

@ -0,0 +1,40 @@
INCLUDE "D2:SORT.ACT" ;from the Action! Tool Kit
PROC PrintArray(INT ARRAY a INT size)
INT i
Put('[)
FOR i=0 TO size-1
DO
IF i>0 THEN Put(' ) FI
PrintI(a(i))
OD
Put(']) PutE()
RETURN
PROC Test(INT ARRAY a INT size BYTE order)
PrintE("Array before sort:")
PrintArray(a,size)
SortI(a,size,order)
PrintE("Array after sort:")
PrintArray(a,size)
PutE()
RETURN
PROC Main()
DEFINE ASCENDING="0"
INT ARRAY
a(10)=[1 4 65535 0 3 7 4 8 20 65530],
b(21)=[10 9 8 7 6 5 4 3 2 1 0
65535 65534 65533 65532 65531
65530 65529 65528 65527 65526],
c(8)=[101 102 103 104 105 106 107 108],
d(12)=[1 65535 1 65535 1 65535 1
65535 1 65535 1 65535]
Put(125) PutE() ;clear screen
Test(a,10,ASCENDING)
Test(b,21,ASCENDING)
Test(c,8,ASCENDING)
Test(d,12,ASCENDING)
RETURN

View file

@ -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);

View 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;

View file

@ -0,0 +1,43 @@
use framework "Foundation"
-- sort :: [a] -> [a]
on sort(lst)
((current application's NSArray's arrayWithArray:lst)'s ¬
sortedArrayUsingSelector:"compare:") as list
end sort
-- TEST -----------------------------------------------------------------------
on run
map(sort, [[9, 1, 8, 2, 8, 3, 7, 0, 4, 6, 5], ¬
["alpha", "beta", "gamma", "delta", "epsilon", "zeta", "eta", ¬
"theta", "iota", "kappa", "lambda", "mu"]])
end run
-- GENERIC FUNCTIONS ---------------------------------------------------------
-- map :: (a -> b) -> [a] -> [b]
on map(f, xs)
tell mReturn(f)
set lng to length of xs
set lst to {}
repeat with i from 1 to lng
set end of lst to |λ|(item i of xs, i, xs)
end repeat
return lst
end tell
end map
-- Lift 2nd class handler function into 1st class script wrapper
-- mReturn :: Handler -> Script
on mReturn(f)
if class of f is script then
f
else
script
property |λ| : f
end script
end if
end mReturn

View file

@ -0,0 +1,4 @@
arr: [2 3 5 8 4 1 6 9 7]
sort 'arr ; in-place
loop arr => print

View file

@ -0,0 +1,3 @@
numbers = 5 4 1 2 3
sort, numbers, N D%A_Space%
Msgbox % numbers

View file

@ -0,0 +1,7 @@
2→{L₁}
4→{L₁+1}
3→{L₁+2}
1→{L₁+3}
2→{L₁+4}
SortD(L₁,5)

View file

@ -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%)

View file

@ -0,0 +1,14 @@
' Sort an integer array
DECLARE values[5] TYPE NUMBER
values[0] = 23
values[1] = 32
values[2] = 12
values[3] = 21
values[4] = 01
SORT values
FOR i = 0 TO 3
PRINT values[i], ", ";
NEXT
PRINT values[4]

View file

@ -0,0 +1,6 @@
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 )
babel> ls2lf --> Convert list to array for sorting
babel> dup {fnord} merge_sort --> The internal sort operator
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 )

View file

@ -0,0 +1,3 @@
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 !
( 9 20 21 28 34 46 49 53 54 63 67 68 73 75 83 83 86 88 89 90 )

View file

@ -0,0 +1,2 @@
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 )

View file

@ -0,0 +1,6 @@
beads 1 program 'Sort an integer array'
calc main_init
var arr = [4, 1, 2, -1, 3, 0, 2]
var newarr : array of num
loop across:arr sort:val count:c val:v
newarr[c] = v

View file

@ -0,0 +1,4 @@
v
> 543** > :#v_ $&> :#v_ 1 > :0g > :#v_ $ 1+: 543** `! #v_ 25*,@
^-1p0\0:< ^-1 p0\+1 g0:&< ^-1\.:\<
^ <

View file

@ -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);

View file

@ -0,0 +1 @@
{1 3 2 5 4}><

View file

@ -0,0 +1,8 @@
#include <algorithm>
int main()
{
int nums[] = {2,4,3,1,2};
std::sort(nums, nums+sizeof(nums)/sizeof(int));
return 0;
}

View 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;
}

View 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;
}

View file

@ -0,0 +1,9 @@
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);
}
}

View 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;
}

View file

@ -0,0 +1,22 @@
PROGRAM-ID. sort-ints.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 array-area VALUE "54321".
03 array PIC 9 OCCURS 5 TIMES.
01 i PIC 9.
PROCEDURE DIVISION.
main-line.
PERFORM display-array
SORT array ASCENDING array
PERFORM display-array
GOBACK
.
display-array.
PERFORM VARYING i FROM 1 BY 1 UNTIL 5 < i
DISPLAY array (i) " " NO ADVANCING
END-PERFORM
DISPLAY SPACE
.

View file

@ -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}

View file

@ -0,0 +1,2 @@
(sort [5 4 3 2 1]) ; sort can also take a comparator function
(1 2 3 4 5)

View file

@ -0,0 +1,2 @@
CL-USER> (sort #(9 -2 1 2 8 0 1 2) #'<)
#(-2 0 1 1 2 2 8 9)

View file

@ -0,0 +1,10 @@
a = [5, 4, 3, 2, 1]
puts a.sort
# => [1, 2, 3, 4, 5]
puts a
# => [5, 4, 3, 2, 1]
a.sort!
puts a
# => [1, 2, 3, 4, 5]

View 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]);
}

View file

@ -0,0 +1,3 @@
var a : array of Integer := [5, 4, 3, 2, 1];
a.Sort; // ascending natural sort
PrintLn(a.Map(IntToStr).Join(',')); // 1,2,3,4,5

View file

@ -0,0 +1,8 @@
uses Types, Generics.Collections;
var
a: TIntegerDynArray;
begin
a := TIntegerDynArray.Create(5, 4, 3, 2, 1);
TArray.Sort<Integer>(a);
end;

View file

@ -0,0 +1 @@
[2,4,3,1,2].sort()

View 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

View file

@ -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

View file

@ -0,0 +1,5 @@
local
l_array: SORTED_TWO_WAY_LIST [INTEGER]
do
create l_array.make_from_iterable (<<9,8,7,6,5,4,3,2,1,0>>)
end

View file

@ -0,0 +1,9 @@
import system'routines;
import extensions;
public program()
{
var unsorted := new int[]{6, 2, 7, 8, 3, 1, 10, 5, 4, 9};
console.printLine(unsorted.clone().sort(ifOrdered).asEnumerable())
}

View file

@ -0,0 +1,3 @@
list = [2, 4, 3, 1, 2]
IO.inspect Enum.sort(list)
IO.inspect Enum.sort(list, &(&1>&2))

View file

@ -0,0 +1,2 @@
List = [2, 4, 3, 1, 2].
SortedList = lists:sort(List).

View file

@ -0,0 +1,2 @@
include sort.e
print(1,sort({20, 7, 65, 10, 3, 0, 8, -60}))

View file

@ -0,0 +1,7 @@
// 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

View file

@ -0,0 +1 @@
{ 1 4 9 2 3 0 5 } natural-sort .

View file

@ -0,0 +1,2 @@
create test-data 2 , 4 , 3 , 1 , 2 ,
test-data 5 cell-sort

View file

@ -0,0 +1,44 @@
100000 CONSTANT SIZE
CREATE MYARRAY SIZE CELLS ALLOT
: [] ( n addr -- addr[n]) SWAP CELLS + ;
: FILLIT ( -- ) ( reversed order)
SIZE 0 DO SIZE I - I MYARRAY [] ! LOOP ;
: SEEIT ( -- )
SIZE 0 DO I MYARRAY [] ? LOOP ;
\ define non-standard words used by Quicksort author
1 CELLS CONSTANT CELL
CELL NEGATE CONSTANT -CELL
: CELL- CELL - ;
: MID ( l r -- mid ) OVER - 2/ -CELL AND + ;
: EXCH ( addr1 addr2 -- )
OVER @ OVER @ ( read values)
SWAP ROT ! SWAP ! ; ( exchange values)
: PARTITION ( l r -- l r r2 l2 )
2DUP MID @ >R ( r: pivot )
2DUP
BEGIN
SWAP BEGIN DUP @ R@ < WHILE CELL+ REPEAT
SWAP BEGIN R@ OVER @ < WHILE CELL- REPEAT
2DUP <= IF 2DUP EXCH >R CELL+ R> CELL- THEN
2DUP >
UNTIL
R> DROP ;
: QSORT ( l r -- )
PARTITION SWAP ROT
2DUP < IF RECURSE ELSE 2DROP THEN
2DUP < IF RECURSE ELSE 2DROP THEN ;
: QUICKSORT ( array len -- )
DUP 2 < IF 2DROP EXIT THEN 1- CELLS OVER + QSORT ;</LANG>
Test at the console
<syntaxhighlight lang="forth">FILLIT ok
MYARRAY SIZE QUICKSORT ok

View file

@ -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.

View file

@ -0,0 +1,58 @@
' version 11-03-2016
' compile with: fbc -s console
#Include Once "crt/stdlib.bi" ' needed for qsort subroutine
' Declare Sub qsort (ByVal As Any Ptr, <== point to start of array
' ByVal As size_t, <== size of array
' ByVal As size_t, <== size of array element
' ByVal As Function(ByVal As Any Ptr, ByVal As Any Ptr) As Long) <== callback function
' declare callback function with Cdecl to ensures that the parameters are passed in the correct order
'
' size of long: 4 bytes on 32bit OS, 8 bytes on 64bit OS
' ascending
Function callback Cdecl (ByVal element1 As Any Ptr, ByVal element2 As Any Ptr) As Long
Function = *Cast(Long Ptr, element1) - *Cast(Long Ptr, element2)
End Function
' Function callback Cdecl (ByVal element1 As Any Ptr, ByVal element2 As Any Ptr) As Long
' Dim As Long e1 = *Cast(Long Ptr, element1)
' Dim As Long e2 = *Cast(Long Ptr, element2)
' Dim As Long result = Sgn(e1 - e2)
' If Sgn(e1) = -1 And Sgn(e2) = -1 Then result = -result
' Function = result
' End Function
' ------=< MAIN >=------
Dim As Long i, array(20)
Dim As Long lb = LBound(array)
Dim As Long ub = UBound(array)
For i = lb To ub ' fill array
array(i) = 10 - i
Next
Print
Print "unsorted array"
For i = lb To ub ' display array
Print Using "###";array(i);
Next
Print : Print
' sort array
qsort(@array(lb), ub - lb +1, SizeOf(array), @callback)
Print "sorted array"
For i = lb To ub ' show sorted array
Print Using "###";array(i);
Next
Print
' empty keyboard buffer
While Inkey <> "" : Wend
Print : Print "hit any key to end program"
Sleep
End

View file

@ -0,0 +1,2 @@
a = [5, 2, 4, 1, 6, 7, 9, 3, 8, 0]
sort[a]

View file

@ -0,0 +1,3 @@
nums = [5, 2, 78, 2, 578, -42]
println( sort(nums) ) // sort in ascending order
println( nums.sortWith((>)) ) // sort in descending order

View file

@ -0,0 +1,11 @@
window 1, @"Sort an integer array"
void local fn DoIt
CFArrayRef array = @[@13,@71,@42,@8,@5,@27]
array = fn ArraySortedArrayUsingSelector( array, @"compare:" )
print fn ArrayComponentsJoinedByString( array, @", " )
end fn
fn DoIt
HandleEvents

View 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 ]

View file

@ -0,0 +1,12 @@
Public Sub Main()
Dim iArray As Integer[] = [8, 2, 5, 9, 1, 3, 6, 7, 4]
Dim iTemp As Integer
Dim sOutput As String
For Each iTemp In iArray.Sort()
sOutput &= iTemp & ", "
Next
Print Left(sOutput, -2)
End

View 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)
}

View file

@ -0,0 +1 @@
[2 4 3 1 2]$

View file

@ -0,0 +1 @@
println ([2,4,0,3,1,2,-12].sort())

View file

@ -0,0 +1,2 @@
nums = [2,4,3,1,2] :: [Int]
sorted = List.sort nums

View file

@ -0,0 +1,4 @@
DIMENSION array(100)
array = INT( RAN(100) )
SORT(Vector=array, Sorted=array)

View file

@ -0,0 +1,4 @@
main() {
nums = [2, 4, 3, 1, 2];
nums.sort();
}

View file

@ -0,0 +1 @@
result = array[sort(array)]

View file

@ -0,0 +1 @@
S := sort(L:= [63, 92, 51, 92, 39, 15, 43, 89, 36, 69]) # will sort a list

View file

@ -0,0 +1,2 @@
let L be {5, 4, 7, 1, 18};
sort L;

View 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"

View file

@ -0,0 +1 @@
/:~

View 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

View file

@ -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);
}
}

View 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);
}
}

View file

@ -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);

View file

@ -0,0 +1,2 @@
from jinja2 import Template
print(Template("{{ [53, 17, 42, 61, 35] | sort }}").render())

View file

@ -0,0 +1,2 @@
from jinja2 import Template
print(Template("{{ [53, 17, 42, 61, 35] | sort(reverse=true) }}").render())

View file

@ -0,0 +1 @@
[2,1,3] | sort # => [1,2,3]

View file

@ -0,0 +1,33 @@
julia> a = [4,2,3,1]
4-element Int32 Array:
4
2
3
1
julia> sort(a) #out-of-place/non-mutating sort
4-element Int32 Array:
1
2
3
4
julia> a
4-element Int32 Array:
4
2
3
1
julia> sort!(a) # in-place/mutating sort
4-element Int32 Array:
1
2
3
4
julia> a
4-element Int32 Array:
1
2
3
4

View 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

View file

@ -0,0 +1,7 @@
// 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 = "]"))
}

View file

@ -0,0 +1,11 @@
1) sorting digits in a number returns a new number of ordered digits
{W.sort < 51324}
-> 12345
2) sorting a sequence of numbers returns a new ordered sequence of these numbers
{S.sort < 51 111 33 2 41}
-> 2 33 41 51 111
3) sorting an array of numbers returns the same array ordered
{A.sort! < {A.new 51 111 33 2 41}}
-> [2,33,41,51,111]

View file

@ -0,0 +1,7 @@
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

View file

@ -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

View file

@ -0,0 +1,4 @@
l = [7, 4, 23]
l.sort()
put l
-- [4, 7, 23]

View file

@ -0,0 +1,4 @@
put "3,2,5,4,1" into X
sort items of X numeric
put X
-- outputs "1,2,3,4,5"

View file

@ -0,0 +1,3 @@
t = {4, 5, 2}
table.sort(t)
print(unpack(t))

View file

@ -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)

View file

@ -0,0 +1,2 @@
arr = #(5, 4, 3, 2, 1)
arr = sort arr

View 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

View file

@ -0,0 +1,2 @@
sort([5,7,8,3,6,1]);
sort(Array([5,7,8,3,6,1]))

View file

@ -0,0 +1 @@
numbers=Sort[{2,4,3,1,2}]

View file

@ -0,0 +1 @@
sort([9, 4, 3, 7, 6, 1, 10, 2, 8, 5]);

View file

@ -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).

View file

@ -0,0 +1 @@
(5 2 1 3 4) '> sort print

View file

@ -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.

View file

@ -0,0 +1,3 @@
% import sort
% println sort({2,4,3,1,2})
[1, 2, 2, 3, 4]

View file

@ -0,0 +1,40 @@
/**
<doc><h2>Sort integer array, in Neko</h2>
<p>Array sort function modified from Haxe codegen with -D neko-source</p>
<p>The Neko target emits support code for Haxe basics, sort is included</p>
<p>Tectonics:<br />prompt$ nekoc sort.neko<br />prompt$ neko sort</p>
</doc>
**/
var sort = function(a) {
var i = 0;
var len = $asize(a);
while ( i < len ) {
var swap = false;
var j = 0;
var max = (len - i) - 1;
while ( j < max ) {
if ( (a[j] - a[j + 1]) > 0 ) {
var tmp = a[j + 1];
a[j + 1] = a[j];
a[j] = tmp;
swap = true;
}
j += 1;
}
if ( $not(swap) )
break;;
i += 1;
}
return a;
}
var arr = $array(5,3,2,1,4)
$print(arr, "\n")
/* Sorts in place */
sort(arr)
$print(arr, "\n")
/* Also returns the sorted array for chaining */
$print(sort($array(3,1,4,1,5,9,2,6,5,3,5,8)), "\n")

View file

@ -0,0 +1,13 @@
using System.Console;
module IntSort
{
Main() : void
{
def nums = [1, 5, 3, 7, 2, 8, 3, 9];
def sorted = nums.Sort((x, y) => x.CompareTo(y));
WriteLine(nums);
WriteLine(sorted);
}
}

Some files were not shown because too many files have changed in this diff Show more