Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
3
Task/Sorting-algorithms-Insertion-sort/00-META.yaml
Normal file
3
Task/Sorting-algorithms-Insertion-sort/00-META.yaml
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
---
|
||||
from: http://rosettacode.org/wiki/Sorting_algorithms/Insertion_sort
|
||||
note: Sorting Algorithms
|
||||
31
Task/Sorting-algorithms-Insertion-sort/00-TASK.txt
Normal file
31
Task/Sorting-algorithms-Insertion-sort/00-TASK.txt
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
{{Sorting Algorithm}}
|
||||
[[Category:Sorting]]
|
||||
{{wikipedia|Insertion sort}}
|
||||
{{omit from|GUISS}}
|
||||
|
||||
<br>
|
||||
An <span style="font-family: serif">[[O]](''n''<sup>2</sup>)</span> sorting algorithm which moves elements one at a time into the correct position.
|
||||
The algorithm consists of inserting one element at a time into the previously sorted part of the array, moving higher ranked elements up as necessary.
|
||||
To start off, the first (or smallest, or any arbitrary) element of the unsorted array is considered to be the sorted part.
|
||||
|
||||
Although insertion sort is an <span style="font-family: serif">[[O]](''n''<sup>2</sup>)</span> algorithm, its simplicity, low overhead, good locality of reference and efficiency make it a good choice in two cases: <br>
|
||||
|
||||
:# small <span style="font-family: serif">''n''</span>, <br>
|
||||
:# as the final finishing-off algorithm for <span style="font-family: serif">[[O]](''n'' log''n'')</span> algorithms such as [[Merge sort|mergesort]] and [[quicksort]].
|
||||
|
||||
|
||||
The algorithm is as follows (from [[wp:Insertion_sort#Algorithm|wikipedia]]):
|
||||
'''function''' ''insertionSort''(array A)
|
||||
'''for''' i '''from''' 1 '''to''' length[A]-1 '''do'''
|
||||
value := A[i]
|
||||
j := i-1
|
||||
'''while''' j >= 0 '''and''' A[j] > value '''do'''
|
||||
A[j+1] := A[j]
|
||||
j := j-1
|
||||
'''done'''
|
||||
A[j+1] = value
|
||||
'''done'''
|
||||
|
||||
Writing the algorithm for integers will suffice.
|
||||
<br><br>
|
||||
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
F insertion_sort(&l)
|
||||
L(i) 1 .< l.len
|
||||
V j = i - 1
|
||||
V key = l[i]
|
||||
L j >= 0 & l[j] > key
|
||||
l[j + 1] = l[j]
|
||||
j--
|
||||
l[j + 1] = key
|
||||
|
||||
V arr = [7, 6, 5, 9, 8, 4, 3, 1, 2, 0]
|
||||
insertion_sort(&arr)
|
||||
print(arr)
|
||||
|
|
@ -0,0 +1,57 @@
|
|||
* Insertion sort 16/06/2016
|
||||
INSSORT CSECT
|
||||
USING INSSORT,R13 base register
|
||||
B 72(R15) skip savearea
|
||||
DC 17F'0' savearea
|
||||
STM R14,R12,12(R13) prolog
|
||||
ST R13,4(R15) "
|
||||
ST R15,8(R13) "
|
||||
LR R13,R15 "
|
||||
LA R6,2 i=2
|
||||
LA R9,A+L'A @a(2)
|
||||
LOOPI C R6,N do i=2 to n
|
||||
BH ELOOPI leave i
|
||||
L R2,0(R9) a(i)
|
||||
ST R2,V v=a(i)
|
||||
LR R7,R6 j=i
|
||||
BCTR R7,0 j=i-1
|
||||
LR R8,R9 @a(i)
|
||||
S R8,=A(L'A) @a(j)
|
||||
LOOPJ LTR R7,R7 do j=i-1 to 1 by -1 while j>0
|
||||
BNH ELOOPJ leave j
|
||||
L R2,0(R8) a(j)
|
||||
C R2,V a(j)>v
|
||||
BNH ELOOPJ leave j
|
||||
MVC L'A(L'A,R8),0(R8) a(j+1)=a(j)
|
||||
BCTR R7,0 j=j-1
|
||||
S R8,=A(L'A) @a(j)
|
||||
B LOOPJ next j
|
||||
ELOOPJ MVC L'A(L'A,R8),V a(j+1)=v;
|
||||
LA R6,1(R6) i=i+1
|
||||
LA R9,L'A(R9) @a(i)
|
||||
B LOOPI next i
|
||||
ELOOPI LA R9,PG pgi=0
|
||||
LA R6,1 i=1
|
||||
LA R8,A @a(1)
|
||||
LOOPXI C R6,N do i=1 to n
|
||||
BH ELOOPXI leave i
|
||||
L R1,0(R8) a(i)
|
||||
XDECO R1,XDEC edit a(i)
|
||||
MVC 0(4,R9),XDEC+8 output a(i)
|
||||
LA R9,4(R9) pgi=pgi+1
|
||||
LA R6,1(R6) i=i+1
|
||||
LA R8,L'A(R8) @a(i)
|
||||
B LOOPXI next i
|
||||
ELOOPXI XPRNT PG,L'PG print buffer
|
||||
L R13,4(0,R13) epilog
|
||||
LM R14,R12,12(R13) "
|
||||
XR R15,R15 "
|
||||
BR R14 exit
|
||||
A DC F'4',F'65',F'2',F'-31',F'0',F'99',F'2',F'83',F'782',F'1'
|
||||
DC F'45',F'82',F'69',F'82',F'104',F'58',F'88',F'112',F'89',F'74'
|
||||
V DS F variable
|
||||
N DC A((V-A)/L'A) n=hbound(a)
|
||||
PG DC CL80' ' buffer
|
||||
XDEC DS CL12 for xdeco
|
||||
YREGS symbolics for registers
|
||||
END INSSORT
|
||||
|
|
@ -0,0 +1,53 @@
|
|||
* Insertion sort 16/06/2016
|
||||
INSSORTS CSECT
|
||||
USING INSSORTS,R13 base register
|
||||
B 72(R15) skip savearea
|
||||
DC 17F'0' savearea
|
||||
STM R14,R12,12(R13) prolog
|
||||
ST R13,4(R15) "
|
||||
ST R15,8(R13) "
|
||||
LR R13,R15 "
|
||||
LA R6,2 i=2
|
||||
LA R9,A+L'A @a(2)
|
||||
DO WHILE=(C,R6,LE,N) do while i<=n
|
||||
L R2,0(R9) a(i)
|
||||
ST R2,V v=a(i)
|
||||
LR R7,R6 j=i
|
||||
BCTR R7,0 j=i-1
|
||||
LR R8,R9 @a(i)
|
||||
S R8,=A(L'A) @a(j)
|
||||
L R2,0(R8) a(j)
|
||||
DO WHILE=(C,R7,GT,0,AND,C,R2,GT,V) do while j>0 & a(j)>v
|
||||
MVC L'A(L'A,R8),0(R8) a(j+1)=a(j)
|
||||
BCTR R7,0 j=j-1
|
||||
S R8,=A(L'A) @a(j)
|
||||
L R2,0(R8) a(j)
|
||||
ENDDO , next j
|
||||
MVC L'A(L'A,R8),V a(j+1)=v;
|
||||
LA R6,1(R6) i=i+1
|
||||
LA R9,L'A(R9) @a(i)
|
||||
ENDDO , next i
|
||||
LA R9,PG pgi=0
|
||||
LA R6,1 i=1
|
||||
LA R8,A @a(1)
|
||||
DO WHILE=(C,R6,LE,N) do while i<=n
|
||||
L R1,0(R8) a(i)
|
||||
XDECO R1,XDEC edit a(i)
|
||||
MVC 0(4,R9),XDEC+8 output a(i)
|
||||
LA R9,4(R9) pgi=pgi+1
|
||||
LA R6,1(R6) i=i+1
|
||||
LA R8,L'A(R8) @a(i)
|
||||
ENDDO , next i
|
||||
XPRNT PG,L'PG print buffer
|
||||
L R13,4(0,R13) epilog
|
||||
LM R14,R12,12(R13) "
|
||||
XR R15,R15 "
|
||||
BR R14 exit
|
||||
A DC F'4',F'65',F'2',F'-31',F'0',F'99',F'2',F'83',F'782',F'1'
|
||||
DC F'45',F'82',F'69',F'82',F'104',F'58',F'88',F'112',F'89',F'74'
|
||||
V DS F variable
|
||||
N DC A((V-A)/L'A) n=hbound(a)
|
||||
PG DC CL80' ' buffer
|
||||
XDEC DS CL12 for xdeco
|
||||
YREGS symbolics for registers
|
||||
END INSSORTS
|
||||
|
|
@ -0,0 +1,161 @@
|
|||
/* ARM assembly AARCH64 Raspberry PI 3B */
|
||||
/* program insertionSort64.s */
|
||||
|
||||
/*******************************************/
|
||||
/* 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 // first element
|
||||
mov x2,NBELEMENTS // number of élements
|
||||
bl insertionSort
|
||||
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
|
||||
/******************************************************************/
|
||||
/* insertion sort */
|
||||
/******************************************************************/
|
||||
/* x0 contains the address of table */
|
||||
/* x1 contains the first element */
|
||||
/* x2 contains the number of element */
|
||||
insertionSort:
|
||||
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
|
||||
add x3,x1,1 // index i
|
||||
1: // start loop 1
|
||||
ldr x4,[x0,x3,lsl 3] // load value A[i]
|
||||
sub x5,x3,1 // index j
|
||||
2: // start loop 2
|
||||
ldr x6,[x0,x5,lsl 3] // load value A[j]
|
||||
cmp x6,x4 // compare value
|
||||
ble 3f
|
||||
add x5,x5,1 // increment index j
|
||||
str x6,[x0,x5,lsl 3] // store value A[j+1}
|
||||
sub x5,x5,2 // j = j - 1
|
||||
cmp x5,x1 // compare first element
|
||||
bge 2b // loop 2
|
||||
3:
|
||||
add x5,x5,1 // increment index j
|
||||
str x4,[x0,x5,lsl 3] // store value A[i}
|
||||
add x3,x3,1 // increment index i
|
||||
cmp x3,x2 // end ?
|
||||
blt 1b // loop 1
|
||||
|
||||
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 conversion10S // 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
|
||||
mov x0,x2
|
||||
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"
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
(defun insert (x xs)
|
||||
(cond ((endp xs) (list x))
|
||||
((< x (first xs))
|
||||
(cons x xs))
|
||||
(t (cons (first xs)
|
||||
(insert x (rest xs))))))
|
||||
|
||||
(defun isort (xs)
|
||||
(if (endp xs)
|
||||
nil
|
||||
(insert (first xs)
|
||||
(isort (rest xs)))))
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
MODE DATA = REF CHAR;
|
||||
|
||||
PROC in place insertion sort = (REF[]DATA item)VOID:
|
||||
BEGIN
|
||||
INT first := LWB item;
|
||||
INT last := UPB item;
|
||||
INT j;
|
||||
DATA value;
|
||||
FOR i FROM first + 1 TO last DO
|
||||
value := item[i];
|
||||
j := i - 1;
|
||||
# WHILE j >= LWB item AND j <= UPB item ANDF item[j] > value DO // example of ANDF extension #
|
||||
WHILE ( j >= LWB item AND j <= UPB item | item[j]>value | FALSE ) DO # no extension! #
|
||||
item[j + 1] := item[j];
|
||||
j -:= 1
|
||||
OD;
|
||||
item[j + 1] := value
|
||||
OD
|
||||
END # in place insertion sort #;
|
||||
|
||||
[32]CHAR data := "big fjords vex quick waltz nymph";
|
||||
[UPB data]DATA ref data; FOR i TO UPB data DO ref data[i] := data[i] OD;
|
||||
in place insertion sort(ref data);
|
||||
FOR i TO UPB ref data DO print(ref data[i]) OD; print(new line);
|
||||
print((data))
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
% insertion sorts in-place the array A. As Algol W procedures can't find the bounds %
|
||||
% of an array parameter, the lower and upper bounds must be specified in lb and ub %
|
||||
procedure insertionSortI ( integer array A ( * ); integer value lb, ub ) ;
|
||||
for i := lb + 1 until ub do begin
|
||||
integer v, j;
|
||||
v := A( i );
|
||||
j := i - 1;
|
||||
while j >= lb and A( j ) > v do begin
|
||||
A( j + 1 ) := A( j );
|
||||
j := j - 1
|
||||
end while_j_ge_0_and_Aj_gt_v ;
|
||||
A( j + 1 ) := v
|
||||
end insertionSortI ;
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
begin
|
||||
% external in-place insertion sort procedure %
|
||||
procedure insertionSortI ( integer array A( * ); integer value lb, ub ) ;
|
||||
algol "ISORTI" ;
|
||||
|
||||
integer array d ( 1 :: 8 );
|
||||
integer p;
|
||||
p := 1;
|
||||
for i := 34, 2, -1, 0, 0, 9, -56, 3 do begin
|
||||
d( p ) := i;
|
||||
p := p + 1
|
||||
end for_i ;
|
||||
insertionSortI( d, 1, 8 );
|
||||
write( i_w := 1, d( 1 ) );
|
||||
for i := 2 until 8 do writeon( i_w := 1, d( i ) )
|
||||
end.
|
||||
|
|
@ -0,0 +1,223 @@
|
|||
/* ARM assembly Raspberry PI */
|
||||
/* program insertionSort.s */
|
||||
/* look Pseudocode begin this task */
|
||||
|
||||
/************************************/
|
||||
/* Constantes */
|
||||
/************************************/
|
||||
.equ STDOUT, 1 @ Linux output console
|
||||
.equ EXIT, 1 @ Linux syscall
|
||||
.equ WRITE, 4 @ Linux syscall
|
||||
/*********************************/
|
||||
/* Initialized data */
|
||||
/*********************************/
|
||||
.data
|
||||
szMessSortOk: .asciz "Table sorted.\n"
|
||||
szMessSortNok: .asciz "Table not sorted !!!!!.\n"
|
||||
sMessResult: .ascii "Value : "
|
||||
sMessValeur: .fill 11, 1, ' ' @ size => 11
|
||||
szCarriageReturn: .asciz "\n"
|
||||
|
||||
.align 4
|
||||
iGraine: .int 123456
|
||||
.equ NBELEMENTS, 10
|
||||
#TableNumber: .int 1,3,6,2,5,9,10,8,4,7
|
||||
TableNumber: .int 10,9,8,7,6,5,4,3,2,1
|
||||
/*********************************/
|
||||
/* UnInitialized data */
|
||||
/*********************************/
|
||||
.bss
|
||||
/*********************************/
|
||||
/* 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 insertionSort
|
||||
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
|
||||
|
||||
iAdrsMessValeur: .int sMessValeur
|
||||
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
|
||||
/******************************************************************/
|
||||
/* insertion sort */
|
||||
/******************************************************************/
|
||||
/* r0 contains the address of table */
|
||||
/* r1 contains the first element */
|
||||
/* r2 contains the number of element */
|
||||
insertionSort:
|
||||
push {r2,r3,r4,lr} @ save registers
|
||||
add r3,r1,#1 @ start index i
|
||||
1: @ start loop
|
||||
ldr r4,[r0,r3,lsl #2] @ load value A[i]
|
||||
sub r5,r3,#1 @ index j
|
||||
2:
|
||||
ldr r6,[r0,r5,lsl #2] @ load value A[j]
|
||||
cmp r6,r4 @ compare value
|
||||
ble 3f
|
||||
add r5,#1 @ increment index j
|
||||
str r6,[r0,r5,lsl #2] @ store value A[j+1]
|
||||
sub r5,#2 @ j = j - 1
|
||||
cmp r5,r1
|
||||
bge 2b @ loop if j >= first item
|
||||
3:
|
||||
add r5,#1 @ increment index j
|
||||
str r4,[r0,r5,lsl #2] @ store value A[i] in A[j+1]
|
||||
add r3,#1 @ increment index i
|
||||
cmp r3,r2 @ end ?
|
||||
blt 1b @ no -> loop
|
||||
|
||||
100:
|
||||
pop {r2,r3,r4,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,iAdrsMessValeur @ display value
|
||||
bl conversion10 @ call function
|
||||
ldr r0,iAdrsMessResult
|
||||
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
|
||||
/******************************************************************/
|
||||
/* display text with size calculation */
|
||||
/******************************************************************/
|
||||
/* r0 contains the address of the message */
|
||||
affichageMess:
|
||||
push {r0,r1,r2,r7,lr} @ save registres
|
||||
mov r2,#0 @ counter length
|
||||
1: @ loop length calculation
|
||||
ldrb r1,[r0,r2] @ read octet start position + index
|
||||
cmp r1,#0 @ if 0 its over
|
||||
addne r2,r2,#1 @ else add 1 in the length
|
||||
bne 1b @ and loop
|
||||
@ so here r2 contains the length of the message
|
||||
mov r1,r0 @ address message in r1
|
||||
mov r0,#STDOUT @ code to write to the standard output Linux
|
||||
mov r7, #WRITE @ code call system "write"
|
||||
svc #0 @ call systeme
|
||||
pop {r0,r1,r2,r7,lr} @ restaur des 2 registres */
|
||||
bx lr @ return
|
||||
/******************************************************************/
|
||||
/* Converting a register to a decimal unsigned */
|
||||
/******************************************************************/
|
||||
/* r0 contains value and r1 address area */
|
||||
/* r0 return size of result (no zero final in area) */
|
||||
/* area size => 11 bytes */
|
||||
.equ LGZONECAL, 10
|
||||
conversion10:
|
||||
push {r1-r4,lr} @ save registers
|
||||
mov r3,r1
|
||||
mov r2,#LGZONECAL
|
||||
|
||||
1: @ start loop
|
||||
bl divisionpar10U @ unsigned r0 <- dividende. quotient ->r0 reste -> r1
|
||||
add r1,#48 @ digit
|
||||
strb r1,[r3,r2] @ store digit on area
|
||||
cmp r0,#0 @ stop if quotient = 0
|
||||
subne r2,#1 @ else previous position
|
||||
bne 1b @ and loop
|
||||
@ and move digit from left of area
|
||||
mov r4,#0
|
||||
2:
|
||||
ldrb r1,[r3,r2]
|
||||
strb r1,[r3,r4]
|
||||
add r2,#1
|
||||
add r4,#1
|
||||
cmp r2,#LGZONECAL
|
||||
ble 2b
|
||||
@ and move spaces in end on area
|
||||
mov r0,r4 @ result length
|
||||
mov r1,#' ' @ space
|
||||
3:
|
||||
strb r1,[r3,r4] @ store space in area
|
||||
add r4,#1 @ next position
|
||||
cmp r4,#LGZONECAL
|
||||
ble 3b @ loop if r4 <= area size
|
||||
|
||||
100:
|
||||
pop {r1-r4,lr} @ restaur registres
|
||||
bx lr @return
|
||||
|
||||
/***************************************************/
|
||||
/* division par 10 unsigned */
|
||||
/***************************************************/
|
||||
/* r0 dividende */
|
||||
/* r0 quotient */
|
||||
/* r1 remainder */
|
||||
divisionpar10U:
|
||||
push {r2,r3,r4, lr}
|
||||
mov r4,r0 @ save value
|
||||
//mov r3,#0xCCCD @ r3 <- magic_number lower raspberry 3
|
||||
//movt r3,#0xCCCC @ r3 <- magic_number higter raspberry 3
|
||||
ldr r3,iMagicNumber @ r3 <- magic_number raspberry 1 2
|
||||
umull r1, r2, r3, r0 @ r1<- Lower32Bits(r1*r0) r2<- Upper32Bits(r1*r0)
|
||||
mov r0, r2, LSR #3 @ r2 <- r2 >> shift 3
|
||||
add r2,r0,r0, lsl #2 @ r2 <- r0 * 5
|
||||
sub r1,r4,r2, lsl #1 @ r1 <- r4 - (r2 * 2) = r4 - (r0 * 10)
|
||||
pop {r2,r3,r4,lr}
|
||||
bx lr @ leave function
|
||||
iMagicNumber: .int 0xCCCCCCCD
|
||||
|
|
@ -0,0 +1,86 @@
|
|||
#include "share/atspre_staload.hats"
|
||||
|
||||
(*------------------------------------------------------------------*)
|
||||
(* Interface *)
|
||||
|
||||
extern fn {a : t@ype} (* The "less than" template. *)
|
||||
insertion_sort$lt : (a, a) -<> bool (* Arguments by value. *)
|
||||
|
||||
extern fn {a : t@ype}
|
||||
insertion_sort
|
||||
{n : int}
|
||||
(arr : &array (a, n) >> _,
|
||||
n : size_t n)
|
||||
:<!wrt> void
|
||||
|
||||
(*------------------------------------------------------------------*)
|
||||
(* Implementation *)
|
||||
|
||||
implement {a}
|
||||
insertion_sort {n} (arr, n) =
|
||||
let
|
||||
macdef lt = insertion_sort$lt<a>
|
||||
|
||||
fun
|
||||
sort {i : int | 1 <= i; i <= n}
|
||||
.<n - i>.
|
||||
(arr : &array (a, n) >> _,
|
||||
i : size_t i)
|
||||
:<!wrt> void =
|
||||
if i <> n then
|
||||
let
|
||||
fun
|
||||
find_new_position
|
||||
{j : nat | j <= i}
|
||||
.<j>.
|
||||
(arr : &array (a, n) >> _,
|
||||
elem : a,
|
||||
j : size_t j)
|
||||
:<> [j : nat | j <= i] size_t j =
|
||||
if j = i2sz 0 then
|
||||
j
|
||||
else if ~(elem \lt arr[pred j]) then
|
||||
j
|
||||
else
|
||||
find_new_position (arr, elem, pred j)
|
||||
|
||||
val j = find_new_position (arr, arr[i], i)
|
||||
in
|
||||
if j < i then
|
||||
array_subcirculate<a> (arr, j, i);
|
||||
sort (arr, succ i)
|
||||
end
|
||||
|
||||
prval () = lemma_array_param arr
|
||||
in
|
||||
if n <> i2sz 0 then
|
||||
sort (arr, i2sz 1)
|
||||
end
|
||||
|
||||
(*------------------------------------------------------------------*)
|
||||
|
||||
implement
|
||||
insertion_sort$lt<int> (x, y) =
|
||||
x < y
|
||||
|
||||
implement
|
||||
main0 () =
|
||||
let
|
||||
#define SIZE 30
|
||||
var i : [i : nat] int i
|
||||
var arr : array (int, SIZE)
|
||||
in
|
||||
array_initize_elt<int> (arr, i2sz SIZE, 0);
|
||||
for (i := 0; i < SIZE; i := succ i)
|
||||
arr[i] := $extfcall (int, "rand") % 10;
|
||||
|
||||
for (i := 0; i < SIZE; i := succ i)
|
||||
print! (" ", arr[i]);
|
||||
println! ();
|
||||
|
||||
insertion_sort<int> (arr, i2sz SIZE);
|
||||
|
||||
for (i := 0; i < SIZE; i := succ i)
|
||||
print! (" ", arr[i]);
|
||||
println! ()
|
||||
end
|
||||
|
|
@ -0,0 +1,162 @@
|
|||
#include "share/atspre_staload.hats"
|
||||
|
||||
(*------------------------------------------------------------------*)
|
||||
(* Interface *)
|
||||
|
||||
extern fn {a : vt@ype} (* The "less than" template. *)
|
||||
insertion_sort$lt : (&a, &a) -<> bool (* Arguments by reference. *)
|
||||
|
||||
extern fn {a : vt@ype}
|
||||
insertion_sort
|
||||
{n : int}
|
||||
(arr : &array (a, n) >> _,
|
||||
n : size_t n)
|
||||
:<!wrt> void
|
||||
|
||||
(*------------------------------------------------------------------*)
|
||||
(* Implementation *)
|
||||
|
||||
implement {a}
|
||||
insertion_sort {n} (arr, n) =
|
||||
let
|
||||
macdef lt = insertion_sort$lt<a>
|
||||
|
||||
fun
|
||||
sort {i : int | 1 <= i; i <= n}
|
||||
{p_arr : addr}
|
||||
.<n - i>.
|
||||
(pf_arr : !array_v (a, p_arr, n) >> _ |
|
||||
p_arr : ptr p_arr,
|
||||
i : size_t i)
|
||||
:<!wrt> void =
|
||||
if i <> n then
|
||||
let
|
||||
val pi = ptr_add<a> (p_arr, i)
|
||||
|
||||
fun
|
||||
find_new_position
|
||||
{j : nat | j <= i}
|
||||
.<j>.
|
||||
(pf_left : !array_v (a, p_arr, j) >> _,
|
||||
pf_i : !a @ (p_arr + (i * sizeof a)) |
|
||||
j : size_t j)
|
||||
:<> [j : nat | j <= i] size_t j =
|
||||
if j = i2sz 0 then
|
||||
j
|
||||
else
|
||||
let
|
||||
prval @(pf_left1, pf_k) = array_v_unextend pf_left
|
||||
|
||||
val k = pred j
|
||||
val pk = ptr_add<a> (p_arr, k)
|
||||
in
|
||||
if ~((!pi) \lt (!pk)) then
|
||||
let
|
||||
prval () = pf_left :=
|
||||
array_v_extend (pf_left1, pf_k)
|
||||
in
|
||||
j
|
||||
end
|
||||
else
|
||||
let
|
||||
val new_pos =
|
||||
find_new_position (pf_left1, pf_i | k)
|
||||
prval () = pf_left :=
|
||||
array_v_extend (pf_left1, pf_k)
|
||||
in
|
||||
new_pos
|
||||
end
|
||||
end
|
||||
|
||||
prval @(pf_left, pf_right) =
|
||||
array_v_split {a} {p_arr} {n} {i} pf_arr
|
||||
prval @(pf_i, pf_rest) = array_v_uncons pf_right
|
||||
|
||||
val j = find_new_position (pf_left, pf_i | i)
|
||||
|
||||
prval () = pf_arr :=
|
||||
array_v_unsplit (pf_left, array_v_cons (pf_i, pf_rest))
|
||||
in
|
||||
if j < i then
|
||||
array_subcirculate<a> (!p_arr, j, i);
|
||||
sort (pf_arr | p_arr, succ i)
|
||||
end
|
||||
|
||||
prval () = lemma_array_param arr
|
||||
in
|
||||
if n <> i2sz 0 then
|
||||
sort (view@ arr | addr@ arr, i2sz 1)
|
||||
end
|
||||
|
||||
(*------------------------------------------------------------------*)
|
||||
|
||||
(* The demonstration converts random numbers to linear strings, then
|
||||
sorts the elements by their first character. Thus here is a simple
|
||||
demonstration that the sort can handle elements of linear type, and
|
||||
also that the sort is stable. *)
|
||||
|
||||
implement
|
||||
main0 () =
|
||||
let
|
||||
implement
|
||||
insertion_sort$lt<Strptr1> (x, y) =
|
||||
let
|
||||
val sx = $UNSAFE.castvwtp1{string} x
|
||||
and sy = $UNSAFE.castvwtp1{string} y
|
||||
val cx = $effmask_all $UNSAFE.string_get_at (sx, 0)
|
||||
and cy = $effmask_all $UNSAFE.string_get_at (sy, 0)
|
||||
in
|
||||
cx < cy
|
||||
end
|
||||
|
||||
implement
|
||||
array_initize$init<Strptr1> (i, x) =
|
||||
let
|
||||
#define BUFSIZE 10
|
||||
var buffer : array (char, BUFSIZE)
|
||||
|
||||
val () = array_initize_elt<char> (buffer, i2sz BUFSIZE, '\0')
|
||||
val _ = $extfcall (int, "snprintf", addr@ buffer,
|
||||
i2sz BUFSIZE, "%d",
|
||||
$extfcall (int, "rand") % 100)
|
||||
val () = buffer[BUFSIZE - 1] := '\0'
|
||||
in
|
||||
x := string0_copy ($UNSAFE.cast{string} buffer)
|
||||
end
|
||||
|
||||
implement
|
||||
array_uninitize$clear<Strptr1> (i, x) =
|
||||
strptr_free x
|
||||
|
||||
#define SIZE 30
|
||||
val @(pf_arr, pfgc_arr | p_arr) =
|
||||
array_ptr_alloc<Strptr1> (i2sz SIZE)
|
||||
macdef arr = !p_arr
|
||||
|
||||
var i : [i : nat] int i
|
||||
in
|
||||
array_initize<Strptr1> (arr, i2sz SIZE);
|
||||
|
||||
for (i := 0; i < SIZE; i := succ i)
|
||||
let
|
||||
val p = ptr_add<Strptr1> (p_arr, i)
|
||||
val s = $UNSAFE.ptr0_get<string> p
|
||||
in
|
||||
print! (" ", s)
|
||||
end;
|
||||
println! ();
|
||||
|
||||
insertion_sort<Strptr1> (arr, i2sz SIZE);
|
||||
|
||||
for (i := 0; i < SIZE; i := succ i)
|
||||
let
|
||||
val p = ptr_add<Strptr1> (p_arr, i)
|
||||
val s = $UNSAFE.ptr0_get<string> p
|
||||
in
|
||||
print! (" ", s)
|
||||
end;
|
||||
println! ();
|
||||
|
||||
array_uninitize<Strptr1> (arr, i2sz SIZE);
|
||||
array_ptr_free (pf_arr, pfgc_arr | p_arr)
|
||||
end
|
||||
|
|
@ -0,0 +1,186 @@
|
|||
#include "share/atspre_staload.hats"
|
||||
|
||||
(*------------------------------------------------------------------*)
|
||||
(* Interface *)
|
||||
|
||||
extern fn {a : vt@ype} (* The "less than" template. *)
|
||||
insertion_sort$lt : (&a, &a) -<> bool (* Arguments by reference. *)
|
||||
|
||||
extern fn {a : vt@ype}
|
||||
insertion_sort
|
||||
{n : int}
|
||||
(lst : list_vt (a, n))
|
||||
:<!wrt> list_vt (a, n)
|
||||
|
||||
(*------------------------------------------------------------------*)
|
||||
(* Implementation *)
|
||||
|
||||
(* This implementation is based on the insertion-sort part of the
|
||||
mergesort code of the ATS prelude.
|
||||
|
||||
Unlike the prelude, however, I build the sorted list in reverse
|
||||
order. Building the list in reverse order actually makes the
|
||||
implementation more like that for an array. *)
|
||||
|
||||
(* Some convenient shorthands. *)
|
||||
#define NIL list_vt_nil ()
|
||||
#define :: list_vt_cons
|
||||
|
||||
(* Inserting in reverse order minimizes the work for a list already
|
||||
nearly sorted, or for stably sorting a list whose entries often
|
||||
have equal keys. *)
|
||||
fun {a : vt@ype}
|
||||
insert_reverse
|
||||
{m : nat}
|
||||
{p_xnode : addr}
|
||||
{p_x : addr}
|
||||
{p_xs : addr}
|
||||
.<m>.
|
||||
(pf_x : a @ p_x,
|
||||
pf_xs : list_vt (a, 0)? @ p_xs |
|
||||
dst : &list_vt (a, m) >> list_vt (a, m + 1),
|
||||
(* list_vt_cons_unfold is a viewtype created by the
|
||||
unfolding of a list_vt_cons (our :: operator). *)
|
||||
xnode : list_vt_cons_unfold (p_xnode, p_x, p_xs),
|
||||
p_x : ptr p_x,
|
||||
p_xs : ptr p_xs)
|
||||
:<!wrt> void =
|
||||
(* dst is some tail of the current (reverse-order) destination list.
|
||||
xnode is a viewtype for the current node in the source list.
|
||||
p_x points to the node's CAR.
|
||||
p_xs points to the node's CDR. *)
|
||||
case+ dst of
|
||||
| @ (y :: ys) =>
|
||||
if insertion_sort$lt<a> (!p_x, y) then
|
||||
let (* Move to the next destination node. *)
|
||||
val () = insert_reverse (pf_x, pf_xs | ys, xnode, p_x, p_xs)
|
||||
prval () = fold@ dst
|
||||
in
|
||||
end
|
||||
else
|
||||
let (* Insert xnode here. *)
|
||||
prval () = fold@ dst
|
||||
val () = !p_xs := dst
|
||||
val () = dst := xnode
|
||||
prval () = fold@ dst
|
||||
in
|
||||
end
|
||||
| ~ NIL =>
|
||||
let (* Put xnode at the end. *)
|
||||
val () = dst := xnode
|
||||
val () = !p_xs := NIL
|
||||
prval () = fold@ dst
|
||||
in
|
||||
end
|
||||
|
||||
implement {a}
|
||||
insertion_sort {n} lst =
|
||||
let
|
||||
fun (* Create a list sorted in reverse. *)
|
||||
loop {i : nat | i <= n}
|
||||
.<n - i>.
|
||||
(dst : &list_vt (a, i) >> list_vt (a, n),
|
||||
src : list_vt (a, n - i))
|
||||
:<!wrt> void =
|
||||
case+ src of
|
||||
| @ (x :: xs) =>
|
||||
let
|
||||
val tail = xs
|
||||
in
|
||||
insert_reverse<a> (view@ x, view@ xs |
|
||||
dst, src, addr@ x, addr@ xs);
|
||||
loop (dst, tail)
|
||||
end
|
||||
| ~ NIL => () (* We are done. *)
|
||||
|
||||
prval () = lemma_list_vt_param lst
|
||||
|
||||
var dst : List_vt a = NIL
|
||||
in
|
||||
loop (dst, lst);
|
||||
|
||||
(* Reversing a linear list is an in-place operation. *)
|
||||
list_vt_reverse<a> dst
|
||||
end
|
||||
|
||||
(*------------------------------------------------------------------*)
|
||||
|
||||
(* The demonstration converts random numbers to linear strings, then
|
||||
sorts the elements by their first character. Thus here is a simple
|
||||
demonstration that the sort can handle elements of linear type, and
|
||||
also that the sort is stable. *)
|
||||
|
||||
implement
|
||||
main0 () =
|
||||
let
|
||||
implement
|
||||
insertion_sort$lt<Strptr1> (x, y) =
|
||||
let
|
||||
val sx = $UNSAFE.castvwtp1{string} x
|
||||
and sy = $UNSAFE.castvwtp1{string} y
|
||||
val cx = $effmask_all $UNSAFE.string_get_at (sx, 0)
|
||||
and cy = $effmask_all $UNSAFE.string_get_at (sy, 0)
|
||||
in
|
||||
cx < cy
|
||||
end
|
||||
|
||||
implement
|
||||
list_vt_freelin$clear<Strptr1> x =
|
||||
strptr_free x
|
||||
|
||||
#define SIZE 30
|
||||
|
||||
fn
|
||||
create_the_list ()
|
||||
:<!wrt> list_vt (Strptr1, SIZE) =
|
||||
let
|
||||
fun
|
||||
loop {i : nat | i <= SIZE}
|
||||
.<SIZE - i>.
|
||||
(lst : list_vt (Strptr1, i),
|
||||
i : size_t i)
|
||||
:<!wrt> list_vt (Strptr1, SIZE) =
|
||||
if i = i2sz SIZE then
|
||||
list_vt_reverse lst
|
||||
else
|
||||
let
|
||||
#define BUFSIZE 10
|
||||
var buffer : array (char, BUFSIZE)
|
||||
|
||||
val () =
|
||||
array_initize_elt<char> (buffer, i2sz BUFSIZE, '\0')
|
||||
val _ = $extfcall (int, "snprintf", addr@ buffer,
|
||||
i2sz BUFSIZE, "%d",
|
||||
$extfcall (int, "rand") % 100)
|
||||
val () = buffer[BUFSIZE - 1] := '\0'
|
||||
val s = string0_copy ($UNSAFE.cast{string} buffer)
|
||||
in
|
||||
loop (s :: lst, succ i)
|
||||
end
|
||||
in
|
||||
loop (NIL, i2sz 0)
|
||||
end
|
||||
|
||||
var p : List string
|
||||
|
||||
val lst = create_the_list ()
|
||||
|
||||
val () =
|
||||
for (p := $UNSAFE.castvwtp1{List string} lst;
|
||||
isneqz p;
|
||||
p := list_tail p)
|
||||
print! (" ", list_head p)
|
||||
val () = println! ()
|
||||
|
||||
val lst = insertion_sort<Strptr1> lst
|
||||
|
||||
val () =
|
||||
for (p := $UNSAFE.castvwtp1{List string} lst;
|
||||
isneqz p;
|
||||
p := list_tail p)
|
||||
print! (" ", list_head p)
|
||||
val () = println! ()
|
||||
|
||||
val () = list_vt_freelin lst
|
||||
in
|
||||
end
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
{
|
||||
line[NR] = $0
|
||||
}
|
||||
END { # sort it with insertion sort
|
||||
for(i=1; i <= NR; i++) {
|
||||
value = line[i]
|
||||
j = i - 1
|
||||
while( ( j > 0) && ( line[j] > value ) ) {
|
||||
line[j+1] = line[j]
|
||||
j--
|
||||
}
|
||||
line[j+1] = value
|
||||
}
|
||||
#print it
|
||||
for(i=1; i <= NR; i++) {
|
||||
print line[i]
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,52 @@
|
|||
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 InsertionSort(INT ARRAY a INT size)
|
||||
INT i,j,value
|
||||
|
||||
FOR i=1 TO size-1
|
||||
DO
|
||||
value=a(i)
|
||||
j=i-1
|
||||
WHILE j>=0 AND a(j)>value
|
||||
DO
|
||||
a(j+1)=a(j)
|
||||
j==-1
|
||||
OD
|
||||
a(j+1)=value
|
||||
OD
|
||||
RETURN
|
||||
|
||||
PROC Test(INT ARRAY a INT size)
|
||||
PrintE("Array before sort:")
|
||||
PrintArray(a,size)
|
||||
InsertionSort(a,size)
|
||||
PrintE("Array after sort:")
|
||||
PrintArray(a,size)
|
||||
PutE()
|
||||
RETURN
|
||||
|
||||
PROC Main()
|
||||
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]
|
||||
|
||||
Test(a,10)
|
||||
Test(b,21)
|
||||
Test(c,8)
|
||||
Test(d,12)
|
||||
RETURN
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
function insertionSort(array:Array)
|
||||
{
|
||||
for(var i:int = 1; i < array.length;i++)
|
||||
{
|
||||
var value = array[i];
|
||||
var j:int = i-1;
|
||||
while(j >= 0 && array[j] > value)
|
||||
{
|
||||
array[j+1] = array[j];
|
||||
j--;
|
||||
}
|
||||
array[j+1] = value;
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
type Data_Array is array(Natural range <>) of Integer;
|
||||
|
||||
procedure Insertion_Sort(Item : in out Data_Array) is
|
||||
First : Natural := Item'First;
|
||||
Last : Natural := Item'Last;
|
||||
Value : Integer;
|
||||
J : Integer;
|
||||
begin
|
||||
for I in (First + 1)..Last loop
|
||||
Value := Item(I);
|
||||
J := I - 1;
|
||||
while J in Item'range and then Item(J) > Value loop
|
||||
Item(J + 1) := Item(J);
|
||||
J := J - 1;
|
||||
end loop;
|
||||
Item(J + 1) := Value;
|
||||
end loop;
|
||||
end Insertion_Sort;
|
||||
|
|
@ -0,0 +1,53 @@
|
|||
-- In-place insertion sort
|
||||
on insertionSort(theList, l, r) -- Sort items l thru r of theList.
|
||||
set listLength to (count theList)
|
||||
if (listLength < 2) then return
|
||||
-- Convert negative and/or transposed range indices.
|
||||
if (l < 0) then set l to listLength + l + 1
|
||||
if (r < 0) then set r to listLength + r + 1
|
||||
if (l > r) then set {l, r} to {r, l}
|
||||
|
||||
-- The list as a script property to allow faster references to its items.
|
||||
script o
|
||||
property lst : theList
|
||||
end script
|
||||
|
||||
-- Set up a minor optimisation whereby the latest instance of the highest value so far isn't
|
||||
-- put back into the list until either it's superseded or the end of the sort is reached.
|
||||
set highestSoFar to o's lst's item l
|
||||
set rv to o's lst's item (l + 1)
|
||||
if (highestSoFar > rv) then
|
||||
set o's lst's item l to rv
|
||||
else
|
||||
set highestSoFar to rv
|
||||
end if
|
||||
-- Work through the rest of the range, rotating values back into the sorted group as necessary.
|
||||
repeat with j from (l + 2) to r
|
||||
set rv to o's lst's item j
|
||||
if (highestSoFar > rv) then
|
||||
repeat with i from (j - 2) to l by -1
|
||||
set lv to o's lst's item i
|
||||
if (lv > rv) then
|
||||
set o's lst's item (i + 1) to lv
|
||||
else
|
||||
set i to i + 1
|
||||
exit repeat
|
||||
end if
|
||||
end repeat
|
||||
set o's lst's item i to rv
|
||||
else
|
||||
set o's lst's item (j - 1) to highestSoFar
|
||||
set highestSoFar to rv
|
||||
end if
|
||||
end repeat
|
||||
set o's lst's item r to highestSoFar
|
||||
|
||||
return -- nothing.
|
||||
end insertionSort
|
||||
property sort : insertionSort
|
||||
|
||||
-- Demo:
|
||||
local aList
|
||||
set aList to {60, 73, 11, 66, 6, 77, 41, 97, 59, 45, 64, 15, 91, 100, 22, 89, 77, 59, 54, 61}
|
||||
sort(aList, 1, -1) -- Sort items 1 thru -1 of aList.
|
||||
return aList
|
||||
|
|
@ -0,0 +1 @@
|
|||
{6, 11, 15, 22, 41, 45, 54, 59, 59, 60, 61, 64, 66, 73, 77, 77, 89, 91, 97, 100}
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
insertionSort: function [items][
|
||||
arr: new items
|
||||
loop 1..(size items)-1 'i [
|
||||
value: arr\[i]
|
||||
j: i - 1
|
||||
|
||||
while [and? -> j >= 0
|
||||
-> value < arr\[j]]
|
||||
[
|
||||
arr\[j+1]: arr\[j]
|
||||
j: j - 1
|
||||
]
|
||||
arr\[j+1]: value
|
||||
]
|
||||
return arr
|
||||
]
|
||||
|
||||
print insertionSort [3 1 2 8 5 7 9 4 6]
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
MsgBox % InsertionSort("")
|
||||
MsgBox % InsertionSort("xxx")
|
||||
MsgBox % InsertionSort("3,2,1")
|
||||
MsgBox % InsertionSort("dog,000000,xx,cat,pile,abcde,1,cat,zz,xx,z")
|
||||
|
||||
InsertionSort(var) { ; SORT COMMA SEPARATED LIST
|
||||
StringSplit a, var, `, ; make array, size = a0
|
||||
Loop % a0-1 {
|
||||
i := A_Index+1, v := a%i%, j := i-1
|
||||
While j>0 and a%j%>v
|
||||
u := j+1, a%u% := a%j%, j--
|
||||
u := j+1, a%u% := v
|
||||
}
|
||||
Loop % a0 ; construct string from sorted array
|
||||
sorted .= "," . a%A_Index%
|
||||
Return SubStr(sorted,2) ; drop leading comma
|
||||
}
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
Sub InsertionSort (A() As Int)
|
||||
For i = 1 To A.Length - 1
|
||||
Dim value As Int = A(i)
|
||||
Dim j As Int = i - 1
|
||||
Do While j >= 0 And A(j) > value
|
||||
A(j + 1) = A(j)
|
||||
j = j - 1
|
||||
Loop
|
||||
A(j + 1) = value
|
||||
Next
|
||||
End Sub
|
||||
|
||||
Sub Test
|
||||
Dim arr() As Int = Array As Int(34, 23, 54, 123, 543, 123)
|
||||
InsertionSort(arr)
|
||||
For Each i As Int In arr
|
||||
Log(i)
|
||||
Next
|
||||
End Sub
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
DECLARE SUB InsertionSort (theList() AS INTEGER)
|
||||
|
||||
DIM n(10) AS INTEGER, L AS INTEGER, o AS STRING
|
||||
FOR L = 0 TO 10
|
||||
n(L) = INT(RND * 32768)
|
||||
NEXT
|
||||
InsertionSort n()
|
||||
FOR L = 1 TO 10
|
||||
PRINT n(L); ";";
|
||||
NEXT
|
||||
|
||||
SUB InsertionSort (theList() AS INTEGER)
|
||||
DIM insertionElementIndex AS INTEGER
|
||||
FOR insertionElementIndex = 1 TO UBOUND(theList)
|
||||
DIM insertionElement AS INTEGER
|
||||
insertionElement = theList(insertionElementIndex)
|
||||
DIM j AS INTEGER
|
||||
j = insertionElementIndex - 1
|
||||
DO WHILE (j >= 0)
|
||||
'necessary for BASICs without short-circuit evaluation
|
||||
IF (insertionElement < theList(j)) THEN
|
||||
theList(j + 1) = theList(j)
|
||||
j = j - 1
|
||||
ELSE
|
||||
EXIT DO
|
||||
END IF
|
||||
LOOP
|
||||
theList(j + 1) = insertionElement
|
||||
NEXT
|
||||
END SUB
|
||||
|
|
@ -0,0 +1,35 @@
|
|||
global array
|
||||
dim array(15)
|
||||
a = array[?,]
|
||||
b = array[?]
|
||||
for i = a to b-1
|
||||
array[i] = int(rand * 100)
|
||||
next i
|
||||
|
||||
print "unsort ";
|
||||
for i = a to b-1
|
||||
print rjust(array[i], 4);
|
||||
next i
|
||||
|
||||
call insertionSort(array) # ordenar el array
|
||||
|
||||
print chr(10); " sort ";
|
||||
for i = a to b-1
|
||||
print rjust(array[i], 4);
|
||||
next i
|
||||
end
|
||||
|
||||
subroutine insertionSort(array)
|
||||
lb = array[?,]
|
||||
|
||||
for i = lb + 1 to array[?]-1
|
||||
valor = array[i]
|
||||
j = i - 1
|
||||
while j >= lb and array[j] > valor
|
||||
array[j +1] = array[j]
|
||||
j -= 1
|
||||
end while
|
||||
|
||||
array[j+1] = valor
|
||||
next i
|
||||
end subroutine
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
DIM test(9)
|
||||
test() = 4, 65, 2, -31, 0, 99, 2, 83, 782, 1
|
||||
PROCinsertionsort(test(), 10)
|
||||
FOR i% = 0 TO 9
|
||||
PRINT test(i%) ;
|
||||
NEXT
|
||||
PRINT
|
||||
END
|
||||
|
||||
DEF PROCinsertionsort(a(), n%)
|
||||
LOCAL i%, j%, t
|
||||
FOR i% = 1 TO n%-1
|
||||
t = a(i%)
|
||||
j% = i%
|
||||
WHILE j%>0 AND t<a(ABS(j%-1))
|
||||
a(j%) = a(j%-1)
|
||||
j% -= 1
|
||||
ENDWHILE
|
||||
a(j%) = t
|
||||
NEXT
|
||||
ENDPROC
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
get "libhdr"
|
||||
|
||||
let insertionSort(A, len) be
|
||||
for i = 1 to len-1 do
|
||||
$( let value = A!i
|
||||
let j = i-1
|
||||
while j >= 0 & A!j > value do
|
||||
$( A!(j+1) := A!j
|
||||
j := j-1
|
||||
$)
|
||||
A!(j+1) := value
|
||||
$)
|
||||
|
||||
let write(s, A, len) be
|
||||
$( writes(s)
|
||||
for i=0 to len-1 do writed(A!i, 4)
|
||||
wrch('*N')
|
||||
$)
|
||||
|
||||
let start() be
|
||||
$( let array = table 4,65,2,-31,0,99,2,83,782,1
|
||||
let length = 10
|
||||
write("Before: ", array, length)
|
||||
insertionSort(array, length)
|
||||
write("After: ", array, length)
|
||||
$)
|
||||
|
|
@ -0,0 +1,33 @@
|
|||
#include <algorithm>
|
||||
#include <iostream>
|
||||
#include <iterator>
|
||||
|
||||
// std::rotate is used to shift the sub-region
|
||||
// if the predicate p is true
|
||||
template <typename RandomAccessIterator, typename Predicate>
|
||||
void insertion_sort(RandomAccessIterator begin, RandomAccessIterator end,
|
||||
Predicate p) {
|
||||
for (auto i = begin; i != end; ++i) {
|
||||
std::rotate(std::upper_bound(begin, i, *i, p), i, i + 1);
|
||||
}
|
||||
}
|
||||
|
||||
// calls with default Predicate std::less (sort ascending)
|
||||
template <typename RandomAccessIterator>
|
||||
void insertion_sort(RandomAccessIterator begin, RandomAccessIterator end) {
|
||||
insertion_sort(begin, end, std::less<typename std::iterator_traits<RandomAccessIterator>::value_type>());
|
||||
}
|
||||
|
||||
|
||||
int main() {
|
||||
|
||||
int a[] = { 100, 2, 56, 200, -52, 3, 99, 33, 177, -199 };
|
||||
|
||||
insertion_sort(std::begin(a), std::end(a));
|
||||
|
||||
// 'iterates' numbers to std::cout
|
||||
// converts ints to strings for output to screen
|
||||
copy(std::begin(a), std::end(a), std::ostream_iterator<int>(std::cout, " "));
|
||||
|
||||
std::cout << "\n";
|
||||
}
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
namespace Sort {
|
||||
using System;
|
||||
|
||||
static class InsertionSort<T> where T : IComparable {
|
||||
public static void Sort(T[] entries) {
|
||||
Sort(entries, 0, entries.Length - 1);
|
||||
}
|
||||
|
||||
public static void Sort(T[] entries, Int32 first, Int32 last) {
|
||||
for (var i = first + 1; i <= last; i++) {
|
||||
var entry = entries[i];
|
||||
var j = i;
|
||||
|
||||
while (j > first && entries[j - 1].CompareTo(entry) > 0)
|
||||
entries[j] = entries[--j];
|
||||
|
||||
entries[j] = entry;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
using Sort;
|
||||
using System;
|
||||
|
||||
class Program {
|
||||
static void Main(String[] args) {
|
||||
var entries = new Int32[] { 3, 9, 4, 6, 8, 1, 7, 2, 5 };
|
||||
InsertionSort<Int32>.Sort(entries);
|
||||
Console.WriteLine(String.Join(" ", entries));
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
#include <stdio.h>
|
||||
|
||||
void insertion_sort(int*, const size_t);
|
||||
|
||||
void insertion_sort(int *a, const size_t n) {
|
||||
for(size_t i = 1; i < n; ++i) {
|
||||
int key = a[i];
|
||||
size_t j = i;
|
||||
while( (j > 0) && (key < a[j - 1]) ) {
|
||||
a[j] = a[j - 1];
|
||||
--j;
|
||||
}
|
||||
a[j] = key;
|
||||
}
|
||||
}
|
||||
|
||||
int main (int argc, char** argv) {
|
||||
|
||||
int a[] = {4, 65, 2, -31, 0, 99, 2, 83, 782, 1};
|
||||
|
||||
const size_t n = sizeof(a) / sizeof(a[0]) ; // array extent
|
||||
|
||||
for (size_t i = 0; i < n; i++)
|
||||
printf("%d%s", a[i], (i == (n - 1))? "\n" : " ");
|
||||
|
||||
insertion_sort(a, n);
|
||||
|
||||
for (size_t i = 0; i < n; i++)
|
||||
printf("%d%s", a[i], (i == (n - 1))? "\n" : " ");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -0,0 +1,36 @@
|
|||
% Insertion-sort an array in place.
|
||||
insertion_sort = proc [T: type] (a: array[T])
|
||||
where T has lt: proctype (T,T) returns (bool)
|
||||
|
||||
bound_lo: int := array[T]$low(a)
|
||||
bound_hi: int := array[T]$high(a)
|
||||
|
||||
for i: int in int$from_to(bound_lo, bound_hi) do
|
||||
value: T := a[i]
|
||||
j: int := i - 1
|
||||
while j >= bound_lo cand value < a[j] do
|
||||
a[j+1] := a[j]
|
||||
j := j-1
|
||||
end
|
||||
a[j+1] := value
|
||||
end
|
||||
end insertion_sort
|
||||
|
||||
% Print an array
|
||||
print_arr = proc [T: type] (a: array[T], w: int, s: stream)
|
||||
where T has unparse: proctype (T) returns (string)
|
||||
for el: T in array[T]$elements(a) do
|
||||
stream$putright(s, T$unparse(el), w)
|
||||
end
|
||||
stream$putc(s, '\n')
|
||||
end print_arr
|
||||
|
||||
start_up = proc ()
|
||||
ai = array[int]
|
||||
po: stream := stream$primary_output()
|
||||
test: ai := ai$[7, -5, 0, 2, 99, 16, 4, 20, 47, 19]
|
||||
|
||||
stream$puts(po, "Before: ") print_arr[int](test, 3, po)
|
||||
insertion_sort[int](test)
|
||||
stream$puts(po, "After: ") print_arr[int](test, 3, po)
|
||||
end start_up
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
# insertion_sort(var [value1 value2...]) sorts a list of integers.
|
||||
function(insertion_sort var)
|
||||
math(EXPR last "${ARGC} - 1") # Sort ARGV[1..last].
|
||||
foreach(i RANGE 1 ${last})
|
||||
# Extend the sorted area to ARGV[1..i].
|
||||
set(b ${i})
|
||||
set(v ${ARGV${b}})
|
||||
# Insert v == ARGV[b] in sorted order. While b > 1, check if b is
|
||||
# too high, then decrement b. After loop, set ARGV[b] = v.
|
||||
while(b GREATER 1)
|
||||
math(EXPR a "${b} - 1")
|
||||
set(u ${ARGV${a}})
|
||||
# Now u == ARGV[a]. Pretend v == ARGV[b]. Compare.
|
||||
if(u GREATER ${v})
|
||||
# ARGV[a] and ARGV[b] are in wrong order. Fix by moving ARGV[a]
|
||||
# to ARGV[b], making room for later insertion of v.
|
||||
set(ARGV${b} ${u})
|
||||
else()
|
||||
break()
|
||||
endif()
|
||||
math(EXPR b "${b} - 1")
|
||||
endwhile()
|
||||
set(ARGV${b} ${v})
|
||||
endforeach(i)
|
||||
|
||||
set(answer)
|
||||
foreach(i RANGE 1 ${last})
|
||||
list(APPEND answer ${ARGV${i}})
|
||||
endforeach(i)
|
||||
set("${var}" "${answer}" PARENT_SCOPE)
|
||||
endfunction(insertion_sort)
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
insertion_sort(result 33 11 44 22 66 55)
|
||||
message(STATUS "${result}") # -- 11;22;33;44;55;66
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
C-PROCESS SECTION.
|
||||
PERFORM E-INSERTION VARYING WB-IX-1 FROM 1 BY 1
|
||||
UNTIL WB-IX-1 > WC-SIZE.
|
||||
|
||||
...
|
||||
|
||||
E-INSERTION SECTION.
|
||||
E-000.
|
||||
MOVE WB-ENTRY(WB-IX-1) TO WC-TEMP.
|
||||
SET WB-IX-2 TO WB-IX-1.
|
||||
|
||||
PERFORM F-PASS UNTIL WB-IX-2 NOT > 1 OR
|
||||
WC-TEMP NOT < WB-ENTRY(WB-IX-2 - 1).
|
||||
|
||||
IF WB-IX-1 NOT = WB-IX-2
|
||||
MOVE WC-TEMP TO WB-ENTRY(WB-IX-2).
|
||||
|
||||
E-999.
|
||||
EXIT.
|
||||
|
||||
F-PASS SECTION.
|
||||
F-000.
|
||||
MOVE WB-ENTRY(WB-IX-2 - 1) TO WB-ENTRY(WB-IX-2).
|
||||
SET WB-IX-2 DOWN BY 1.
|
||||
|
||||
F-999.
|
||||
EXIT.
|
||||
|
|
@ -0,0 +1,70 @@
|
|||
>>SOURCE FORMAT FREE
|
||||
*> This code is dedicated to the public domain
|
||||
*> This is GNUCOBOL 2.0
|
||||
identification division.
|
||||
program-id. insertionsort.
|
||||
environment division.
|
||||
configuration section.
|
||||
repository. function all intrinsic.
|
||||
data division.
|
||||
working-storage section.
|
||||
01 filler.
|
||||
03 a pic 99.
|
||||
03 a-lim pic 99 value 10.
|
||||
03 array occurs 10 pic 99.
|
||||
|
||||
01 filler.
|
||||
03 s pic 99.
|
||||
03 o pic 99.
|
||||
03 o1 pic 99.
|
||||
03 sorted-len pic 99.
|
||||
03 sorted-lim pic 99 value 10.
|
||||
03 sorted-array occurs 10 pic 99.
|
||||
|
||||
procedure division.
|
||||
start-insertionsort.
|
||||
|
||||
*> fill the array
|
||||
compute a = random(seconds-past-midnight)
|
||||
perform varying a from 1 by 1 until a > a-lim
|
||||
compute array(a) = random() * 100
|
||||
end-perform
|
||||
|
||||
*> display the array
|
||||
perform varying a from 1 by 1 until a > a-lim
|
||||
display space array(a) with no advancing
|
||||
end-perform
|
||||
display space 'initial array'
|
||||
|
||||
*> sort the array
|
||||
move 0 to sorted-len
|
||||
perform varying a from 1 by 1 until a > a-lim
|
||||
*> find the insertion point
|
||||
perform varying s from 1 by 1
|
||||
until s > sorted-len
|
||||
or array(a) <= sorted-array(s)
|
||||
continue
|
||||
end-perform
|
||||
|
||||
*>open the insertion point
|
||||
perform varying o from sorted-len by -1
|
||||
until o < s
|
||||
compute o1 = o + 1
|
||||
move sorted-array(o) to sorted-array(o1)
|
||||
end-perform
|
||||
|
||||
*> move the array-entry to the insertion point
|
||||
move array(a) to sorted-array(s)
|
||||
|
||||
add 1 to sorted-len
|
||||
end-perform
|
||||
|
||||
*> display the sorted array
|
||||
perform varying s from 1 by 1 until s > sorted-lim
|
||||
display space sorted-array(s) with no advancing
|
||||
end-perform
|
||||
display space 'sorted array'
|
||||
|
||||
stop run
|
||||
.
|
||||
end program insertionsort.
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
(defn insertion-sort [coll]
|
||||
(reduce (fn [result input]
|
||||
(let [[less more] (split-with #(< % input) result)]
|
||||
(concat less [input] more)))
|
||||
[]
|
||||
coll))
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
(defn in-sort! [data]
|
||||
(letfn [(insert ([raw x](insert [] raw x))
|
||||
([sorted [y & raw] x]
|
||||
(if (nil? y) (conj sorted x)
|
||||
(if (<= x y ) (concat sorted [x,y] raw)
|
||||
(recur (conj sorted y) raw x )))))]
|
||||
(reduce insert [] data)))
|
||||
;Usage:(in-sort! [6,8,5,9,3,2,1,4,7])
|
||||
;Returns: [1 2 3 4 5 6 7 8 9]
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
10 DIM A(10): N=9
|
||||
11 REM GENERATE SOME RANDOM NUMBERS AND PRINT THEM
|
||||
12 FOR I=0 TO N: A(I)=INT(RND(1)*10)+1: NEXT: GOSUB 50
|
||||
20 FOR J=1 TO N:KEY=A(J): I=J-1: GOSUB 30: A(I+1)=KEY: NEXT: GOSUB 50: END
|
||||
30 IFI=-1 THEN RETURN
|
||||
31 IFA(I)>KEY THEN A(I+1)=A(I):I=I-1: GOTO 30
|
||||
32 RETURN
|
||||
50 PRINT: FOR I=0 TO N: PRINTA(I): NEXT: RETURN
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
(defun span (predicate list)
|
||||
(let ((tail (member-if-not predicate list)))
|
||||
(values (ldiff list tail) tail)))
|
||||
|
||||
(defun less-than (x)
|
||||
(lambda (y) (< y x)))
|
||||
|
||||
(defun insert (list elt)
|
||||
(multiple-value-bind (left right) (span (less-than elt) list)
|
||||
(append left (list elt) right)))
|
||||
|
||||
(defun insertion-sort (list)
|
||||
(reduce #'insert list :initial-value nil))
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
(defun insertion-sort (sequence &optional (predicate #'<))
|
||||
(if (cdr sequence)
|
||||
(insert (car sequence) ;; insert the current item into
|
||||
(insertion-sort (cdr sequence) ;; the already-sorted
|
||||
predicate) ;; remainder of the list
|
||||
predicate)
|
||||
sequence)) ; a list of one element is already sorted
|
||||
|
||||
(defun insert (item sequence predicate)
|
||||
(cond ((null sequence) (list item))
|
||||
((funcall (complement predicate) ;; if the first element of the list
|
||||
(car sequence) ;; isn't better than the item,
|
||||
item) ;; cons the item onto
|
||||
(cons item sequence)) ;; the front of the list
|
||||
(t (cons (car sequence) ;; otherwise cons the first element onto the front of
|
||||
(insert item ;; the list of the item sorted with the rest of the list
|
||||
(cdr sequence)
|
||||
predicate)))))
|
||||
|
|
@ -0,0 +1,57 @@
|
|||
define size = 10, value = 0
|
||||
|
||||
dim list[size]
|
||||
|
||||
gosub fill
|
||||
gosub sort
|
||||
gosub show
|
||||
|
||||
end
|
||||
|
||||
sub fill
|
||||
|
||||
for i = 0 to size - 1
|
||||
|
||||
let list[i] = int(rnd * 100)
|
||||
|
||||
next i
|
||||
|
||||
return
|
||||
|
||||
sub sort
|
||||
|
||||
for i = 1 to size - 1
|
||||
|
||||
let value = list[i]
|
||||
let j = i - 1
|
||||
|
||||
do
|
||||
|
||||
if j >= 0 and list[j] > value then
|
||||
|
||||
let p = j + 1
|
||||
let list[p] = list[j]
|
||||
let j = j - 1
|
||||
|
||||
endif
|
||||
|
||||
loop j >= 0 and list[j] > value
|
||||
|
||||
let p = j + 1
|
||||
let list[p] = value
|
||||
|
||||
wait
|
||||
|
||||
next i
|
||||
|
||||
return
|
||||
|
||||
sub show
|
||||
|
||||
for i = 0 to size - 1
|
||||
|
||||
print i, ": ", list[i]
|
||||
|
||||
next i
|
||||
|
||||
return
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
void insertionSort(T)(T[] data) pure nothrow @safe @nogc {
|
||||
foreach (immutable i, value; data[1 .. $]) {
|
||||
auto j = i + 1;
|
||||
for ( ; j > 0 && value < data[j - 1]; j--)
|
||||
data[j] = data[j - 1];
|
||||
data[j] = value;
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
import std.stdio;
|
||||
auto items = [28, 44, 46, 24, 19, 2, 17, 11, 25, 4];
|
||||
items.insertionSort;
|
||||
items.writeln;
|
||||
}
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
import std.stdio, std.range, std.algorithm, std.traits;
|
||||
|
||||
void insertionSort(R)(R arr)
|
||||
if (hasLength!R && isRandomAccessRange!R && hasSlicing!R) {
|
||||
foreach (immutable i; 1 .. arr.length)
|
||||
bringToFront(arr[0 .. i].assumeSorted.upperBound(arr[i]), arr[i .. i + 1]);
|
||||
}
|
||||
|
||||
void main() {
|
||||
import std.random, std.container;
|
||||
|
||||
auto arr1 = [28, 44, 46, 24, 19, 2, 17, 11, 25, 4];
|
||||
arr1.insertionSort;
|
||||
assert(arr1.isSorted);
|
||||
writeln("arr1 sorted: ", arr1);
|
||||
|
||||
auto arr2 = Array!int([28, 44, 46, 24, 19, 2, 17, 11, 25, 4]);
|
||||
arr2[].insertionSort;
|
||||
assert(arr2[].isSorted);
|
||||
writeln("arr2 sorted: ", arr2[]);
|
||||
|
||||
// Random data test.
|
||||
int[10] buf;
|
||||
foreach (immutable _; 0 .. 100_000) {
|
||||
auto arr3 = buf[0 .. uniform(0, $)];
|
||||
foreach (ref x; arr3)
|
||||
x = uniform(-6, 6);
|
||||
arr3.insertionSort;
|
||||
assert(arr3.isSorted);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
insertSort(List<int> array){
|
||||
for(int i = 1; i < array.length; i++){
|
||||
int value = array[i];
|
||||
int j = i - 1;
|
||||
while(j >= 0 && array[j] > value){
|
||||
array[j + 1] = array[j];
|
||||
j = j - 1;
|
||||
}
|
||||
array[j + 1] = value;
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
void main() {
|
||||
List<int> a = insertSort([10, 3, 11, 15, 19, 1]);
|
||||
print('${a}');
|
||||
}
|
||||
|
|
@ -0,0 +1,50 @@
|
|||
program TestInsertionSort;
|
||||
|
||||
{$APPTYPE CONSOLE}
|
||||
|
||||
{.$DEFINE DYNARRAY} // remove '.' to compile with dynamic array
|
||||
|
||||
type
|
||||
TItem = Integer; // declare ordinal type for array item
|
||||
{$IFDEF DYNARRAY}
|
||||
TArray = array of TItem; // dynamic array
|
||||
{$ELSE}
|
||||
TArray = array[0..15] of TItem; // static array
|
||||
{$ENDIF}
|
||||
|
||||
procedure InsertionSort(var A: TArray);
|
||||
var
|
||||
I, J: Integer;
|
||||
Item: TItem;
|
||||
|
||||
begin
|
||||
for I:= 1 + Low(A) to High(A) do begin
|
||||
Item:= A[I];
|
||||
J:= I - 1;
|
||||
while (J >= Low(A)) and (A[J] > Item) do begin
|
||||
A[J + 1]:= A[J];
|
||||
Dec(J);
|
||||
end;
|
||||
A[J + 1]:= Item;
|
||||
end;
|
||||
end;
|
||||
|
||||
var
|
||||
A: TArray;
|
||||
I: Integer;
|
||||
|
||||
begin
|
||||
{$IFDEF DYNARRAY}
|
||||
SetLength(A, 16);
|
||||
{$ENDIF}
|
||||
for I:= Low(A) to High(A) do
|
||||
A[I]:= Random(100);
|
||||
for I:= Low(A) to High(A) do
|
||||
Write(A[I]:3);
|
||||
Writeln;
|
||||
InsertionSort(A);
|
||||
for I:= Low(A) to High(A) do
|
||||
Write(A[I]:3);
|
||||
Writeln;
|
||||
Readln;
|
||||
end.
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
procedure InsertionSort(var S: string);
|
||||
var
|
||||
I, J, L: Integer;
|
||||
Ch: Char;
|
||||
|
||||
begin
|
||||
L:= Length(S);
|
||||
for I:= 2 to L do begin
|
||||
Ch:= S[I];
|
||||
J:= I - 1;
|
||||
while (J > 0) and (S[J] > Ch) do begin
|
||||
S[J + 1]:= S[J];
|
||||
Dec(J);
|
||||
end;
|
||||
S[J + 1]:= Ch;
|
||||
end;
|
||||
end;
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
def insertionSort(array) {
|
||||
for i in 1..!(array.size()) {
|
||||
def value := array[i]
|
||||
var j := i-1
|
||||
while (j >= 0 && array[j] > value) {
|
||||
array[j + 1] := array[j]
|
||||
j -= 1
|
||||
}
|
||||
array[j+1] := value
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
? def a := [71, 53, 22, 24, 83, 54, 39, 78, 65, 26, 60, 75, 67, 27, 52, 59, 93, 62, 85, 99, 88, 10, 91, 85, 13, 17, 14, 96, 55, 10, 61, 94, 27, 50, 75, 40, 47, 63, 10, 23].diverge()
|
||||
> insertionSort(a)
|
||||
> a
|
||||
# value: [10, 10, 10, 13, 14, 17, 22, 23, 24, 26, 27, 27, 39, 40, 47, 50, 52, 53, 54, 55, 59, 60, 61, 62, 63, 65, 67, 71, 75, 75, 78, 83, 85, 85, 88, 91, 93, 94, 96, 99].diverge()
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
fun insertionSort = void by List a # sort list in place
|
||||
for int i = 1; i < a.length; ++i
|
||||
var v = a[i]
|
||||
int j
|
||||
for j = i - 1; j >= 0 and a[j] > v; --j
|
||||
a[j + 1] = a[j]
|
||||
end
|
||||
a[j + 1] = v
|
||||
end
|
||||
end
|
||||
List lists = List[ # a list of lists
|
||||
int[4, 65, 2, -31, 0, 99, 83, 782, 1],
|
||||
real[5.17, 2, 5.12],
|
||||
text["this", "is", "insertion", "sort"]]
|
||||
for each List list in lists
|
||||
writeLine("Before: " + text!list) # list as text
|
||||
insertionSort(list)
|
||||
writeLine("After : " + text!list)
|
||||
writeLine()
|
||||
end
|
||||
|
|
@ -0,0 +1,33 @@
|
|||
PROGRAM INSERTION_SORT
|
||||
|
||||
DIM A[9]
|
||||
|
||||
PROCEDURE INSERTION_SORT(A[])
|
||||
LOCAL I,J
|
||||
FOR I=0 TO UBOUND(A,1) DO
|
||||
V=A[I]
|
||||
J=I-1
|
||||
WHILE J>=0 DO
|
||||
IF A[J]>V THEN
|
||||
A[J+1]=A[J]
|
||||
J=J-1
|
||||
ELSE
|
||||
EXIT
|
||||
END IF
|
||||
END WHILE
|
||||
A[J+1]=V
|
||||
END FOR
|
||||
END PROCEDURE
|
||||
|
||||
BEGIN
|
||||
A[]=(4,65,2,-31,0,99,2,83,782,1)
|
||||
FOR I%=0 TO UBOUND(A,1) DO
|
||||
PRINT(A[I%];)
|
||||
END FOR
|
||||
PRINT
|
||||
INSERTION_SORT(A[])
|
||||
FOR I%=0 TO UBOUND(A,1) DO
|
||||
PRINT(A[I%];)
|
||||
END FOR
|
||||
PRINT
|
||||
END PROGRAM
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
proc sort . d[] .
|
||||
for i = 2 to len d[]
|
||||
h = d[i]
|
||||
j = i - 1
|
||||
while j >= 1 and h < d[j]
|
||||
d[j + 1] = d[j]
|
||||
j -= 1
|
||||
.
|
||||
d[j + 1] = h
|
||||
.
|
||||
.
|
||||
data[] = [ 29 4 72 44 55 26 27 77 92 5 ]
|
||||
call sort data[]
|
||||
print data[]
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
class
|
||||
MY_SORTED_SET [G -> COMPARABLE]
|
||||
inherit
|
||||
TWO_WAY_SORTED_SET [G]
|
||||
redefine
|
||||
sort
|
||||
end
|
||||
create
|
||||
make
|
||||
|
||||
feature
|
||||
sort
|
||||
-- Insertion sort
|
||||
local
|
||||
l_j: INTEGER
|
||||
l_value: like item
|
||||
do
|
||||
across 2 |..| count as ii loop
|
||||
from
|
||||
l_j := ii.item - 1
|
||||
l_value := Current.i_th (ii.item)
|
||||
until
|
||||
l_j < 1 or Current.i_th (l_j) <= l_value
|
||||
loop
|
||||
Current.i_th (l_j + 1) := Current.i_th (l_j)
|
||||
l_j := l_j - 1
|
||||
end
|
||||
Current.i_th (l_j + 1) := l_value
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
|
@ -0,0 +1,33 @@
|
|||
import extensions;
|
||||
|
||||
extension op
|
||||
{
|
||||
insertionSort()
|
||||
= self.clone().insertionSort(0, self.Length - 1);
|
||||
|
||||
insertionSort(int first, int last)
|
||||
{
|
||||
for(int i := first + 1, i <= last, i += 1)
|
||||
{
|
||||
var entry := self[i];
|
||||
int j := i;
|
||||
|
||||
while (j > first && self[j - 1] > entry)
|
||||
{
|
||||
self[j] := self[j - 1];
|
||||
|
||||
j -= 1
|
||||
};
|
||||
|
||||
self[j] := entry
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public program()
|
||||
{
|
||||
var list := new int[]{3, 9, 4, 6, 8, 1, 7, 2, 5};
|
||||
|
||||
console.printLine("before:", list.asEnumerable());
|
||||
console.printLine("after :", list.insertionSort().asEnumerable());
|
||||
}
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
defmodule Sort do
|
||||
def insert_sort(list) when is_list(list), do: insert_sort(list, [])
|
||||
|
||||
def insert_sort([], sorted), do: sorted
|
||||
def insert_sort([h | t], sorted), do: insert_sort(t, insert(h, sorted))
|
||||
|
||||
defp insert(x, []), do: [x]
|
||||
defp insert(x, sorted) when x < hd(sorted), do: [x | sorted]
|
||||
defp insert(x, [h | t]), do: [h | insert(x, t)]
|
||||
end
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
(defun min-or-max-of-a-list (numbers comparator)
|
||||
"Return minimum or maximum of NUMBERS using COMPARATOR."
|
||||
(let ((extremum (car numbers)))
|
||||
(dolist (n (cdr numbers))
|
||||
(when (funcall comparator n extremum)
|
||||
(setq extremum n)))
|
||||
extremum))
|
||||
|
||||
(defun remove-number-from-list (numbers n)
|
||||
"Return NUMBERS without N.
|
||||
If n is present twice or more, it will be removed only once."
|
||||
(let (result)
|
||||
(while numbers
|
||||
(let ((number (pop numbers)))
|
||||
(if (= number n)
|
||||
(while numbers
|
||||
(push (pop numbers) result))
|
||||
(push number result))))
|
||||
(nreverse result)))
|
||||
|
||||
(defun insertion-sort (numbers comparator)
|
||||
"Return sorted list of NUMBERS using COMPARATOR."
|
||||
(if numbers
|
||||
(let ((extremum (min-or-max-of-a-list numbers comparator)))
|
||||
(cons extremum
|
||||
(insertion-sort (remove-number-from-list numbers extremum)
|
||||
comparator)))
|
||||
nil))
|
||||
|
||||
(insertion-sort '(1 2 3 9 8 7 25 12 3 2 1) #'>)
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
-module(sort).
|
||||
-export([insertion/1]).
|
||||
|
||||
insertion(L) -> lists:foldl(fun insert/2, [], L).
|
||||
|
||||
insert(X,[]) -> [X];
|
||||
insert(X,L=[H|_]) when X =< H -> [X|L];
|
||||
insert(X,[H|T]) -> [H|insert(X, T)].
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
1> c(sort).
|
||||
{ok,sort}
|
||||
2> sort:insertion([5,3,9,4,1,6,8,2,7]).
|
||||
[1,2,3,4,5,6,7,8,9]
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
function insertion_sort(sequence s)
|
||||
object temp
|
||||
integer j
|
||||
for i = 2 to length(s) do
|
||||
temp = s[i]
|
||||
j = i-1
|
||||
while j >= 1 and compare(s[j],temp) > 0 do
|
||||
s[j+1] = s[j]
|
||||
j -= 1
|
||||
end while
|
||||
s[j+1] = temp
|
||||
end for
|
||||
return s
|
||||
end function
|
||||
|
||||
include misc.e
|
||||
constant s = {4, 15, "delta", 2, -31, 0, "alfa", 19, "gamma", 2, 13, "beta", 782, 1}
|
||||
|
||||
puts(1,"Before: ")
|
||||
pretty_print(1,s,{2})
|
||||
puts(1,"\nAfter: ")
|
||||
pretty_print(1,insertion_sort(s),{2})
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
// This function performs an insertion sort with an array.
|
||||
// The input parameter is a generic array (any type that can perform comparison).
|
||||
// As is typical of functional programming style the input array is not modified;
|
||||
// a copy of the input array is made and modified and returned.
|
||||
let insertionSort (A: _ array) =
|
||||
let B = Array.copy A
|
||||
for i = 1 to B.Length - 1 do
|
||||
let mutable value = B.[i]
|
||||
let mutable j = i - 1
|
||||
while (j >= 0 && B.[j] > value) do
|
||||
B.[j+1] <- B.[j]
|
||||
j <- j - 1
|
||||
B.[j+1] <- value
|
||||
B // the array B is returned
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
let insertionSort collection =
|
||||
|
||||
// Inserts an element into its correct place in a sorted collection
|
||||
let rec sinsert element collection =
|
||||
match element, collection with
|
||||
| x, [] -> [x]
|
||||
| x, y::ys when x < y -> x::y::ys
|
||||
| x, y::ys -> y :: (ys |> sinsert x)
|
||||
|
||||
// Performs Insertion Sort
|
||||
let rec isort acc collection =
|
||||
match collection, acc with
|
||||
| [], _ -> acc
|
||||
| x::xs, ys -> xs |> isort (sinsert x ys)
|
||||
collection |> isort []
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
USING: kernel prettyprint sorting.extras sequences ;
|
||||
|
||||
: insertion-sort ( seq -- sorted-seq )
|
||||
<reversed> V{ } clone [ swap insort-left! ] reduce ;
|
||||
|
||||
{ 6 8 5 9 3 2 1 4 7 } insertion-sort .
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
: insert ( start end -- start )
|
||||
dup @ >r ( r: v ) \ v = a[i]
|
||||
begin
|
||||
2dup < \ j>0
|
||||
while
|
||||
r@ over cell- @ < \ a[j-1] > v
|
||||
while
|
||||
cell- \ j--
|
||||
dup @ over cell+ ! \ a[j] = a[j-1]
|
||||
repeat then
|
||||
r> swap ! ; \ a[j] = v
|
||||
|
||||
: sort ( array len -- )
|
||||
1 ?do dup i cells + insert loop drop ;
|
||||
|
||||
create test 7 , 3 , 0 , 2 , 9 , 1 , 6 , 8 , 4 , 5 ,
|
||||
test 10 sort
|
||||
test 10 cells dump
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
subroutine sort(n, a)
|
||||
implicit none
|
||||
integer :: n, i, j
|
||||
real :: a(n), x
|
||||
|
||||
do i = 2, n
|
||||
x = a(i)
|
||||
j = i - 1
|
||||
do while (j >= 1)
|
||||
if (a(j) <= x) exit
|
||||
a(j + 1) = a(j)
|
||||
j = j - 1
|
||||
end do
|
||||
a(j + 1) = x
|
||||
end do
|
||||
end subroutine
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
SUBROUTINE SORT(N,A)
|
||||
IMPLICIT NONE
|
||||
INTEGER N,I,J
|
||||
DOUBLE PRECISION A(N),X
|
||||
DO 30 I = 2,N
|
||||
X = A(I)
|
||||
J = I
|
||||
10 J = J - 1
|
||||
IF (J.EQ.0) GO TO 20
|
||||
IF (A(J).LE.X) GO TO 20
|
||||
A(J + 1) = A(J)
|
||||
GO TO 10
|
||||
20 A(J + 1) = X
|
||||
30 CONTINUE
|
||||
END
|
||||
|
|
@ -0,0 +1,50 @@
|
|||
' version 20-10-2016
|
||||
' compile with: fbc -s console
|
||||
' for boundry checks on array's compile with: fbc -s console -exx
|
||||
|
||||
Sub insertionSort( arr() As Long )
|
||||
|
||||
' sort from lower bound to the highter bound
|
||||
' array's can have subscript range from -2147483648 to +2147483647
|
||||
|
||||
Dim As Long lb = LBound(arr)
|
||||
Dim As Long i, j, value
|
||||
|
||||
For i = lb +1 To UBound(arr)
|
||||
|
||||
value = arr(i)
|
||||
j = i -1
|
||||
While j >= lb And arr(j) > value
|
||||
arr(j +1) = arr(j)
|
||||
j = j -1
|
||||
Wend
|
||||
|
||||
arr(j +1) = value
|
||||
|
||||
Next
|
||||
|
||||
End Sub
|
||||
|
||||
' ------=< MAIN >=------
|
||||
|
||||
Dim As Long i, array(-7 To 7)
|
||||
Dim As Long a = LBound(array), b = UBound(array)
|
||||
|
||||
Randomize Timer
|
||||
For i = a To b : array(i) = i : Next
|
||||
For i = a To b ' little shuffle
|
||||
Swap array(i), array(Int(Rnd * (b - a +1)) + a)
|
||||
Next
|
||||
|
||||
Print "unsort ";
|
||||
For i = a To b : Print Using "####"; array(i); : Next : Print
|
||||
insertionSort(array()) ' sort the array
|
||||
Print " sort ";
|
||||
For i = a To b : Print Using "####"; array(i); : Next : Print
|
||||
|
||||
|
||||
' empty keyboard buffer
|
||||
While Inkey <> "" : Wend
|
||||
Print : Print "hit any key to end program"
|
||||
Sleep
|
||||
End
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
InsertionSort := function(L)
|
||||
local n, i, j, x;
|
||||
n := Length(L);
|
||||
for i in [ 2 .. n ] do
|
||||
x := L[i];
|
||||
j := i - 1;
|
||||
while j >= 1 and L[j] > x do
|
||||
L[j + 1] := L[j];
|
||||
j := j - 1;
|
||||
od;
|
||||
L[j + 1] := x;
|
||||
od;
|
||||
end;
|
||||
|
||||
s := "BFKRIMPOQACNESWUTXDGLVZHYJ";
|
||||
InsertionSort(s);
|
||||
s;
|
||||
# "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func insertionSort(a []int) {
|
||||
for i := 1; i < len(a); i++ {
|
||||
value := a[i]
|
||||
j := i - 1
|
||||
for j >= 0 && a[j] > value {
|
||||
a[j+1] = a[j]
|
||||
j = j - 1
|
||||
}
|
||||
a[j+1] = value
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
list := []int{31, 41, 59, 26, 53, 58, 97, 93, 23, 84}
|
||||
fmt.Println("unsorted:", list)
|
||||
|
||||
insertionSort(list)
|
||||
fmt.Println("sorted! ", list)
|
||||
}
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
)
|
||||
|
||||
func insertionSort(a sort.Interface) {
|
||||
for i := 1; i < a.Len(); i++ {
|
||||
for j := i; j > 0 && a.Less(j, j-1); j-- {
|
||||
a.Swap(j-1, j)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
list := []int{31, 41, 59, 26, 53, 58, 97, 93, 23, 84}
|
||||
fmt.Println("unsorted:", list)
|
||||
|
||||
insertionSort(sort.IntSlice(list))
|
||||
fmt.Println("sorted! ", list)
|
||||
}
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
)
|
||||
|
||||
func insertionSort(a []int) {
|
||||
for i := 1; i < len(a); i++ {
|
||||
value := a[i]
|
||||
j := sort.Search(i, func(k int) bool { return a[k] > value })
|
||||
copy(a[j+1:i+1], a[j:i])
|
||||
a[j] = value
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
list := []int{31, 41, 59, 26, 53, 58, 97, 93, 23, 84}
|
||||
fmt.Println("unsorted:", list)
|
||||
|
||||
insertionSort(list)
|
||||
fmt.Println("sorted! ", list)
|
||||
}
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
def insertionSort = { list ->
|
||||
|
||||
def size = list.size()
|
||||
(1..<size).each { i ->
|
||||
def value = list[i]
|
||||
def j = i - 1
|
||||
for (; j >= 0 && list[j] > value; j--) {
|
||||
print "."; list[j+1] = list[j]
|
||||
}
|
||||
print "."; list[j+1] = value
|
||||
}
|
||||
list
|
||||
}
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
println (insertionSort([23,76,99,58,97,57,35,89,51,38,95,92,24,46,31,24,14,12,57,78,4]))
|
||||
println (insertionSort([88,18,31,44,4,0,8,81,14,78,20,76,84,33,73,75,82,5,62,70,12,7,1]))
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
import Data.List (insert)
|
||||
|
||||
insertionSort :: Ord a => [a] -> [a]
|
||||
insertionSort = foldr insert []
|
||||
|
||||
-- Example use:
|
||||
-- *Main> insertionSort [6,8,5,9,3,2,1,4,7]
|
||||
-- [1,2,3,4,5,6,7,8,9]
|
||||
|
|
@ -0,0 +1,33 @@
|
|||
class InsertionSort {
|
||||
@:generic
|
||||
public static function sort<T>(arr:Array<T>) {
|
||||
for (i in 1...arr.length) {
|
||||
var value = arr[i];
|
||||
var j = i - 1;
|
||||
while (j >= 0 && Reflect.compare(arr[j], value) > 0) {
|
||||
arr[j + 1] = arr[j--];
|
||||
}
|
||||
arr[j + 1] = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class Main {
|
||||
static function main() {
|
||||
var integerArray = [1, 10, 2, 5, -1, 5, -19, 4, 23, 0];
|
||||
var floatArray = [1.0, -3.2, 5.2, 10.8, -5.7, 7.3,
|
||||
3.5, 0.0, -4.1, -9.5];
|
||||
var stringArray = ['We', 'hold', 'these', 'truths', 'to',
|
||||
'be', 'self-evident', 'that', 'all',
|
||||
'men', 'are', 'created', 'equal'];
|
||||
Sys.println('Unsorted Integers: ' + integerArray);
|
||||
InsertionSort.sort(integerArray);
|
||||
Sys.println('Sorted Integers: ' + integerArray);
|
||||
Sys.println('Unsorted Floats: ' + floatArray);
|
||||
InsertionSort.sort(floatArray);
|
||||
Sys.println('Sorted Floats: ' + floatArray);
|
||||
Sys.println('Unsorted Strings: ' + stringArray);
|
||||
InsertionSort.sort(stringArray);
|
||||
Sys.println('Sorted Strings: ' + stringArray);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
DO i = 2, LEN(A)
|
||||
value = A(i)
|
||||
j = i - 1
|
||||
1 IF( j > 0 ) THEN
|
||||
IF( A(j) > value ) THEN
|
||||
A(j+1) = A(j)
|
||||
j = j - 1
|
||||
GOTO 1 ! no WHILE in HicEst
|
||||
ENDIF
|
||||
ENDIF
|
||||
A(j+1) = value
|
||||
ENDDO
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
100 PROGRAM "InserSrt.bas"
|
||||
110 RANDOMIZE
|
||||
120 NUMERIC ARRAY(5 TO 21)
|
||||
130 CALL INIT(ARRAY)
|
||||
140 CALL WRITE(ARRAY)
|
||||
150 CALL INSERTSORT(ARRAY)
|
||||
160 CALL WRITE(ARRAY)
|
||||
170 DEF INIT(REF A)
|
||||
180 FOR I=LBOUND(A) TO UBOUND(A)
|
||||
190 LET A(I)=RND(98)+1
|
||||
200 NEXT
|
||||
210 END DEF
|
||||
220 DEF WRITE(REF A)
|
||||
230 FOR I=LBOUND(A) TO UBOUND(A)
|
||||
240 PRINT A(I);
|
||||
250 NEXT
|
||||
260 PRINT
|
||||
270 END DEF
|
||||
280 DEF INSERTSORT(REF A)
|
||||
290 FOR J=LBOUND(A)+1 TO UBOUND(A)
|
||||
300 LET I=J-1:LET SW=A(J)
|
||||
310 DO WHILE I>=LBOUND(A) AND SW<A(I)
|
||||
320 LET A(I+1)=A(I):LET I=I-1
|
||||
330 LOOP
|
||||
340 LET A(I+1)=SW
|
||||
350 NEXT
|
||||
360 END DEF
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
procedure main() #: demonstrate various ways to sort a list and string
|
||||
demosort(insertionsort,[3, 14, 1, 5, 9, 2, 6, 3],"qwerty")
|
||||
end
|
||||
|
||||
procedure insertionsort(X,op) #: return sorted X
|
||||
local i,temp
|
||||
|
||||
op := sortop(op,X) # select how and what we sort
|
||||
|
||||
every i := 2 to *X do {
|
||||
temp := X[j := i]
|
||||
while op(temp,X[1 <= (j -:= 1)]) do
|
||||
X[j+1] := X[j]
|
||||
X[j+1] := temp
|
||||
}
|
||||
return X
|
||||
end
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
List do(
|
||||
insertionSortInPlace := method(
|
||||
for(j, 1, size - 1,
|
||||
key := at(j)
|
||||
i := j - 1
|
||||
|
||||
while(i >= 0 and at(i) > key,
|
||||
atPut(i + 1, at(i))
|
||||
i = i - 1
|
||||
)
|
||||
atPut(i + 1, key)
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
lst := list(7, 6, 5, 9, 8, 4, 3, 1, 2, 0)
|
||||
lst insertionSortInPlace println # ==> list(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
List do(
|
||||
insertionSortInPlace := method(
|
||||
# In fact, we could've done slice(1, size - 1) foreach(...)
|
||||
# but creating a new list in memory can only make it worse.
|
||||
foreach(idx, key,
|
||||
newidx := slice(0, idx) map(x, x > key) indexOf(true)
|
||||
if(newidx, insertAt(removeAt(idx), newidx))
|
||||
)
|
||||
self)
|
||||
)
|
||||
|
||||
lst := list(7, 6, 5, 9, 8, 4, 3, 1, 2, 0)
|
||||
lst insertionSortInPlace println # ==> list(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
|
||||
|
|
@ -0,0 +1,58 @@
|
|||
theory Insertionsort
|
||||
imports Main
|
||||
begin
|
||||
|
||||
fun insert :: "int ⇒ int list ⇒ int list" where
|
||||
"insert x [] = [x]"
|
||||
| "insert x (y#ys) = (if x ≤ y then (x#y#ys) else y#(insert x ys))"
|
||||
|
||||
text‹Example:›
|
||||
lemma "insert 4 [1, 2, 3, 5, 6] = [1, 2, 3, 4, 5, 6]" by(code_simp)
|
||||
|
||||
fun insertionsort :: "int list ⇒ int list" where
|
||||
"insertionsort [] = []"
|
||||
| "insertionsort (x#xs) = insert x (insertionsort xs)"
|
||||
|
||||
lemma "insertionsort [4, 2, 6, 1, 8, 1] = [1, 1, 2, 4, 6, 8]" by(code_simp)
|
||||
|
||||
text‹
|
||||
Our function behaves the same as the \<^term>‹sort› function of the standard library.
|
||||
›
|
||||
lemma insertionsort: "insertionsort xs = sort xs"
|
||||
proof(induction xs)
|
||||
case Nil
|
||||
show "insertionsort [] = sort []" by simp
|
||||
next
|
||||
case (Cons x xs)
|
||||
text‹Our \<^const>‹insert› behaves the same as the std libs \<^const>‹insort›.›
|
||||
have "insert a as = insort a as" for a as by(induction as) simp+
|
||||
with Cons show "insertionsort (x # xs) = sort (x # xs)" by simp
|
||||
qed
|
||||
|
||||
text‹
|
||||
Given that we behave the same as the std libs sorting algorithm,
|
||||
we get the correctness properties for free.
|
||||
›
|
||||
corollary insertionsort_correctness:
|
||||
"sorted (insertionsort xs)" and
|
||||
"set (insertionsort xs) = set xs"
|
||||
using insertionsort by(simp)+
|
||||
|
||||
text‹
|
||||
The Haskell implementation from
|
||||
🌐‹https://rosettacode.org/wiki/Sorting_algorithms/Insertion_sort#Haskell›
|
||||
also behaves the same. Ultimately, they all return a sorted list.
|
||||
One exception to the Haskell implementation is that the type signature of
|
||||
\<^const>‹foldr› in Isabelle is slightly different:
|
||||
The initial value of the accumulator goes last.
|
||||
›
|
||||
definition rosettacode_haskell_insertionsort :: "int list ⇒ int list" where
|
||||
"rosettacode_haskell_insertionsort ≡ λxs. foldr insert xs []"
|
||||
|
||||
lemma "rosettacode_haskell_insertionsort [4, 2, 6, 1, 8, 1] =
|
||||
[1, 1, 2, 4, 6, 8]" by(code_simp)
|
||||
|
||||
lemma "rosettacode_haskell_insertionsort xs = insertionsort xs"
|
||||
unfolding rosettacode_haskell_insertionsort_def by(induction xs) simp+
|
||||
|
||||
end
|
||||
|
|
@ -0,0 +1 @@
|
|||
isort=:((>: # ]) , [ , < #])/
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
isort 32 4 1 34 95 3 2 120 _38
|
||||
_38 1 2 3 4 32 34 95 120
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
public static void insertSort(int[] A){
|
||||
for(int i = 1; i < A.length; i++){
|
||||
int value = A[i];
|
||||
int j = i - 1;
|
||||
while(j >= 0 && A[j] > value){
|
||||
A[j + 1] = A[j];
|
||||
j = j - 1;
|
||||
}
|
||||
A[j + 1] = value;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
public static <E extends Comparable<? super E>> void insertionSort(List<E> a) {
|
||||
for (int i = 1; i < a.size(); i++) {
|
||||
int j = Math.abs(Collections.binarySearch(a.subList(0, i), a.get(i)) + 1);
|
||||
Collections.rotate(a.subList(j, i+1), j - i);
|
||||
}
|
||||
}
|
||||
public static <E extends Comparable<? super E>> void insertionSort(E[] a) {
|
||||
for (int i = 1; i < a.length; i++) {
|
||||
E x = a[i];
|
||||
int j = Math.abs(Arrays.binarySearch(a, 0, i, x) + 1);
|
||||
System.arraycopy(a, j, a, j+1, i-j);
|
||||
a[j] = x;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
function insertionSort (a) {
|
||||
for (var i = 0; i < a.length; i++) {
|
||||
var k = a[i];
|
||||
for (var j = i; j > 0 && k < a[j - 1]; j--)
|
||||
a[j] = a[j - 1];
|
||||
a[j] = k;
|
||||
}
|
||||
return a;
|
||||
}
|
||||
|
||||
var a = [4, 65, 2, -31, 0, 99, 83, 782, 1];
|
||||
insertionSort(a);
|
||||
document.write(a.join(" "));
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
def insertion_sort:
|
||||
reduce .[] as $x ([]; insert($x));
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
# As soon as "condition" is true, then emit . and stop:
|
||||
def do_until(condition; next):
|
||||
def u: if condition then . else (next|u) end;
|
||||
u;
|
||||
|
|
@ -0,0 +1,38 @@
|
|||
def bsearch(target):
|
||||
if length == 0 then -1
|
||||
elif length == 1 then
|
||||
if target == .[0] then 0 elif target < .[0] then -1 else -2 end
|
||||
else . as $in
|
||||
# state variable: [start, end, answer]
|
||||
# where start and end are the upper and lower offsets to use.
|
||||
| [0, length-1, null]
|
||||
| do_until( .[0] > .[1] ;
|
||||
(if .[2] != null then (.[1] = -1) # i.e. break
|
||||
else
|
||||
( ( (.[1] + .[0]) / 2 ) | floor ) as $mid
|
||||
| $in[$mid] as $monkey
|
||||
| if $monkey == target then (.[2] = $mid) # success
|
||||
elif .[0] == .[1] then (.[1] = -1) # failure
|
||||
elif $monkey < target then (.[0] = ($mid + 1))
|
||||
else (.[1] = ($mid - 1))
|
||||
end
|
||||
end ))
|
||||
| if .[2] == null then # compute the insertion point
|
||||
if $in[ .[0] ] < target then (-2 -.[0])
|
||||
else (-1 -.[0])
|
||||
end
|
||||
else .[2]
|
||||
end
|
||||
end;
|
||||
|
||||
# insert x assuming input is sorted
|
||||
def insert(x):
|
||||
if length == 0 then [x]
|
||||
else
|
||||
bsearch(x) as $i
|
||||
| ( if $i < 0 then -(1+$i) else $i end ) as $i
|
||||
| .[0:$i] + [x] + .[$i:]
|
||||
end ;
|
||||
|
||||
def insertion_sort:
|
||||
reduce .[] as $x ([]; insert($x));
|
||||
|
|
@ -0,0 +1 @@
|
|||
[1, 2, 1, 1.1, -1.1, null, [null], {"null":null}] | insertion_sort
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
# v0.6
|
||||
|
||||
function insertionsort!(A::Array{T}) where T <: Number
|
||||
for i in 1:length(A)-1
|
||||
value = A[i+1]
|
||||
j = i
|
||||
while j > 0 && A[j] > value
|
||||
A[j+1] = A[j]
|
||||
j -= 1
|
||||
end
|
||||
A[j+1] = value
|
||||
end
|
||||
return A
|
||||
end
|
||||
|
||||
x = randn(5)
|
||||
@show x insertionsort!(x)
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
fun insertionSort(array: IntArray) {
|
||||
for (index in 1 until array.size) {
|
||||
val value = array[index]
|
||||
var subIndex = index - 1
|
||||
while (subIndex >= 0 && array[subIndex] > value) {
|
||||
array[subIndex + 1] = array[subIndex]
|
||||
subIndex--
|
||||
}
|
||||
array[subIndex + 1] = value
|
||||
}
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val numbers = intArrayOf(5, 2, 3, 17, 12, 1, 8, 3, 4, 9, 7)
|
||||
|
||||
fun printArray(message: String, array: IntArray) = with(array) {
|
||||
print("$message [")
|
||||
forEachIndexed { index, number ->
|
||||
print(if (index == lastIndex) number else "$number, ")
|
||||
}
|
||||
println("]")
|
||||
}
|
||||
|
||||
printArray("Unsorted:", numbers)
|
||||
insertionSort(numbers)
|
||||
printArray("Sorted:", numbers)
|
||||
}
|
||||
|
|
@ -0,0 +1,39 @@
|
|||
#!/bin/ksh
|
||||
|
||||
# An insertion sort in ksh
|
||||
|
||||
# # Variables:
|
||||
#
|
||||
typeset -a arr=( 4 65 2 -31 0 99 2 83 782 1 )
|
||||
|
||||
# # Functions:
|
||||
#
|
||||
|
||||
# # Function _insertionSort(array) - Insersion sort of array of integers
|
||||
#
|
||||
function _insertionSort {
|
||||
typeset _arr ; nameref _arr="$1"
|
||||
typeset _i _j _val ; integer _i _j _val
|
||||
|
||||
for (( _i=1; _i<${#_arr[*]}; _i++ )); do
|
||||
_val=${_arr[_i]}
|
||||
(( _j = _i - 1 ))
|
||||
while (( _j>=0 && _arr[_j]>_val )); do
|
||||
_arr[_j+1]=${_arr[_j]}
|
||||
(( _j-- ))
|
||||
done
|
||||
_arr[_j+1]=${_val}
|
||||
done
|
||||
}
|
||||
|
||||
######
|
||||
# main #
|
||||
######
|
||||
|
||||
_insertionSort arr
|
||||
|
||||
printf "%s" "( "
|
||||
for (( i=0; i<${#arr[*]}; i++ )); do
|
||||
printf "%d " ${arr[i]}
|
||||
done
|
||||
printf "%s\n" " )"
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
{def sort
|
||||
|
||||
{def sort.i
|
||||
{lambda {:x :a}
|
||||
{if {A.empty? :a}
|
||||
then {A.new :x}
|
||||
else {if {<= :x {A.first :a}}
|
||||
then {A.addfirst! :x :a}
|
||||
else {A.addfirst! {A.first :a} {sort.i :x {A.rest :a}}} }}}}
|
||||
|
||||
{def sort.r
|
||||
{lambda {:a1 :a2}
|
||||
{if {A.empty? :a1}
|
||||
then :a2
|
||||
else {sort.r {A.rest :a1} {sort.i {A.first :a1} :a2}} }}}
|
||||
|
||||
{lambda {:a}
|
||||
{sort.r :a {A.new}} }}
|
||||
-> sort
|
||||
|
||||
{def A {A.new 4 65 2 -31 0 99 83 782 1}}
|
||||
-> A
|
||||
|
||||
{sort {A}}
|
||||
-> [-31,0,1,2,4,65,83,99,782]
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
itemCount = 20
|
||||
dim A(itemCount)
|
||||
for i = 1 to itemCount
|
||||
A(i) = int(rnd(1) * 100)
|
||||
next i
|
||||
|
||||
print "Before Sort"
|
||||
gosub [printArray]
|
||||
|
||||
'--- Insertion sort algorithm
|
||||
for i = 2 to itemCount
|
||||
value = A(i)
|
||||
j = i-1
|
||||
while j >= 0 and A(j) > value
|
||||
A(j+1) = A(j)
|
||||
j = j-1
|
||||
wend
|
||||
A(j+1) = value
|
||||
next
|
||||
'--- end of (Insertion sort algorithm)
|
||||
|
||||
print "After Sort"
|
||||
gosub [printArray]
|
||||
end
|
||||
|
||||
[printArray]
|
||||
for i = 1 to itemCount
|
||||
print using("###", A(i));
|
||||
next i
|
||||
print
|
||||
return
|
||||
|
|
@ -0,0 +1,46 @@
|
|||
do
|
||||
local function lower_bound(container, container_begin, container_end, value, comparator)
|
||||
local count = container_end - container_begin + 1
|
||||
|
||||
while count > 0 do
|
||||
local half = bit.rshift(count, 1) -- or math.floor(count / 2)
|
||||
local middle = container_begin + half
|
||||
|
||||
if comparator(container[middle], value) then
|
||||
container_begin = middle + 1
|
||||
count = count - half - 1
|
||||
else
|
||||
count = half
|
||||
end
|
||||
end
|
||||
|
||||
return container_begin
|
||||
end
|
||||
|
||||
local function binary_insertion_sort_impl(container, comparator)
|
||||
for i = 2, #container do
|
||||
local j = i - 1
|
||||
local selected = container[i]
|
||||
local loc = lower_bound(container, 1, j, selected, comparator)
|
||||
|
||||
while j >= loc do
|
||||
container[j + 1] = container[j]
|
||||
j = j - 1
|
||||
end
|
||||
|
||||
container[j + 1] = selected
|
||||
end
|
||||
end
|
||||
|
||||
local function binary_insertion_sort_comparator(a, b)
|
||||
return a < b
|
||||
end
|
||||
|
||||
function table.bininsertionsort(container, comparator)
|
||||
if not comparator then
|
||||
comparator = binary_insertion_sort_comparator
|
||||
end
|
||||
|
||||
binary_insertion_sort_impl(container, comparator)
|
||||
end
|
||||
end
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
function bins(tb, val, st, en)
|
||||
local st, en = st or 1, en or #tb
|
||||
local mid = math.floor((st + en)/2)
|
||||
if en == st then return tb[st] > val and st or st+1
|
||||
else return tb[mid] > val and bins(tb, val, st, mid) or bins(tb, val, mid+1, en)
|
||||
end
|
||||
end
|
||||
function isort(t)
|
||||
local ret = {t[1], t[2]}
|
||||
for i = 3, #t do
|
||||
table.insert(ret, bins(ret, t[i]), t[i])
|
||||
end
|
||||
return ret
|
||||
end
|
||||
|
||||
print(unpack(isort{4,5,2,7,8,3}))
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
function list = insertionSort(list)
|
||||
|
||||
for i = (2:numel(list))
|
||||
|
||||
value = list(i);
|
||||
j = i - 1;
|
||||
|
||||
while (j >= 1) && (list(j) > value)
|
||||
list(j+1) = list(j);
|
||||
j = j-1;
|
||||
end
|
||||
|
||||
list(j+1) = value;
|
||||
|
||||
end %for
|
||||
end %insertionSort
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
>> insertionSort([4 3 1 5 6 2])
|
||||
|
||||
ans =
|
||||
|
||||
1 2 3 4 5 6
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
fn inSort arr =
|
||||
(
|
||||
arr = deepcopy arr
|
||||
for i = 1 to arr.count do
|
||||
(
|
||||
j = i
|
||||
while j > 1 and arr[j-1] > arr[j] do
|
||||
(
|
||||
swap arr[j] arr[j-1]
|
||||
j -= 1
|
||||
)
|
||||
)
|
||||
return arr
|
||||
)
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
b = for i in 1 to 20 collect random 1 40
|
||||
#(2, 28, 35, 31, 27, 24, 2, 22, 15, 34, 9, 10, 22, 40, 26, 5, 23, 6, 18, 33)
|
||||
a = insort b
|
||||
#(2, 2, 5, 6, 9, 10, 15, 18, 22, 22, 23, 24, 26, 27, 28, 31, 33, 34, 35, 40)
|
||||
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