June 2018 Update

This commit is contained in:
Ingy döt Net 2018-06-22 20:57:24 +00:00
parent ba8067c3b7
commit 22f33d4004
5278 changed files with 84726 additions and 14379 deletions

View file

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

View file

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

View file

@ -0,0 +1,33 @@
import extensions.
extension $op
{
insertionSort
= self clone; insertionSort(0, self length - 1).
insertionSort(IntNumber first, IntNumber last)
[
(first + 1) to:last do(:i)
[
var entry := self[i].
int j := i.
while ((j > first)&& $(self[j - 1] > entry))
[
self[j] := self[j - 1].
j -= 1
].
self[j] := entry
]
]
}
program =
[
var list := (3, 9, 4, 6, 8, 1, 7, 2, 5).
console printLine("before:", list).
console printLine("after :", list insertionSort).
].

View file

@ -1,15 +1,16 @@
PURE SUBROUTINE Insertion_Sort(a)
REAL, INTENT(in out), DIMENSION(:) :: a
REAL :: temp
INTEGER :: i, j
subroutine sort(n, a)
implicit none
integer :: n, i, j
real :: a(n), x
DO i = 2, SIZE(a)
j = i - 1
temp = a(i)
DO WHILE (j>=1 .AND. a(j)>temp)
a(j+1) = a(j)
j = j - 1
END DO
a(j+1) = temp
END DO
END SUBROUTINE Insertion_Sort
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

View file

@ -1,7 +1,16 @@
DO i = 2, SIZE(a)
j = i - 1
DO WHILE (j>=1 .AND. a(j) > a(i))
j = j - 1
END DO
a(j+1:i) = cshift(a(j+1:i),-1)
END DO
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
Can't IF (J.EQ.0 .OR. A(J).LE.X) GO TO 20 in case both sides are ALWAYS evaluated.
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

View file

@ -0,0 +1,12 @@
arr := Array([17,3,72,0,36,2,3,8,40,0]):
len := numelems(arr):
for i from 2 to len do
val := arr[i]:
j := i-1:
while(j > 0 and arr[j] > val) do
arr[j+1] := arr[j]:
j--:
end do:
arr[j+1] := val:
end do:
arr;

View file

@ -0,0 +1,55 @@
.de end
..
.de array
. nr \\$1.c 0 1
. de \\$1.push end
. nr \\$1..\\\\n+[\\$1.c] \\\\$1
. end
. de \\$1.pushln end
. if \\\\n(.$>0 .\\$1.push \\\\$1
. if \\\\n(.$>1 \{ \
. shift
. \\$1.pushln \\\\$@
. \}
. end
. de \\$1.dump end
. nr i 0 1
. ds out "
. while \\\\n+i<=\\\\n[\\$1.c] .as out "\\\\n[\\$1..\\\\ni]
. tm \\\\*[out]
. rm out
. rr i
. end
. de \\$1.slideright end
. nr i \\\\$1
. nr i+1 \\\\ni+1
. nr \\$1..\\\\n[i+1] \\\\n[\\$1..\\\\ni]
. rr i
. rr i+1
. end
..
.de insertionsort
. nr keyidx 1 1
. while \\n+[keyidx]<=\\n[\\$1.c] \{ \
. nr key \\n[\\$1..\\n[keyidx]]
. nr compidx \\n[keyidx] 1
. while \\n-[compidx]>=0 \{ \
. if \\n[compidx]=0 \{ \
. nr \\$1..1 \\n[key]
. break
. \}
. ie \\n[\\$1..\\n[compidx]]>\\n[key] \{ \
. \\$1.slideright \\n[compidx]
. \}
. el \{ \
. nr compidx+1 \\n[compidx]+1
. nr \\$1..\\n[compidx+1] \\n[key]
. break
. \}
. \}
. \}
..
.array a
.a.pushln 13 64 22 87 54 87 23 92 11 64 5 9 3 3 0
.insertionsort a
.a.dump

View file

@ -0,0 +1,45 @@
.de end
..
.de array
. nr \\$1.c 0 1
. de \\$1.push end
. nr \\$1..\\\\n+[\\$1.c] \\\\$1
. end
. de \\$1.pushln end
. if \\\\n(.$>0 .\\$1.push \\\\$1
. if \\\\n(.$>1 \{ \
. shift
. \\$1.pushln \\\\$@
. \}
. end
. de \\$1.dump end
. nr i 0 1
. ds out "
. while \\\\n+i<=\\\\n[\\$1.c] .as out "\\\\n[\\$1..\\\\ni]
. tm \\\\*[out]
. rm out
. rr i
. end
. de \\$1.swap end
. if (\\\\$1<=\\\\n[\\$1.c])&(\\\\$1<=\\\\n[\\$1.c]) \{ \
. nr tmp \\\\n[\\$1..\\\\$2]
. nr \\$1..\\\\$2 \\\\n[\\$1..\\\\$1]
. nr \\$1..\\\\$1 \\\\n[tmp]
. rr tmp
. \}
. end
..
.de insertionsort
. nr keyidx 1 1
. while \\n+[keyidx]<=\\n[\\$1.c] \{ \
. nr compidx \\n[keyidx]+1 1
. nr compidx-1 \\n[keyidx] 1
. while (\\n-[compidx]>0)&(\\n[\\$1..\\n-[compidx-1]]>\\n[\\$1..\\n[compidx]]) \{ \
. \\$1.swap \\n[compidx] \\n[compidx-1]
. \}
. \}
..
.array a
.a.pushln 13 64 22 87 54 87 23 92 11 64 5 9 3 3 0
.insertionsort a
.a.dump

View file

@ -0,0 +1,15 @@
mata
void insertion_sort(real vector a) {
real scalar i, j, n, x
n = length(a)
for (i=2; i<=n; i++) {
x = a[i]
for (j=i-1; j>=1; j--) {
if (a[j] <= x) break
a[j+1] = a[j]
}
a[j+1] = x
}
}
end