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,9 +1,9 @@
defmodule Sort do
def gnome_sort(list) when length(list) <= 1, do: list
def gnome_sort([]), do: []
def gnome_sort([h|t]), do: gnome_sort([h], t)
defp gnome_sort(list, []), do: list
defp gnome_sort([prev|p], [next|n]) when next > prev, do: gnome_sort(p, [next|[prev|n]])
defp gnome_sort([prev|p], [next|n]) when next > prev, do: gnome_sort(p, [next,prev|n])
defp gnome_sort(p, [next|n]), do: gnome_sort([next|p], n)
end

View file

@ -1,4 +1,4 @@
sub gnome_sort (@a is rw) {
sub gnome_sort (@a) {
my ($i, $j) = 1, 2;
while $i < @a {
if @a[$i - 1] <= @a[$i] {
@ -6,7 +6,8 @@ sub gnome_sort (@a is rw) {
}
else {
(@a[$i - 1], @a[$i]) = @a[$i], @a[$i - 1];
--$i or ($i, $j) = $j, $j + 1;
$i--;
($i, $j) = $j, $j + 1 if $i == 0;
}
}
}

View file

@ -1,25 +1,25 @@
/*REXX program sorts a stemmed array using the gnome sort algorithm. */
call gen; w=length(#) /*generate @ array; W is width of #.*/
call show 'before sort' /*display the "before" array elements.*/
say copies('',60) /*show a separator line between sorts. */
call gnomeSort # /*invoke the well─known gnome sort. */
call show ' after sort' /*display the "after" array elements.*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────GNOMESORT subroutine──────────────────────*/
gnomeSort: procedure expose @.; parse arg n; k=2 /*N: is number items.*/
do j=3 while k<=n; p=k-1 /*P: is previous item*/
if @.p<<=@.k then do; k=j; iterate; end /*array is OK so far.*/
_=@.p; @.p=@.k; @.k=_ /*swap two @ entries.*/
k=k-1; if k==1 then k=j; else j=j-1 /*test for 1st index.*/
end /*j*/
return
/*──────────────────────────────────GEN subroutine────────────────────────────*/
gen: @.=; @.1 = '---the seven virtues---' ; @.5 = 'Charity [Love]'
@.2 = '=======================' ; @.6 = 'Fortitude'
@.3 = 'Faith' ; @.7 = 'Justice'
@.4 = 'Hope' ; @.8 = 'Prudence'
@.9 = 'Temperance'
do #=1 while @.#\==''; end; #=#-1 /*determine number of items in @ array.*/
return
/*──────────────────────────────────SHOW subroutine───────────────────────────*/
show: do j=1 for #; say ' element' right(j,w) arg(1)":" @.j; end; return
/*REXX program sorts an array using the gnome sort algorithm (elements contain blanks). */
call gen /*generate the @ stemmed array. */
call show 'before sort' /*display the before array elements.*/
say copies('', 60) /*show a separator line between sorts. */
call gnomeSort # /*invoke the well─known gnome sort. */
call show ' after sort' /*display the after array elements.*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
gnomeSort: procedure expose @.; parse arg n; k=2 /*N: is number items. */
do j=3 while k<=n; p=k-1 /*P: is previous item.*/
if @.p<<=@.k then do; k=j; iterate; end /*order is OK so far. */
_=@.p; @.p=@.k; @.k=_ /*swap two @ entries. */
k=k-1; if k==1 then k=j; else j=j-1 /*test for 1st index. */
end /*j*/
return
/*──────────────────────────────────────────────────────────────────────────────────────*/
gen: @.=; @.1 = '---the seven virtues---' ; @.5 = "Charity [Love]"
@.2 = '=======================' ; @.6 = "Fortitude"
@.3 = 'Faith' ; @.7 = "Justice"
@.4 = 'Hope' ; @.8 = "Prudence"
@.9 = "Temperance"
do #=1 while @.#\==''; end; #=#-1; w=length(#) /*find # of items.*/
return
/*──────────────────────────────────────────────────────────────────────────────────────*/
show: do j=1 for #; say ' element' right(j,w) arg(1)":" @.j; end; return