2016 Update

This commit is contained in:
Tina Müller 2016-12-05 22:15:40 +01:00
parent 948b86eafa
commit dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions

View file

@ -1,6 +1,8 @@
{{Sorting Algorithm}}
{{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.
@ -22,3 +24,4 @@ The algorithm is as follows (from [[wp:Insertion_sort#Algorithm|wikipedia]]):
'''done'''
Writing the algorithm for integers will suffice.
<br><br>

View file

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

View file

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

View file

@ -1,14 +1,15 @@
#include <stdio.h>
void insertion_sort (int *a, int n) {
int i, j, t;
for (i = 1; i < n; i++) {
t = a[i];
for (j = i; j > 0 && t < a[j - 1]; j--) {
a[j] = a[j - 1];
}
a[j] = t;
}
void insertion_sort(int *a, int n) {
for(size_t i = 1; i < n; ++i) {
int tmp = a[i];
size_t j = i;
while(j > 0 && tmp < a[j - 1]) {
a[j] = a[j - 1];
--j;
}
a[j] = tmp;
}
}
int main () {

View file

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

View file

@ -0,0 +1,6 @@
(defn insertion-sort [coll]
(reduce (fn [result input]
(let [[less more] (split-with #(< % input) result)]
(concat less [input] more)))
[]
coll))

View file

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

View file

@ -1,14 +1,12 @@
INSSORT: PROCEDURE (A);
DCL A(*) FIXED BIN(31);
DCL (I, J, V, N) FIXED BIN(31);
INSSORT: PROC(A);
DCL A(*) FIXED BIN(31);
DCL (I,J,V,N,M) FIXED BIN(31);
N = HBOUND(A,1); M = LBOUND(A,1);
DO I=M+1 TO N;
V=A(I);
J=I-1;
DO WHILE (J > M-1);
if A(J) <= V then leave;
A(J+1)=A(J); J=J-1;
DO J=I-1 BY -1 WHILE (J>M-1 & A(J)>V);
A(J+1)=A(J);
END;
A(J+1)=V;
END;

View file

@ -0,0 +1,39 @@
; This program works with REBOL version R2 and R3, to make it work with Red
; change the word func to function
insertion-sort: func [
a [block!]
/local i [integer!] j [integer!] n [integer!]
value [integer! string! date!]
][
i: 2
n: length? a
while [i <= n][
value: a/:i
j: i
while [ all [ 1 < j
value < a/(j - 1) ]][
a/:j: a/(j - 1)
j: j - 1
]
a/:j: value
i: i + 1
]
a
]
probe insertion-sort [4 2 1 6 9 3 8 7]
probe insertion-sort [ "---Monday's Child Is Fair of Face (by Mother Goose)---"
"Monday's child is fair of face;"
"Tuesday's child is full of grace;"
"Wednesday's child is full of woe;"
"Thursday's child has far to go;"
"Friday's child is loving and giving;"
"Saturday's child works hard for a living;"
"But the child that is born on the Sabbath day"
"Is blithe and bonny, good and gay."]
; just by adding the date! type to the local variable value the same function can sort dates.
probe insertion-sort [12-Jan-2015 11-Jan-2015 11-Jan-2016 12-Jan-2014]

View file

@ -1,30 +1,30 @@
/*REXX program sorts a stemmed array using the insertion sort algorithm. */
call gen /*generate the array's elements. */
call show 'before sort' /*display the before array elements. */
say copies('',79) /*display a separator line (a fence). */
call insertionSort # /*invoke the insertion sort. */
call show ' after sort' /*display the after array elements. */
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────GEN subroutine────────────────────────────*/
gen: @.=; @.1 = "---Monday's Child Is Fair of Face (by Mother Goose)---"
@.2 = "Monday's child is fair of face;"
@.3 = "Tuesday's child is full of grace;"
@.4 = "Wednesday's child is full of woe;"
@.5 = "Thursday's child has far to go;"
@.6 = "Friday's child is loving and giving;"
@.7 = "Saturday's child works hard for a living;"
@.8 = "But the child that is born on the Sabbath day"
@.9 = "Is blithe and bonny, good and gay."
do #=1 while @.#\==''; end; #=#-1 /*determine how many entries in @ array*/
return
/*──────────────────────────────────INSERTIONSORT subroutine──────────────────*/
insertionSort: procedure expose @.; parse arg #
do i=2 to #; $=@.i
do j=i-1 by -1 while j\==0 & @.j>$
_=j+1; @._=@.j
end /*j*/
_=j+1; @._=$
end /*i*/
return
/*──────────────────────────────────SHOW subroutine───────────────────────────*/
show: do j=1 for #; say 'element' right(j,length(#)) arg(1)': ' @.j; end; return
/*REXX program sorts a stemmed array (has characters) using the insertion sort algorithm*/
call gen /*generate the array's (data) elements.*/
call show 'before sort' /*display the before array elements. */
say copies('', 85) /*display a separator line (a fence). */
call insertionSort # /*invoke the insertion sort. */
call show ' after sort' /*display the after array elements. */
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
gen: @.=; @.1 = "---Monday's Child Is Fair of Face (by Mother Goose)---"
@.2 = "======================================================="
@.3 = "Monday's child is fair of face;"
@.4 = "Tuesday's child is full of grace;"
@.5 = "Wednesday's child is full of woe;"
@.6 = "Thursday's child has far to go;"
@.7 = "Friday's child is loving and giving;"
@.8 = "Saturday's child works hard for a living;"
@.9 = "But the child that is born on the Sabbath day"
@.10 = "Is blithe and bonny, good and gay."
do #=1 while @.#\==''; end; #=#-1 /*determine how many entries in @ array*/
return
/*──────────────────────────────────────────────────────────────────────────────────────*/
insertionSort: procedure expose @.; parse arg #
do i=2 to #; $=@.i; do j=i-1 by -1 to 1 while @.j>$
_=j+1; @._=@.j
end /*j*/
_=j+1; @._=$
end /*i*/
return
/*──────────────────────────────────────────────────────────────────────────────────────*/
show: do j=1 for #; say ' element' right(j,length(#)) arg(1)": " @.j; end; return

View file

@ -1,9 +1,9 @@
#lang racket
(define (sort < l)
(define (insert x y)
(match* (x y)
[(x '()) (list x)]
[(x (cons y ys)) (cond [(< x y) (list* x y ys)]
[else (cons y (insert x ys))])]))
(define (insert x ys)
(match ys
[(list) (list x)]
[(cons y rst) (cond [(< x y) (cons x ys)]
[else (cons y (insert x rst))])]))
(foldl insert '() l))